func main()

in nmxact/example/serial_plain/serial_plain.go [32:85]


func main() {
	// Initialize the serial transport.
	cfg := nmserial.NewXportCfg()
	cfg.DevPath = "/dev/cu.usbserial-A600ANJ1"
	cfg.Baud = 115200
	cfg.ReadTimeout = 3 * time.Second

	x := nmserial.NewSerialXport(cfg)

	// Start the serial transport.
	if err := x.Start(); err != nil {
		fmt.Fprintf(os.Stderr, "error starting serial transport: %s\n",
			err.Error())
		os.Exit(1)
	}
	defer x.Stop()

	// Create and open a session for connected Mynewt device.
	sc := sesn.NewSesnCfg()
	sc.MgmtProto = sesn.MGMT_PROTO_NMP

	s, err := x.BuildSesn(sc)
	if err != nil {
		fmt.Fprintf(os.Stderr, "error creating serial session: %s\n",
			err.Error())
		os.Exit(1)
	}

	if err := s.Open(); err != nil {
		fmt.Fprintf(os.Stderr, "error starting serial session: %s\n",
			err.Error())
		os.Exit(1)
	}
	defer s.Close()

	// Send an echo command to the device.
	c := xact.NewEchoCmd()
	c.Payload = "hello"

	res, err := c.Run(s)
	if err != nil {
		fmt.Fprintf(os.Stderr, "error executing echo command: %s\n",
			err.Error())
		os.Exit(1)
	}

	if res.Status() != 0 {
		fmt.Printf("Device responded negatively to echo command; status=%d\n",
			res.Status())
	}

	eres := res.(*xact.EchoResult)
	fmt.Printf("Device echoed back: %s\n", eres.Rsp.Payload)
}