func TestSetGet(t *testing.T) { if dev == "" { t.Skip("No TEST_SERIAL_DEV variable set.") } f, err := os.Open(dev) if err != nil { t.Fatal("Open:", err) } defer f.Close() var ti termios.Termios err = ti.GetFd(int(f.Fd())) if err != nil { t.Fatal("Cannot get termios:", err) } err = ti.SetOSpeed(19200) if err != nil { t.Fatal("Cannot set O speed to 19200:", err) } ti.SetISpeed(19200) if err != nil { t.Fatal("Cannot set I speed to 19200:", err) } err = ti.SetFd(int(f.Fd()), termios.TCSAFLUSH) if err != nil { t.Fatal("Cannot set termios:", err) } ti = termios.Termios{} err = ti.GetFd(int(f.Fd())) if err != nil { t.Fatal("Cannot get termios:", err) } spd, err := ti.GetOSpeed() if err != nil { t.Fatal("Cannot get O speed:", err) } if spd != 19200 { t.Fatalf("Bad O speed: %d != %d", spd, 19200) } spd, err = ti.GetISpeed() if err != nil { t.Fatal("Cannot get I speed:", err) } if spd != 19200 && spd != 0 { t.Fatalf("Bad I speed: %d != %d", spd, 19200) } }
func TestSpeed(t *testing.T) { ti := termios.Termios{} spds_ok := []int{1200, 4800, 9600, 19200, 38400} spds_fail := []int{-1, -2, -3} for _, s := range spds_ok { // Output Speeds if err := ti.SetOSpeed(s); err != nil { t.Fatalf("Cannot set out speed %d: %v", s, err) } s1, err := ti.GetOSpeed() if err != nil { t.Fatalf("Cannot get out speed %d: %v", s, err) } if s1 != s { t.Fatalf("Out speed: %d != %d", s1, s) } // Input Speeds if err := ti.SetISpeed(s); err != nil { t.Fatalf("Cannot set in speed %d: %v", s, err) } s1, err = ti.GetISpeed() if err != nil { t.Fatalf("Cannot get in speed %d: %v", s, err) } if s1 != s { t.Fatalf("In speed: %d != %d", s1, s) } } for _, s := range spds_fail { if err := ti.SetOSpeed(s); err != syscall.EINVAL { t.Fatalf("Set bad out speed %d", s) } if err := ti.SetISpeed(s); err != syscall.EINVAL { t.Fatalf("Set bad in speed %d", s) } } }
func (p *port) getConf() (conf Conf, err error) { var tios termios.Termios var noReset bool if err = p.fd.Lock(); err != nil { return conf, ErrClosed } err = tios.GetFd(p.fd.Sysfd()) noReset = p.noReset p.fd.Unlock() if err != nil { return conf, newErr("tcgetattr: " + err.Error()) } // Baudrate conf.Baudrate, err = tios.GetOSpeed() if err != nil { return conf, newErr("getospeed: " + err.Error()) } // Databits switch tios.CFlag().Msk(termios.CSIZE) { case termios.CS5: conf.Databits = 5 case termios.CS6: conf.Databits = 6 case termios.CS7: conf.Databits = 7 case termios.CS8: conf.Databits = 8 default: return conf, newErr("cannot decode databits") } // Stopbits if tios.CFlag().Any(termios.CSTOPB) { conf.Stopbits = 2 } else { conf.Stopbits = 1 } // Parity if !tios.CFlag().Any(termios.PARENB) { conf.Parity = ParityNone } else if tios.CFlag().Any(termios.CMSPAR) { if tios.CFlag().Any(termios.PARODD) { conf.Parity = ParityMark } else { conf.Parity = ParitySpace } } else { if tios.CFlag().Any(termios.PARODD) { conf.Parity = ParityOdd } else { conf.Parity = ParityEven } } // Flow rtscts := tios.CFlag().Any(termios.CRTSCTS) xoff := tios.IFlag().Any(termios.IXOFF) xon := tios.IFlag().Any(termios.IXON | termios.IXANY) if rtscts && !xoff && !xon { conf.Flow = FlowRTSCTS } else if !rtscts && xoff && xon { conf.Flow = FlowXONXOFF } else if !rtscts && !xoff && !xon { conf.Flow = FlowNone } else { conf.Flow = FlowOther } // NoReset conf.NoReset = noReset return conf, nil }