func (p *port) flush(q flushSel) error {
	var qsel int
	switch q {
	case flushIn:
		qsel = termios.TCIFLUSH
	case flushOut:
		qsel = termios.TCOFLUSH
	case flushInOut:
		qsel = termios.TCIOFLUSH
	default:
		return newErr("invalid flush selector")
	}

	if err := p.fd.Lock(); err != nil {
		return ErrClosed
	}
	defer p.fd.Unlock()
	err := termios.Flush(p.fd.Sysfd(), qsel)
	if err != nil {
		return newErr("tcflush: " + err.Error())
	}
	return nil
}
Example #2
0
func TestMisc(t *testing.T) {
	if dev == "" {
		t.Skip("No TEST_SERIAL_DEV variable set.")
	}
	f, err := os.OpenFile(dev, os.O_RDWR, 0)
	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)
	}
	/* Set low baudrate */
	baudrate := 1200
	ti.SetOSpeed(baudrate)
	ti.SetISpeed(baudrate)
	/* Disable flow control */
	ti.CFlag().Clr(termios.CRTSCTS)
	ti.IFlag().Clr(termios.IXON | termios.IXOFF | termios.IXANY)
	err = ti.SetFd(int(f.Fd()), termios.TCSANOW)
	if err != nil {
		t.Fatal("Cannot set termios:", err)
	}

	/* Try to test Drain */
	b := make([]byte, 600)
	start := time.Now()
	if _, err := f.Write(b); err != nil {
		t.Fatal("Cannot write:", err)
	}
	err = termios.Drain(int(f.Fd()))
	if err != nil {
		t.Fatal("Cannot drain:", err)
	}
	dur := time.Since(start)
	charTime := 10 * time.Second / time.Duration(baudrate)
	chars := int(dur / charTime)
	// Allow some fuzz for h/w queues and stuff.
	if chars < len(b)-16 || chars > len(b)+16 {
		t.Logf("Invalid tx time %v (%d chars):", dur, chars)
	}

	/* Try to test SendBreak */
	start = time.Now()
	termios.SendBreak(int(f.Fd()))
	dur = time.Since(start)
	// POSIX says SendBreak should last between 0.25 and 1 Sec.
	if dur < 200*time.Millisecond || dur > 1100*time.Millisecond {
		t.Log("Bad SendBreak duration:", dur)
	}

	// Just call Flush
	if err := termios.Flush(int(f.Fd()), termios.TCIFLUSH); err != nil {
		t.Fatal("Flush In failed:", err)
	}
	if err := termios.Flush(int(f.Fd()), termios.TCOFLUSH); err != nil {
		t.Fatal("Flush Out failed:", err)
	}
	if err := termios.Flush(int(f.Fd()), termios.TCIOFLUSH); err != nil {
		t.Fatal("Flush InOut failed:", err)
	}
	// This should normally fail (depends on system tcflush()
	// implementation)
	if err := termios.Flush(int(f.Fd()), 4242); err == nil {
		t.Logf("Flush 4242 should fail!")
	}
}