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) } } }