本文整理汇总了Golang中C.tcgetattr函数的典型用法代码示例。如果您正苦于以下问题:Golang tcgetattr函数的具体用法?Golang tcgetattr怎么用?Golang tcgetattr使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了tcgetattr函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: termioInit
func (t *tScreen) termioInit() error {
var e error
var rv C.int
var newtios C.struct_termios
var fd C.int
if t.in, e = os.OpenFile("/dev/tty", os.O_RDONLY, 0); e != nil {
goto failed
}
if t.out, e = os.OpenFile("/dev/tty", os.O_WRONLY, 0); e != nil {
goto failed
}
t.tiosp = &termiosPrivate{}
fd = C.int(t.out.Fd())
if rv, e = C.tcgetattr(fd, &t.tiosp.tios); rv != 0 {
goto failed
}
t.baud = int(C.getbaud(&t.tiosp.tios))
newtios = t.tiosp.tios
newtios.c_iflag &^= C.IGNBRK | C.BRKINT | C.PARMRK |
C.ISTRIP | C.INLCR | C.IGNCR |
C.ICRNL | C.IXON
newtios.c_oflag &^= C.OPOST
newtios.c_lflag &^= C.ECHO | C.ECHONL | C.ICANON |
C.ISIG | C.IEXTEN
newtios.c_cflag &^= C.CSIZE | C.PARENB
newtios.c_cflag |= C.CS8
// We wake up at the earliest of 100 msec or when data is received.
// We need to wake up frequently to permit us to exit cleanly and
// close file descriptors on systems like Darwin, where close does
// cause a wakeup. (Probably we could reasonably increase this to
// something like 1 sec or 500 msec.)
newtios.c_cc[C.VMIN] = 0
newtios.c_cc[C.VTIME] = 1
if rv, e = C.tcsetattr(fd, C.TCSANOW|C.TCSAFLUSH, &newtios); rv != 0 {
goto failed
}
signal.Notify(t.sigwinch, syscall.SIGWINCH)
if w, h, e := t.getWinSize(); e == nil && w != 0 && h != 0 {
t.w = w
t.h = h
}
return nil
failed:
if t.in != nil {
t.in.Close()
}
if t.out != nil {
t.out.Close()
}
return e
}
示例2: openSerial
/**
* Open the serial port setting the baud etc.
*/
func openSerial() (io.ReadWriteCloser, error) {
file, err :=
os.OpenFile(
"/dev/ttyUSB0",
syscall.O_RDWR|syscall.O_NOCTTY,
0600)
if err != nil {
return nil, err
}
fd := C.int(file.Fd())
if C.isatty(fd) == 0 {
err := errors.New("File is not a serial port")
return nil, err
}
var termios C.struct_termios
_, err = C.tcgetattr(fd, &termios)
if err != nil {
return nil, err
}
var baud C.speed_t
baud = C.B115200
_, err = C.cfsetispeed(&termios, baud)
if err != nil {
return nil, err
}
_, err = C.cfsetospeed(&termios, baud)
if err != nil {
return nil, err
}
return file, nil
}
示例3: setBaudRate
func (c *Connection) setBaudRate(baud Baud) error {
fd, err := c.getFileDescriptor()
if err != nil {
return err
}
var st C.struct_termios
_, err = C.tcgetattr(fd, &st)
if err != nil {
return err
}
speed, err := convertBaud(c.Baud)
if err != nil {
return err
}
_, err = C.cfsetispeed(&st, speed)
if err != nil {
return err
}
_, err = C.cfsetospeed(&st, speed)
if err != nil {
return err
}
return nil
}
示例4: tcget
func tcget(fd uintptr, p *Termios) syscall.Errno {
ret, err := C.tcgetattr(C.int(fd), (*C.struct_termios)(unsafe.Pointer(p)))
if ret != 0 {
return err.(syscall.Errno)
}
return 0
}
示例5: getTermSettings
// Thin wrapper around tcgetattr.
func getTermSettings() (settings C.struct_termios) {
res, err := C.tcgetattr(C.int(os.Stderr.Fd()), &settings)
if res != 0 {
panic(fmt.Sprintf("tcgetattr returned %d: %v", res, err))
}
return
}
示例6: tcgetattr
// Gets terminal state.
//
// int tcgetattr(int fd, struct termios *termios_p);
func (tc *termios) tcgetattr() error {
exitCode, errno := C.tcgetattr(C.int(tc.fd), tc.wrap)
if exitCode == 0 {
return nil
}
return errno
}
示例7: GetAttributes
// Gets a terminal's attributes. Akin to tcgetattr().
func (t *Terminal) GetAttributes() (*Attributes, os.Error) {
var cattr C.struct_termios
result := int(C.tcgetattr(C.int(t.Fd()), &cattr))
if result < 0 {
return nil, os.NewError("Unable to get terminal attributes.")
}
return makeGoAttributes(&cattr), nil
}
示例8: GetAttr
// Wrapper of tcgetattr(3).
// The GetAttr() function copies the parameters associated with the terminal.
func (t *Termios) GetAttr(fd int) error {
var cTerm C.struct_termios
if C.tcgetattr(C.int(fd), &cTerm) == -1 {
return errors.New("tcgetattr failure")
}
*t = *goTermios(&cTerm)
return nil
}
示例9: getattr
func (bp *baseport) getattr() (*C.struct_termios, error) {
var tio C.struct_termios
res, err := C.tcgetattr(C.int(bp.f.Fd()), (*C.struct_termios)(unsafe.Pointer(&tio)))
if res != 0 || err != nil {
return nil, err
}
return &tio, nil
}
示例10: open
func (c *Connection) open() (err error) {
c.file, err = os.OpenFile(c.Name, syscall.O_RDWR|syscall.O_NOCTTY|syscall.O_NONBLOCK, 0666)
if err != nil {
return err
}
fd, err := c.getFileDescriptor()
if err != nil {
c.file.Close()
return err
}
var st C.struct_termios
_, err = C.tcgetattr(fd, &st)
if err != nil {
c.file.Close()
return err
}
// set the baud rate of the connection
err = c.setBaudRate(c.Baud)
if err != nil {
c.file.Close()
return err
}
// No timeout set directly on termios
st.c_cc[C.VMIN] = 0
st.c_cc[C.VTIME] = 0
// Select local mode
st.c_cflag |= (C.CLOCAL | C.CREAD)
// Select raw mode
st.c_lflag &= ^C.tcflag_t(C.ICANON | C.ECHO | C.ECHOE | C.ISIG)
st.c_oflag &= ^C.tcflag_t(C.OPOST)
_, err = C.tcsetattr(fd, C.TCSANOW, &st)
if err != nil {
c.file.Close()
return err
}
r1, _, e := syscall.Syscall(syscall.SYS_FCNTL,
uintptr(c.file.Fd()),
uintptr(syscall.F_SETFL),
uintptr(0))
if e != 0 || r1 != 0 {
s := fmt.Sprint("Clearing NONBLOCK syscall error:", e, r1)
c.file.Close()
return errors.New(s)
}
return nil
}
示例11: Getattr
// Getattr copies the parameters associated with the terminal
// referenced by fd in the Termios structure referenced by dst.
func Getattr(fd uintptr, dst *Termios) error {
_, rv := C.tcgetattr(C.int(fd), &dst.i)
if rv == nil {
dst.C_iflag = dst.i.c_iflag
dst.C_oflag = dst.i.c_oflag
dst.C_cflag = dst.i.c_cflag
dst.C_lflag = dst.i.c_lflag
dst.C_cc = dst.i.c_cc
}
return rv
}
示例12: GetState
func GetState(fd int) (*State, error) {
termios := syscall.Termios{}
ret, err := C.tcgetattr(C.int(fd), (*C.struct_termios)(unsafe.Pointer(&termios)))
if ret != 0 {
return nil, err.(syscall.Errno)
}
state := State{}
state.Termios = termios
return &state, nil
}
示例13: GetFd
// GetFd reads the attributes of the terminal corresponding to the
// file-descriptor fd and stores them in the Termios structure t. See
// tcgetattr(3) for more.
func (t *Termios) GetFd(fd int) error {
for {
r, err := C.tcgetattr(C.int(fd), &t.t)
if r < 0 {
// This is most-likely not possible, but
// better be safe.
if err == syscall.EINTR {
continue
}
return err
}
return nil
}
}
示例14: ReadPassphrase
func ReadPassphrase() string {
stdin := C.int(os.Stdin.Fd())
C.tcgetattr(stdin, &C.AttrOld)
C.Attr = C.AttrOld
C.Attr.c_lflag = C.Attr.c_lflag&^C.ECHO | C.ECHONL
C.tcsetattr(stdin, C.TCSANOW, &C.Attr)
defer C.tcsetattr(stdin, C.TCSANOW, &C.AttrOld)
reader := bufio.NewReader(os.Stdin)
line, err := reader.ReadString('\n')
if err != nil {
return ""
}
return strings.TrimSpace(line)
}
示例15: SetInputAttr
// SetInputAttr sets VMIN and VTIME for control serial reads.
//
// In non-canonical input processing mode, input is not assembled into
// lines and input processing (erase, kill, delete, etc.) does not
// occur. Two parameters control the behavior of this mode:
// c_cc[VTIME] sets the character timer, and c_cc[VMIN] sets the
// minimum number of characters to receive before satisfying the read.
//
// If MIN > 0 and TIME = 0, MIN sets the number of characters to
//receive before the read is satisfied. As TIME is zero, the timer is
//not used.
//
// If MIN = 0 and TIME > 0, TIME serves as a timeout value. The read
//will be satisfied if a single character is read, or TIME is exceeded
//(t = TIME *0.1 s). If TIME is exceeded, no character will be
//returned.
//
// If MIN > 0 and TIME > 0, TIME serves as an inter-character
//timer. The read will be satisfied if MIN characters are received, or
//the time between two characters exceeds TIME. The timer is restarted
//every time a character is received and only becomes active after the
//first character has been received.
//
// If MIN = 0 and TIME = 0, read will be satisfied immediately. The
//number of characters currently available, or the number of
//characters requested will be returned. According to Antonino (see
//contributions), you could issue a fcntl(fd, F_SETFL, FNDELAY);
//before reading to get the same result.
//
// By modifying newtio.c_cc[VTIME] and newtio.c_cc[VMIN] all modes
//described above can be tested.
//
// -- copied from http://tldp.org/HOWTO/Serial-Programming-HOWTO/x115.html
func (port *SerialPort) SetInputAttr(minBytes int, timeout time.Duration) error {
fd := port.port.Fd()
var options C.struct_termios
if C.tcgetattr(C.int(fd), &options) < 0 {
return fmt.Errorf("tcgetattr failed")
}
options.c_cc[C.VMIN] = _Ctype_cc_t(minBytes)
options.c_cc[C.VTIME] = _Ctype_cc_t(timeout / (time.Second / 10))
if C.tcsetattr(C.int(fd), C.TCSANOW, &options) < 0 {
return fmt.Errorf("tcsetattr failed")
}
return nil
}