【发布时间】:2011-10-24 22:37:01
【问题描述】:
这是我的第一个 C 程序。你好世界!我敢肯定这对现在的高中程序员来说是没有问题的,但是当我在高中的时候他们没有编程。 :)
我想写入串行端口,直到我写入的字符串回显给我。然后做其他事情。我下面的代码运行了几秒钟,然后声称看到了字符串并结束,即使它实际上看不到字符串。无论如何它的行为都是一样的,我显然有什么非常不对的地方。
是的,串行设备 /dev/kittens 是真实的,当端口循环时,从终端接收(回显)到 /dev/kittens 的 bash 回显字符串。
如果有人能纠正我的错误,我将不胜感激。
#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 the port */
wr = write(fd, msg, sizeof(msg));
printf("debugging - Wrote to port\n");
usleep(10000);
if (wr < 0) {
fputs("write() to port /dev/kittens failed!\n", stderr);
break;
}
} while (buff != msg);
if (buff=msg)
printf(buff, "String Found! Now do the work.");
/*
system("dostuff.sh);
*/
/* Close the port on exit. */
close(fd);
return 0;
}
【问题讨论】:
-
您对
open()的调用使用/dev/kitens。是不是打错字了? -
是的。这是我帖子中的错字。谢谢。
标签: c linux serial-port