栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在C中从串行端口打开,读取和写入?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在C中从串行端口打开,读取和写入?

您必须调用

cfmakeraw
一个
tty
从获得
tcgetattr
。你不能零了
structtermios
,配置它,然后将
tty
tcsetattr
。如果使用归零方法,则会遇到无法解释的间歇性故障,尤其是在BSD和OS
X上。“无法解释的间歇性故障”包括挂起
read(3)

#include <errno.h>#include <fcntl.h> #include <string.h>#include <termios.h>#include <unistd.h>intset_interface_attribs (int fd, int speed, int parity){        struct termios tty;        if (tcgetattr (fd, &tty) != 0)        {     error_message ("error %d from tcgetattr", errno);     return -1;        }        cfsetospeed (&tty, speed);        cfsetispeed (&tty, speed);        tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;     // 8-bit chars        // disable IGNBRK for mismatched speed tests; otherwise receive break        // as 00 chars        tty.c_iflag &= ~IGNBRK;         // disable break processing        tty.c_lflag = 0;     // no signaling chars, no echo,       // no canonical processing        tty.c_oflag = 0;     // no remapping, no delays        tty.c_cc[VMIN]  = 0; // read doesn't block        tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout        tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl        tty.c_cflag |= (CLOCAL | CREAD);// ignore modem controls,       // enable reading        tty.c_cflag &= ~(PARENB | PARODD);      // shut off parity        tty.c_cflag |= parity;        tty.c_cflag &= ~CSTOPB;        tty.c_cflag &= ~CRTSCTS;        if (tcsetattr (fd, TCSANOW, &tty) != 0)        {     error_message ("error %d from tcsetattr", errno);     return -1;        }        return 0;}voidset_blocking (int fd, int should_block){        struct termios tty;        memset (&tty, 0, sizeof tty);        if (tcgetattr (fd, &tty) != 0)        {     error_message ("error %d from tggetattr", errno);     return;        }        tty.c_cc[VMIN]  = should_block ? 1 : 0;        tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout        if (tcsetattr (fd, TCSANOW, &tty) != 0)     error_message ("error %d setting term attributes", errno);}...char *portname = "/dev/ttyUSB1" ...int fd = open (portname, O_RDWR | O_NOCTTY | O_SYNC);if (fd < 0){        error_message ("error %d opening %s: %s", errno, portname, strerror (errno));        return;}set_interface_attribs (fd, B115200, 0);  // set speed to 115,200 bps, 8n1 (no parity)set_blocking (fd, 0);     // set no blockingwrite (fd, "hello!n", 7);// send 7 character greetingusleep ((7 + 25) * 100);  // sleep enough to transmit the 7 plus    // receive 25:  approx 100 uS per char transmitchar buf [100];int n = read (fd, buf, sizeof buf);  // read up to 100 characters if ready to read

对速度的值是

B115200
B230400
B9600
B19200
B38400
B57600
B1200
B2400
B4800
,等。用于奇偶校验的值
0
(意味着没有奇偶校验),
PARENB|PARODD
(启用奇偶并使用奇数),
PARENB
(启用奇偶校验并使用偶数),
PARENB|PARODD|CMSPAR
(标记奇偶校验),和
PARENB|CMSPAR
(空间平价)。

“阻止”设置

read()
端口上的a是否等待指定数量的字符到达。设置 无阻塞
意味着无论
read()
返回多少字符都可用,而无需等待更多字符,直到缓冲区限制。


附录:

CMSPAR
仅在选择标记和空间奇偶校验时才需要,这是罕见的。对于大多数应用程序,可以将其省略。我的头文件仅在
/usr/include/bits/termios.h
定义
CMSPAR
了预处理器符号的
__USE_MISC
情况下才启用定义。该定义发生时(在
features.h
)与

#if defined _BSD_SOURCE || defined _SVID_SOURCE #define __USE_MISC     1#endif

的介绍性评论

<features.h>
说:



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/371342.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号