这是我的第一个C程序.你好,世界!我相信这对于高中程序员来说这不是问题,但是当我在高中时他们没有编程.:)
我想写一个串口,直到我写的字符串回显给我.然后做其他的事情.我的下面的代码运行了几秒钟,然后声称看到字符串并结束,即使它实际上没有看到字符串.它无论如何都表现得一样,我显然有一些非常错误.
是的,串行设备/ dev/kittens是真实的,当端口循环时,从终端接收(回送)串行端口上的bash回显字符串到/ dev/kittens.
我会非常感谢能够纠正错误的人.
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
int fd;
char *buff;
int open_port(void)
{
fd = open("/dev/kitens", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
perror("open_port: Unable to open /dev/kittens ");
}
else
fcntl(fd, F_SETFL, 0);
return (fd);
}
int main()
{
int wr,rd;
open_port();
char msg[]="There are mice in the wire.\r";
do
{
/* Read from the port */
fcntl(fd, F_SETFL, FNDELAY);
rd=read(fd,buff,sizeof(msg));
/* Write to …Run Code Online (Sandbox Code Playgroud)