From: Bernhard Tittelbach Date: Fri, 5 Dec 2014 21:17:03 +0000 (+0000) Subject: reopen ttyACM0 after timeout X-Git-Url: https://git.realraum.at/?a=commitdiff_plain;h=b558aac395859de274b40cb0d96f3932721bcaf5;p=svn42.git reopen ttyACM0 after timeout --- diff --git a/go/termios/termios_mips.go b/go/termios/termios_mips.go index b92fd0d..e8cf82a 100644 --- a/go/termios/termios_mips.go +++ b/go/termios/termios_mips.go @@ -19,25 +19,25 @@ type tcflag_t uint32 const ( IGNBRK = tcflag_t (0000001) BRKINT = tcflag_t (0000002) - IGNPAR = tcflag_t (0000004) - PARMRK = tcflag_t (0000010) - INLCR = tcflag_t (0000100) - ECHONL = tcflag_t (0000100) - IGNCR = tcflag_t (0000200) - ICRNL = tcflag_t (0000400) - INPCK = tcflag_t (0000020) - ISTRIP = tcflag_t (0000040) - IXON = tcflag_t (0002000) - OPOST = tcflag_t (0000001) - CS8 = tcflag_t (0000060) - ECHO = tcflag_t (0000010) - ICANON = tcflag_t (0000002) - IEXTEN = tcflag_t (0100000) - ISIG = tcflag_t (0000001) - VTIME = tcflag_t (5) - VMIN = tcflag_t (6) - CBAUD = tcflag_t (0010017) - CBAUDEX = tcflag_t (0010000) + syscall.IGNPAR = tcflag_t (0000004) + syscall.PARMRK = tcflag_t (0000010) + syscall.INLCR = tcflag_t (0000100) + syscall.ECHONL = tcflag_t (0000100) + syscall.IGNCR = tcflag_t (0000200) + syscall.ICRNL = tcflag_t (0000400) + syscall.INPCK = tcflag_t (0000020) + syscall.ISTRIP = tcflag_t (0000040) + syscall.IXON = tcflag_t (0002000) + syscall.OPOST = tcflag_t (0000001) + syscall.CS8 = tcflag_t (0000060) + syscall.ECHO = tcflag_t (0000010) + syscall.ICANON = tcflag_t (0000002) + syscall.IEXTEN = tcflag_t (0100000) + syscall.ISIG = tcflag_t (0000001) + syscall.VTIME = tcflag_t (5) + syscall.VMIN = tcflag_t (6) + syscall.CBAUD = tcflag_t (0010017) + syscall..CBAUDEX = tcflag_t (0010000) ) const ( @@ -90,7 +90,7 @@ const ( ) func getTermios(ttyfd uintptr, dst *termios) error { - r1, _, errno := syscall.Syscall (syscall.SYS_IOCTL, + r1, _, errno := syscall. (syscall.SYS_IOCTL, uintptr (ttyfd), uintptr (TCGETS), uintptr (unsafe.Pointer (dst))); @@ -105,7 +105,7 @@ func getTermios(ttyfd uintptr, dst *termios) error { } func setTermios(ttyfd uintptr, src *termios) error { - r1, _, errno := syscall.Syscall (syscall.SYS_IOCTL, + r1, _, errno := syscall. (syscall.SYS_IOCTL, uintptr (ttyfd), uintptr (TCSETS), uintptr (unsafe.Pointer (src))); @@ -123,13 +123,13 @@ func SetRawFd(fd uintptr) (error) { var orig_termios termios; if err := getTermios (fd, &orig_termios); err != nil { return err} - orig_termios.c_iflag &= ^(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON); - orig_termios.c_oflag &= ^(OPOST); - orig_termios.c_lflag &= ^(ECHO | ECHONL | ICANON | IEXTEN | ISIG); - orig_termios.c_cflag |= (CS8); + orig_termios.c_iflag &= ^(syscall.IGNBRK|syscall.BRKINT|syscall.PARMRK|syscall.ISTRIP|syscall.INLCR|syscall.IGNCR|syscall.ICRNL|syscall.IXON); + orig_termios.c_oflag &= ^(syscall.OPOST); + orig_termios.c_lflag &= ^(syscall.ECHO | syscall.ECHONL | syscall.ICANON | syscall.IEXTEN | syscall.ISIG); + orig_termios.c_cflag |= (syscall.CS8); - orig_termios.c_cc[VMIN] = 1; - orig_termios.c_cc[VTIME] = 0; + orig_termios.c_cc[syscall.VMIN] = 1; + orig_termios.c_cc[syscall.VTIME] = 0; return setTermios(fd, &orig_termios) } @@ -145,7 +145,7 @@ func SetSpeedFd(fd uintptr, speed speed_t) (err error) { orig_termios.c_ispeed = speed orig_termios.c_ospeed = speed //~ //input baudrate == output baudrate and we ignore special case B0 - //~ orig_termios.c_cflag &= ^(CBAUD | CBAUDEX) + //~ orig_termios.c_cflag &= ^(syscall.CBAUD | syscall..CBAUDEX) //~ orig_termios.c_cflag |= speed if err = setTermios(fd, &orig_termios); err != nil { return } if err = getTermios (fd, &orig_termios); err != nil { return } @@ -153,7 +153,7 @@ func SetSpeedFd(fd uintptr, speed speed_t) (err error) { err = errors.New("Failed to set speed") } //~ if err = getTermios (fd, &orig_termios); err != nil { return } - //~ if orig_termios.c_cflag & (CBAUD | CBAUDEX) != speed { + //~ if orig_termios.c_cflag & (syscall.CBAUD | syscall.CBAUDEX) != speed { //~ err = errors.New("Failed to set speed") //~ } return diff --git a/go/uc_sensor_node_zmq/main.go b/go/uc_sensor_node_zmq/main.go index 2c7e0ae..fa917e8 100644 --- a/go/uc_sensor_node_zmq/main.go +++ b/go/uc_sensor_node_zmq/main.go @@ -3,70 +3,91 @@ package main import ( - "flag" - "time" - zmq "github.com/vaughan0/go-zmq" -) + "flag" + "time" + zmq "github.com/vaughan0/go-zmq" +) // ---------- Main Code ------------- var ( - tty_dev_ string - pub_addr string - use_syslog_ bool - enable_debug_ bool - serial_speed_ uint + tty_dev_ string + pub_addr string + use_syslog_ bool + enable_debug_ bool + serial_speed_ uint ) 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() + 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() } -func ConnectSerialToZMQ(pub_sock *zmq.Socket) { - defer func() { - if x:= recover(); x != nil { Syslog_.Println(x) } - }() +func ConnectSerialToZMQ(pub_sock *zmq.Socket, timeout time.Duration) { + defer func() { + if x := recover(); x != nil { + Syslog_.Println(x) + } + }() + + serial_wr, serial_rd, err := OpenAndHandleSerial(tty_dev_, serial_speed_) + if err != nil { + panic(err) + } + defer close(serial_wr) - serial_wr, serial_rd, err := OpenAndHandleSerial(tty_dev_, serial_speed_) - if err != nil { panic(err) } - defer close(serial_wr) + t := time.NewTimer(timeout) + for { + select { + case incoming_ser_line, seropen := <-serial_rd: + if !seropen { + return + } + t.Reset(timeout) + Syslog_.Printf("%s", incoming_ser_line) + if err := pub_sock.Send(incoming_ser_line); err != nil { + Syslog_.Println(err.Error()) + } - for incoming_ser_line := range(serial_rd) { - Syslog_.Printf("%s",incoming_ser_line) - if err := pub_sock.Send(incoming_ser_line); err != nil { Syslog_.Println(err.Error()) } - } + case <-t.C: + Syslog_.Print("Timeout, no message for 120 seconds") + } + } } func main() { - zmqctx, pub_sock := ZmqsInit(pub_addr) - if pub_sock == nil { panic("zmq socket creation failed") } - defer zmqctx.Close() - defer pub_sock.Close() + zmqctx, pub_sock := ZmqsInit(pub_addr) + if pub_sock == nil { + panic("zmq socket creation failed") + } + defer zmqctx.Close() + defer pub_sock.Close() - if enable_debug_ { - LogEnableDebuglog() - } else if use_syslog_ { - LogEnableSyslog() - Syslog_.Print("started") - } + if enable_debug_ { + LogEnableDebuglog() + } else if use_syslog_ { + LogEnableSyslog() + Syslog_.Print("started") + } - var backoff_exp uint32 = 0 - for { - start_time := time.Now().Unix() - ConnectSerialToZMQ(pub_sock) - run_time := time.Now().Unix() - start_time - if run_time > exponential_backof_activation_threshold { - backoff_exp = 0 - } - time.Sleep(150*(1 << backoff_exp) * time.Millisecond) - if backoff_exp < 12 { backoff_exp++ } - } + var backoff_exp uint32 = 0 + for { + start_time := time.Now().Unix() + ConnectSerialToZMQ(pub_sock, time.Second*120) + run_time := time.Now().Unix() - start_time + if run_time > exponential_backof_activation_threshold { + backoff_exp = 0 + } + time.Sleep(150 * (1 << backoff_exp) * time.Millisecond) + if backoff_exp < 12 { + backoff_exp++ + } + } }