Example #1
0
func main() {
	ttyPtr := flag.String("tty", "/dev/ttymxc1", "Open tty device")
	baudPtr := flag.Int("baud", 115200, "Set baud rate")
	flag.Parse()

	c := &serial.Config{
		Name:        *ttyPtr,
		Baud:        *baudPtr,
		ReadTimeout: time.Millisecond * 500,
	}

	// this is the channel that moves data from the serial and socket
	// goroutines
	ch := make(chan []byte)

	s, err := serial.OpenPort(c)
	if err != nil {
		log.Fatal(err)
	}

	_, err = s.Write([]byte("Hello!"))
	if err != nil {
		log.Fatal(err)
	}

	go handlePort(s, ch)

	laddr, err := net.ResolveUnixAddr("unix", "/tmp/tioSocket")
	if nil != err {
		log.Fatalln(err)
	}

	listener, err := net.ListenUnix("unix", laddr)
	if nil != err {
		log.Fatalln(err)
	}
	log.Println("listening on", listener.Addr())
	go accept(listener, ch)

	// Handle SIGINT and SIGTERM.
	ex := make(chan os.Signal)
	signal.Notify(ex, syscall.SIGINT, syscall.SIGTERM)
	log.Println(<-ex)

	log.Println("Closing up...")
	s.Close()
}
Example #2
0
func main() {
	ttyPtr := flag.String("tty", "/dev/ttymxc1", "Open tty device")
	baudPtr := flag.Int("baud", 115200, "Set baud rate")
	flag.Parse()

	c := &serial.Config{
		Name:        *ttyPtr,
		Baud:        *baudPtr,
		ReadTimeout: time.Millisecond * 500,
	}

	s, err := serial.OpenPort(c)
	if err != nil {
		log.Fatal("fatal error: ", err)
	}
	defer s.Close()

	for j := 0; j < 100; j++ {

		for i := 0; i < 18; i++ {
			w := fmt.Sprintf("w=%d\n", random(0, i+1))
			f := fmt.Sprintf("f=%d\n", random(0, i+1))
			t := fmt.Sprintf("t=%d\n", random(0, i+1))
			p := fmt.Sprintf("s=%d\n", random(0, (i+1)*10))
			o := fmt.Sprintf("o=%d\n", i%2)

			_, err := s.Write([]byte(w))
			checkError(err)
			time.Sleep((time.Duration(dur) * time.Millisecond))
			_, err = s.Write([]byte(f))
			checkError(err)
			time.Sleep((time.Duration(dur) * time.Millisecond))
			_, err = s.Write([]byte(t))
			checkError(err)
			time.Sleep((time.Duration(dur) * time.Millisecond))
			_, err = s.Write([]byte(o))
			checkError(err)
			time.Sleep((time.Duration(dur) * time.Millisecond))
			_, err = s.Write([]byte(p))
			checkError(err)

		}

		for i := 18; i > 0; i-- {
			w := fmt.Sprintf("w=%d\n", random(0, i+1))
			f := fmt.Sprintf("f=%d\n", random(0, i+1))
			t := fmt.Sprintf("t=%d\n", random(0, i+1))
			p := fmt.Sprintf("s=%d\n", random(0, (i+1)*10))
			o := fmt.Sprintf("o=%d\n", i%2)

			_, err := s.Write([]byte(w))
			checkError(err)
			time.Sleep((time.Duration(dur) * time.Millisecond))
			_, err = s.Write([]byte(f))
			checkError(err)
			time.Sleep((time.Duration(dur) * time.Millisecond))
			_, err = s.Write([]byte(t))
			checkError(err)
			time.Sleep((time.Duration(dur) * time.Millisecond))
			_, err = s.Write([]byte(o))
			checkError(err)
			time.Sleep((time.Duration(dur) * time.Millisecond))
			_, err = s.Write([]byte(p))
			checkError(err)
			time.Sleep((time.Duration(dur) * time.Millisecond))
		}
	}
}