Ejemplo n.º 1
0
// handle the serial port communication. When data comes in through
// the serial port write it to the channel for the socket to see. When
// data comes in from the socket over the channel translate the message
// and write to serial port
func handlePort(p *serial.Port, ch chan []byte) {
	readCh := make(chan []byte)

	go serialRead(p, readCh)

	for {
		select {
		case s := <-ch:
			{
				// map gui -> micro
				trans := mapGui.ItemTranslate(string(s))
				_, err := p.Write([]byte(trans))
				if err != nil {
					fmt.Println(err)
				}
			}
		case r := <-readCh:
			{
				ch <- r
			}
		case <-time.After(timeout):
			continue
		}
	}
}
Ejemplo n.º 2
0
// serial read, we run this in a goroutine so we can block on read
// on data read send through the channel
func serialRead(p *serial.Port, ch chan []byte) {
	buf := make([]byte, 256)
	for {
		n, err := p.Read(buf)
		if err != nil && err != io.EOF {
			log.Fatal(err)
		}
		if n > 0 {
			ch <- buf[:n]
		}
	}
}