Skip to content

Instantly share code, notes, and snippets.

@facchinm
Created June 30, 2015 15:53
Show Gist options
  • Save facchinm/177bdf48b0d4143e9737 to your computer and use it in GitHub Desktop.
Save facchinm/177bdf48b0d4143e9737 to your computer and use it in GitHub Desktop.
package main
import (
//"flag"
"bytes"
//"crypto/rand"
"fmt"
"github.com/facchinm/go-serial"
"math/rand"
"os"
"strconv"
"time"
)
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func randSeq(n int) string {
rand.Seed(time.Now().UTC().UnixNano())
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
func main() {
var serialName string
var command string
length := 0
if len(os.Args) < 3 {
fmt.Println("usage: ./usbtest-go <serial-name> <test_code> <parameters>")
}
serialName = os.Args[1]
command = os.Args[2]
fmt.Println("Port: " + serialName)
fmt.Println("Command: " + command)
if command == "s" {
length, _ = strconv.Atoi(os.Args[3])
fmt.Println("Size: " + os.Args[3])
} else {
if command == "l" && len(os.Args) == 4 {
length, _ = strconv.Atoi(os.Args[3])
fmt.Println("Size: " + os.Args[3])
} else {
length = 64
}
}
mode := &serial.Mode{
BaudRate: 115200,
Vmin: 1, //byte(length),
Vtimeout: 0,
}
p, err := serial.OpenPort(serialName, mode)
if err != nil {
fmt.Println("Error opening port " + err.Error())
return
}
speedTestBuffer := make([]byte, length)
//create the buffer
i := 0
for i < length {
speedTestBuffer[i] = (byte)(i & 0xFF)
i++
}
if command[0] == 's' {
command = command + " " + strconv.Itoa(length)
}
p.Write([]byte(command))
i = 0
total_bytes := 0
time_start := time.Now()
if command[0] == 's' {
fmt.Println(speedTestBuffer)
ch := make([]byte, 1024)
for {
n, _ := p.Read(ch)
k := n
//fmt.Println(ch[:n])
if n > 0 && total_bytes == 0 {
time_start = time.Now()
}
for k > 0 {
//fmt.Printf("%x %x\n", byte(i), ch[n-k])
if byte(i) == ch[n-k] {
//fmt.Println("data ok")
} else {
fmt.Println("data broken")
}
k = k - 1
i = (i + 1) % length
total_bytes = total_bytes + 1
}
if total_bytes%10000 == 0 {
speed := float64(total_bytes) / time.Since(time_start).Seconds()
fmt.Printf("%3.2f kbytes/s\n", speed/1000)
}
//i := 0
//for n > 0 {
// data := ch[i*length : (i+1)*length]
// n = n - length
// if bytes.Compare(data, speedTestBuffer) == 0 {
// fmt.Println("data ok")
// fmt.Println([]byte(data))
// } else {
// fmt.Println("data broken")
// fmt.Println([]byte(data))
// }
//}
}
}
expected_str := []byte("0123456789abcdeABCDE=)(/&qwertyuiopasdfghjklzQWERT")
if command[0] == 'b' {
ch := make([]byte, 1024)
for {
n, _ := p.Read(ch)
k := n
//fmt.Println(string(ch[:n]))
if n > 0 && total_bytes == 0 {
time_start = time.Now()
}
for k > 0 {
//fmt.Printf("%c %c\n", expected_str[i], ch[n-k])
if expected_str[i] == ch[n-k] {
//fmt.Println("data ok")
} else {
fmt.Println("data broken")
}
k = k - 1
i = (i + 1) % len(expected_str)
total_bytes = total_bytes + 1
}
if total_bytes%10000 == 0 {
speed := float64(total_bytes) / time.Since(time_start).Seconds()
fmt.Printf("%3.2f kbytes/s\n", speed/1000)
}
if total_bytes >= 20000*len(expected_str) {
fmt.Println("completed in " + time.Since(time_start).String())
return
}
}
}
random_str := []byte(randSeq(length))
if command[0] == 'l' {
fmt.Println(string(random_str))
ch := make([]byte, 1024)
// dummy read to empty the buffer
//p.Read(ch)
for {
i = (i + 1) % len(random_str)
if i == 0 {
i = i + 1
}
loopback_str := random_str[:i]
if total_bytes == 0 {
time_start = time.Now()
}
p.Write([]byte(loopback_str))
k := 0
var slice []byte
for k < i {
n, _ := p.Read(ch)
k = k + n
slice = append(slice, ch[:n]...)
}
total_bytes = total_bytes + k
//fmt.Println(string(slice))
if bytes.Compare(random_str[:i], slice) == 0 {
//fmt.Println("data ok")
} else {
fmt.Println(string(slice))
fmt.Println("data broken")
}
i = i + 1
if total_bytes%10000 > 0 && total_bytes%10000 < 100 {
speed := float64(total_bytes) / time.Since(time_start).Seconds()
fmt.Printf("%3.2f kbytes/s\n", speed/1000)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment