Exemplo n.º 1
0
func main() {
	// устанавливаем соединение
	port, err := sio.Open("/dev/ttyACM0", syscall.B9600)
	if err != nil {
		log.Fatal(err)
	}

	time.Sleep(2 * time.Second)

	anaconda.SetConsumerKey(consumerKey)
	anaconda.SetConsumerSecret(consumerSecret)
	api := anaconda.NewTwitterApi(key, secretKey)
	var twitt string
	var searchResult []anaconda.Tweet

	for {

		searchResult, _ = api.GetSearch("#golang", nil)
		twitt = " -- " + searchResult[0].Text
		fmt.Println(twitt)

		// отправляем данные
		_, err = port.Write([]byte(twitt))
		if err != nil {
			log.Fatal(err)
		}

		time.Sleep(120 * time.Second)
	}
}
Exemplo n.º 2
0
func Open(name string, rows, cols uint8) (*Device, error) {
	p, err := sio.Open(name, 9600)
	if err != nil {
		return nil, err
	}

	d := &Device{
		rows: rows,
		cols: cols,
		p:    p,
	}

	if err = d.send(cmdLCDSize, cols, rows); err != nil {
		return nil, err
	}
	return d, nil
}
Exemplo n.º 3
0
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++
	}
}
Exemplo n.º 4
0
func main() {
	// Command line flags are defined here
	debug := flag.Bool("d", false, "Debug mode, extra logging information shown on stderr")
	verbose := flag.Bool("v", false, "Verbose mode, be chatty")
	help := flag.Bool("h", false, "This help message")
	pgn := flag.Int("pgn", 0, "Display only this PGN")
	src := flag.Int("source", 255, "Display PGNs from this source only")
	quiet := flag.Bool("q", false, "Don't display PGN data")
	addr := flag.String("addr", ":8081", "http service address")
	stats := flag.Bool("s", false, "Display live statistics")
	dev_type := flag.String("dev", "actisense", "Choose type of device: actisense, canusb")
	no_server := flag.Bool("no-server", false, "Don't start Web Sockets or ZeroMQ")
	map_file := flag.String("map", "map.xml", "File to use for mapping between input and Signal K")
	device := "/dev/ttyUSB0"

	flag.Parse()

	if *dev_type != "canusb" && *dev_type != "actisense" {
		log.Fatalln("expected either canusb or actisense, got", *dev_type)
	}

	switch flag.NArg() {
	case 0:
		// Use default device
	case 1:
		device = flag.Arg(0)
	default:
		log.Fatalln("expected max 1 arg for the serial port device, default is", device)
	}

	if *help {
		flag.PrintDefaults()
		return
	}

	if *debug {
		log.Println("opening", device)
	}

	txch := make(chan nmea2k.ParsedMessage)
	cmdch := make(chan CommandRequest)

	statLog := make(map[uint32]uint64)
	var statPgns UintSlice

	data, err := ioutil.ReadFile(*map_file)
	if err != nil {
		log.Fatalln("could not read XML map file:", err, *map_file)
	}

	map_data := Mappings{}

	err = xml.Unmarshal(data, &map_data)
	if err != nil {
		log.Fatalln("could not parse XML map file:", err, *map_file)
	}

	port, err := sio.Open(device, syscall.B230400)

	if err != nil {
		log.Fatalln("open: %s", err)
	}

	var canport can.ReadWriter

	socket := new(zmq.Socket)
	if !*no_server {

		// Start up the WebSockets hub
		go h.run()

		socket, _ = zmq.NewSocket(zmq.PUB)
		defer socket.Close()
		socket.Bind("tcp://*:5555")

		// PgnDefServer causes invalid heap pointer error
		//go PgnDefServer()

		go WebSocketServer(addr)

		go ApiServer(cmdch)
	}

	// Print and transmit received messages
	go func() {
		for {
			res := <-txch

			if (*pgn == 0 || int(res.Header.Pgn) == *pgn) &&
				(*src == 255 || int(res.Header.Source) == *src) &&
				(*quiet == false) && (*stats == false) {
				if *debug {
					fmt.Println(res.Header.Print(*verbose))
				}
				fmt.Println(res.Print(*verbose))
			}

			if *stats {
				if _, ok := statLog[res.Header.Pgn]; ok {
					statLog[res.Header.Pgn]++
				} else {
					statLog[res.Header.Pgn] = 1
					statPgns = append(statPgns, res.Header.Pgn)
					sort.Sort(statPgns)
				}
				terminal.Stdout.Clear()
				for _, k := range statPgns {
					fmt.Println(k, "=>", statLog[k])
				}
			}

			if !*no_server {
				//bm := res.MsgPack()
				//socket.SendBytes(bm, 0)

				bj, err := map_data.Delta(&res)
				if err == nil {
					bytes, err := json.Marshal(bj)
					if err == nil {
						h.broadcast <- bytes
					}
				}
			}
		}
	}()

	// Set up hardware
	if *debug {
		fmt.Println(*dev_type)
	}
	if *dev_type == "canusb" {
		if *debug {
			fmt.Println("Adding Fast Packets")
		}
		for _, p := range nmea2k.PgnList {
			if p.Size > 8 {
				if *debug {
					log.Println("Adding PGN:", p.Pgn)
				}
				canusb.AddFastPacket(p.Pgn)
			}
		}

		if *debug {
			fmt.Println("Opening Channel")
		}
		canport, _ = canusb.OpenChannel(port, 221)
	} else {
		canport, _ = actisense.OpenChannel(port)
		time.Sleep(2)
	}

	// Handle command requests
	go func() {
		for {
			req := <-cmdch

			if req.RequestType == "iso" {
				b0 := (byte)(req.RequestedPgn) & 0xFF
				b1 := (byte)(req.RequestedPgn>>8) & 0xFF
				b2 := (byte)(req.RequestedPgn>>16) & 0xFF
				if canport != nil {
					canport.Write([]byte{0x03, 0x00, 0xEA, 0x00, 0xFF, 0x03, b0, b1, b2})
				} else {
					log.Println("canport is nil")
				}
			}
		}
	}()

	// Read from hardware
	if *dev_type == "canusb" {
		for {
			raw, err := canport.Read()
			if err == nil {
				txch <- *(nmea2k.ParsePacket(raw))
			}
		}
	} else {
		for {
			raw, err := canport.Read()
			if err == nil {
				txch <- *(nmea2k.ParsePacket(raw))
			}
		}
	}
}
Exemplo n.º 5
0
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])
				}

			}
		}
	}
}
Exemplo n.º 6
0
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
	}
}