From: Bernhard Tittelbach Date: Mon, 25 Nov 2013 05:10:05 +0000 (+0000) Subject: use new termios api X-Git-Url: https://git.realraum.at/?a=commitdiff_plain;h=6656f26adaada3bd9341595a7312f6a4ba0e181c;p=svn42.git use new termios api --- diff --git a/go/door_daemon_zmq/main.go b/go/door_daemon_zmq/main.go index 32096d7..84db848 100644 --- a/go/door_daemon_zmq/main.go +++ b/go/door_daemon_zmq/main.go @@ -46,7 +46,7 @@ func main() { defer pub_chans.Close() defer zmqctx.Close() - serial_wr, serial_rd, err := OpenAndHandleSerial(door_tty_path_) + serial_wr, serial_rd, err := OpenAndHandleSerial(door_tty_path_, 0) defer close(serial_wr) if err != nil { panic(err) diff --git a/go/door_daemon_zmq/serial_tty.go b/go/door_daemon_zmq/serial_tty.go index e609971..ac6b1bb 100644 --- a/go/door_daemon_zmq/serial_tty.go +++ b/go/door_daemon_zmq/serial_tty.go @@ -6,21 +6,33 @@ import ( "bufio" "bytes" "os" + "errors" "svn.spreadspace.org/realraum/go.svn/termios" ) // ---------- Serial TTY Code ------------- -func openTTY(name string) (*os.File, error) { - file, err := os.OpenFile(name,os.O_RDWR, 0600) // For read access. - if err != nil { - Syslog_.Println(err.Error()) - return nil, err +func openTTY(name string, speed uint) (file *os.File, err error) { + file, err = os.OpenFile(name,os.O_RDWR, 0666) + if err != nil { return } + if err = termios.SetRawFile(file); err != nil { return } + switch speed { + case 0: // set no baudrate + case 1200: err = termios.SetSpeedFile(file, termios.B1200) + case 2400: err = termios.SetSpeedFile(file, termios.B2400) + case 4800: err = termios.SetSpeedFile(file, termios.B4800) + case 9600: err = termios.SetSpeedFile(file, termios.B9600) + case 19200: err = termios.SetSpeedFile(file, termios.B19200) + case 38400: err = termios.SetSpeedFile(file, termios.B38400) + case 57600: err = termios.SetSpeedFile(file, termios.B57600) + case 115200: err = termios.SetSpeedFile(file, termios.B115200) + case 230400: err = termios.SetSpeedFile(file, termios.B230400) + default: + file.Close() + err = errors.New("Unsupported Baudrate, use 0 to disable setting a baudrate") } - termios.Ttyfd(file.Fd()) - termios.SetRaw() - return file, nil + return } func serialWriter(in <- chan string, serial * os.File) { @@ -46,8 +58,8 @@ func serialReader(out chan <- [][]byte, serial * os.File) { } } -func OpenAndHandleSerial(filename string) (chan string, chan [][]byte, error) { - serial, err :=openTTY(filename) +func OpenAndHandleSerial(filename string, serspeed uint) (chan string, chan [][]byte, error) { + serial, err :=openTTY(filename, serspeed) if err != nil { return nil, nil, err } diff --git a/go/uc_sensor_node_zmq/main.go b/go/uc_sensor_node_zmq/main.go index 2e3c0de..2c7e0ae 100644 --- a/go/uc_sensor_node_zmq/main.go +++ b/go/uc_sensor_node_zmq/main.go @@ -14,8 +14,9 @@ import ( var ( tty_dev_ string pub_addr string - use_syslog_ bool - enable_debug_ bool + use_syslog_ bool + enable_debug_ bool + serial_speed_ uint ) const exponential_backof_activation_threshold int64 = 4 @@ -23,6 +24,7 @@ const exponential_backof_activation_threshold int64 = 4 func init() { flag.StringVar(&pub_addr, "brokeraddr", "tcp://torwaechter.realraum.at:4243", "zmq address to send stuff to") flag.StringVar(&tty_dev_, "ttydev", "/dev/ttyACM0", "path do tty uc device") + flag.UintVar(&serial_speed_, "serspeed", 0, "tty baudrate (0 to disable setting a baudrate e.g. in case of ttyACM)") flag.BoolVar(&use_syslog_, "syslog", false, "log to syslog local1 facility") flag.BoolVar(&enable_debug_, "debug", false, "debugging messages on") flag.Parse() @@ -33,7 +35,7 @@ func ConnectSerialToZMQ(pub_sock *zmq.Socket) { if x:= recover(); x != nil { Syslog_.Println(x) } }() - serial_wr, serial_rd, err := OpenAndHandleSerial(tty_dev_) + serial_wr, serial_rd, err := OpenAndHandleSerial(tty_dev_, serial_speed_) if err != nil { panic(err) } defer close(serial_wr) diff --git a/go/uc_sensor_node_zmq/serial_tty.go b/go/uc_sensor_node_zmq/serial_tty.go index de06bdd..a73f384 100644 --- a/go/uc_sensor_node_zmq/serial_tty.go +++ b/go/uc_sensor_node_zmq/serial_tty.go @@ -6,21 +6,33 @@ import ( "bufio" "bytes" "os" + "errors" "svn.spreadspace.org/realraum/go.svn/termios" ) // ---------- Serial TTY Code ------------- -func openTTY(name string) (*os.File, error) { - file, err := os.OpenFile(name,os.O_RDWR, 0600) // For read access. - if err != nil { - Syslog_.Print(err.Error()) - return nil, err +func openTTY(name string, speed uint) (file *os.File, err error) { + file, err = os.OpenFile(name,os.O_RDWR, 0600) + if err != nil { return } + if err = termios.SetRawFile(file); err != nil { return } + switch speed { + case 0: // set no baudrate + case 1200: err = termios.SetSpeedFile(file, termios.B1200) + case 2400: err = termios.SetSpeedFile(file, termios.B2400) + case 4800: err = termios.SetSpeedFile(file, termios.B4800) + case 9600: err = termios.SetSpeedFile(file, termios.B9600) + case 19200: err = termios.SetSpeedFile(file, termios.B19200) + case 38400: err = termios.SetSpeedFile(file, termios.B38400) + case 57600: err = termios.SetSpeedFile(file, termios.B57600) + case 115200: err = termios.SetSpeedFile(file, termios.B115200) + case 230400: err = termios.SetSpeedFile(file, termios.B230400) + default: + file.Close() + err = errors.New("Unsupported Baudrate, use 0 to disable setting a baudrate") } - termios.Ttyfd(file.Fd()) - termios.SetRaw() - return file, nil + return } func serialWriter(in <- chan string, serial * os.File) { @@ -46,8 +58,8 @@ func serialReader(out chan <- [][]byte, serial * os.File) { } } -func OpenAndHandleSerial(filename string) (chan string, chan [][]byte, error) { - serial, err :=openTTY(filename) +func OpenAndHandleSerial(filename string, serspeed uint) (chan string, chan [][]byte, error) { + serial, err :=openTTY(filename, serspeed) if err != nil { return nil, nil, err }