本文整理匯總了Golang中github.com/npat-efault/serial/termios.Termios.GetOSpeed方法的典型用法代碼示例。如果您正苦於以下問題:Golang Termios.GetOSpeed方法的具體用法?Golang Termios.GetOSpeed怎麽用?Golang Termios.GetOSpeed使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/npat-efault/serial/termios.Termios
的用法示例。
在下文中一共展示了Termios.GetOSpeed方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestSetGet
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)
}
}
示例2: TestSpeed
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)
}
}
}
示例3: getConf
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
}