Exemplo n.º 1
0
func Open(portName string) (*Flasher, error) {
	mode := &serial.Mode{
		BaudRate: 115200,
	}

	port, err := serial.OpenPort(portName, mode)
	if err != nil {
		return nil, &FlasherError{err: "Error opening serial port. " + err.Error()}
	}

	flasher := &Flasher{
		port: port,
	}

	// Wait for the complete reset of the board
	time.Sleep(2500 * time.Millisecond)

	return flasher, err
}
Exemplo n.º 2
0
func ExampleCommunication() {
	ports, err := serial.GetPortsList()
	if err != nil {
		log.Fatal(err)
	}
	if len(ports) == 0 {
		log.Fatal("No serial ports found!")
	}

	for _, port := range ports {
		fmt.Printf("Found port: %v\n", port)
	}

	mode := &serial.Mode{
		BaudRate: 9600,
		Parity:   serial.PARITY_NONE,
		DataBits: 8,
		StopBits: serial.STOPBITS_ONE,
	}
	port, err := serial.OpenPort(ports[0], mode)
	if err != nil {
		log.Fatal(err)
	}
	n, err := port.Write([]byte("10,20,30\n\r"))
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("Sent %v bytes\n", n)

	buff := make([]byte, 100)
	for {
		n, err := port.Read(buff)
		if err != nil {
			log.Fatal(err)
			break
		}
		if n == 0 {
			fmt.Println("\nEOF")
			break
		}
		fmt.Printf("%v", string(buff[:n]))
	}
}