串口设备:读8N1的作品,但是写一个字节失败 [英] Serial Device: Reading 8N1 works, but writing a single byte fails

查看:209
本文介绍了串口设备:读8N1的作品,但是写一个字节失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的节目,我从串行设备(Linux操作系统,8N1)没有任何问题读取。但在情况下,我想写出一个字节,我得到什么了接口。我以为我的串行输出的设置是错误的。但是有没有那么多的方式如何设置c_oflag定义...

In my program I read from the serial device (Linux, 8N1) without any problem. But in the case I want to write out a single byte, I get nothing out on the interface. I assume that my serial output settings are wrong. But there aren't that many ways how to set c_oflag...

我的code:

#define TTYDEVICE "/dev/ttyS0"
#define BAUDRATE B9600

int openSerialDevice(const char* devTTY, struct termios oldTio) {
//----< Open serial device >----------------------------------
int fileDescriptor;
// fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
fileDescriptor = open(devTTY, O_RDWR | O_NOCTTY);
//fileDescriptor = open(devTTY, O_RDWR | O_NOCTTY /*| OPOST*/);
if (fileDescriptor == -1) {
    perror("Error while opening serial interface occurred!");
    return -99;
}

// set new parameters to the serial device
struct termios newtio;
bzero(&newtio, sizeof(newtio));
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;

// set to 8N1
newtio.c_cflag &= ~PARENB;
newtio.c_cflag &= ~CSTOPB;
newtio.c_cflag &= ~CSIZE;
newtio.c_cflag |= CS8;

newtio.c_iflag = IGNPAR;

// output mode to
//newtio.c_oflag = 0;
newtio.c_oflag |= OPOST;

/* set input mode (non-canonical, no echo,...) */
newtio.c_lflag = 0;

newtio.c_cc[VTIME] = 10; /* inter-character timer 1 sec */
newtio.c_cc[VMIN] = 0; /* blocking read disabled  */

tcflush(fileDescriptor, TCIFLUSH);
if (tcsetattr(fileDescriptor, TCSANOW, &newtio)) {
    perror("could not set the serial settings!");
    return -99;
}

//----< / Open serial device >----------------------------------
return fileDescriptor;
}

int ACK[1] = { 6 };

int main() {
// old termios to restablish
struct termios oldTIO;
// filedescriptor
int fd;

fd = openSerialDevice(TTYDEVICE, oldTIO);

if ((fd == -1) | (fd == -99)) {
    perror("Could not open TTY-Device. Exit on failure!");
    return EXIT_FAILURE;
}

write(fd, ACK, 1);  // Problem !! 


     return 0:
}

现在,如果我用

屏幕上的/ dev / 9600就是ttyS1 8N1

screen /dev/ttyS1 9600 8n1

要验证什么现身在/ dev / ttyS0来。我什么都看不到。同样的,如果我用嗅探1.8 Docklight。

to verify what's coming out on /dev/ttyS0. I can't see anything. Same if I sniff with Docklight 1.8.

有什么建议?谢谢

推荐答案

如何验证什么都不出来?

How do you verify nothing is coming out ?

您可以尝试删除该RTSCTS,然后再试一次。逸岸,如果你想从tty层最小的干扰,您应将终端设置为原料,采用这样的:

You can try to drop the RTSCTS, and try again. Infact, if you want minimal interference from the tty layer, you should set your terminal to raw, using this :

cfmakeraw(&newtio);

这篇关于串口设备:读8N1的作品,但是写一个字节失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆