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)
"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) {
}
}
-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
}
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
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()
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)
"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) {
}
}
-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
}