Example #1
0
File: main.go Project: phicode/sio
func main() {
	baudRate := flag.Int("baud", 57600, "Baud rate")
	dev := "/dev/ttyS0"
	flag.Parse()
	switch flag.NArg() {
	case 0:
		// nop
	case 1:
		dev = flag.Arg(0)
	default:
		logFatalf("expected max 1 arg: the serial port device, default is /dev/ttyS0")
	}

	port, err := sio.Open(dev, bauds[*baudRate])
	if err != nil {
		logFatalf("open: %s", err)
	}

	rxbuf := []byte{0}
	ofs := 0
	for {
		n, err := port.Read(rxbuf)
		if err != nil {
			logFatalf("read: %s", err)
		}

		if n != len(rxbuf) {
			logFatalf("short read: %d %d", n, len(rxbuf))
		}

		if ofs%16 == 0 {
			fmt.Printf("\n%04x: ", ofs)
		}
		fmt.Printf("%02x ", rxbuf[0])
		ofs++
	}
}
Example #2
0
File: main.go Project: phicode/sio
func main() {
	oDTR := flag.Bool("dtr", false, "excercise the DTR control line")
	oDSR := flag.Bool("dsr", false, "excercise the DSR control line")
	oRTS := flag.Bool("rts", false, "excercise the RTS control line")
	oCTS := flag.Bool("cts", false, "excercise the CTS control line")
	oHalf := flag.Duration("t", 100*time.Millisecond, "flipping time")
	baudRate := flag.Int("baud", 57600, "Baud rate")
	flag.Parse()
	if !*oDTR && !*oRTS && !*oDSR && !*oCTS {
		log.Fatal("Please specify at least on of -dtr -rts -dsr -cts")
	}

	dev := "/dev/ttyS0"
	switch flag.NArg() {
	case 0:
		// nop
	case 1:
		dev = flag.Arg(0)
	default:
		log.Fatalf("expected max 1 arg: the serial port device, default is %s", dev)
	}

	port, err := sio.Open(dev, bauds[*baudRate])
	if err != nil {
		log.Fatal(err)
	}

	c := time.Tick(*oHalf)
	var on bool
	for {
		<-c
		if *oDTR {
			if err := port.SetDTR(on); err != nil {
				port.Close()
				log.Fatal(err)
			}
		}

		if *oDSR {
			if err := port.SetDSR(on); err != nil {
				port.Close()
				log.Fatal(err)
			}
		}

		if *oRTS {
			if err := port.SetRTS(on); err != nil {
				port.Close()
				log.Fatal(err)
			}
		}

		if *oCTS {
			if err := port.SetCTS(on); err != nil {
				port.Close()
				log.Fatal(err)
			}
		}

		on = !on
	}
}
Example #3
0
File: main.go Project: phicode/sio
func main() {
	baudRate := flag.Int("baud", 57600, "Baud rate")
	server := flag.Bool("server", false, "Send dummy text to the serial device continuously")
	echo := flag.Bool("echo", false, "ping client/server, combine w/ -server")
	verbose := flag.Bool("v", false, "verbose (applies to -echo)")
	slow := flag.Bool("slow", false, "slow echo server to 1 byte/sec")
	flag.Parse()
	dev := "/dev/ttyS0"
	switch flag.NArg() {
	case 0:
		// nop
	case 1:
		dev = flag.Arg(0)
	default:
		logFatalf("expected max 1 arg: the serial port device, default is /dev/ttyS0")
	}

	port, err := sio.Open(dev, bauds[*baudRate])
	if err != nil {
		logFatalf("open: %s", err)
	}

	switch *echo {
	case false:
		switch *server {
		case true:
			var s string
			var sum int
			pass := 0
			for {
				pass++
				s = fmt.Sprintf("%d(%d): %s @ %s.\n", pass, sum, serve, time.Now())
				n, err := port.Write([]byte(s))
				if err != nil {
					logFatalf("write: %s", err)
				}

				if n != len(s) {
					logFatalf("short write: %d %d", n, len(serve))
				}

				fmt.Print(s)
				sum += n
			}
		case false:
			io.Copy(os.Stdout, port)
		}
	case true:
		switch *server {
		case true:
			pass := 0
			for {
				pass++
				rxbuf := []byte{0}
				n, err := port.Read(rxbuf)
				if err != nil {
					logFatalf("read: %s", err)
				}

				if n != len(rxbuf) {
					logFatalf("short read: %d %d", n, len(rxbuf))
				}

				if *verbose {
					fmt.Printf("rx(%d): %02xH\n", pass, rxbuf[0])
				}

				if *slow {
					<-time.After(time.Second)
				}

				txbuf := []byte{0}
				copy(txbuf, rxbuf)
				if n, err = port.Write(txbuf); err != nil {
					logFatalf("write: %s", err)
				}

				if n != len(txbuf) {
					logFatalf("short write: %d %d", n, len(txbuf))
				}

				if *verbose {
					fmt.Printf("tx(%d): %02xH\n", pass, txbuf[0])
				}

			}
		case false:
			pass := 0
			for txbuf, rxbuf := []byte{' '}, []byte{0}; ; txbuf[0]++ {
				pass++

				n, err := port.Write(txbuf)
				if err != nil {
					logFatalf("write: %s", err)
				}

				if n != len(txbuf) {
					logFatalf("short write: %d %d", n, len(txbuf))
				}

				if *verbose {
					fmt.Printf("tx(%d): %02xH\n", pass, txbuf)
					os.Stdout.Sync()
				}

				if n, err = port.Read(rxbuf); err != nil {
					logFatalf("read: %s", err)
				}

				if n != len(rxbuf) {
					logFatalf("short read: %d %d", n, len(rxbuf))
				}

				if !bytes.Equal(txbuf, rxbuf) {
					logFatalf(
						"echoed data mismatch:\nTX:\n%s\nRX:\n%s",
						hex.Dump(txbuf),
						hex.Dump(rxbuf),
					)
				}

				if *verbose {
					fmt.Printf("rx(%d): %02xH\n", pass, rxbuf[0])
				}

			}
		}
	}
}