无法让C读写串口
这是我的第一个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;
}
This is my first C program. hello world! I'm sure this is no problem for high school programmers these days, but they didn't have programming when I was in high school. :)
I want to write to a serial port until the string I write is echoed back to me. Then do other stuff. My code below runs for a few seconds and then claims to see the string and ends, even when it could not have actually seen the string. It behaves the same no matter what so, I obviously have something very wrong.
Yes, the serial device /dev/kittens is real and, from a terminal, bash echoed strings to /dev/kittens are received(echoed) on the serial port when the port is looped.
I would be most appreciative to anyone who could correct my mistakes.
#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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先,
是赋值,而不是比较:)后者是
==
。其次,
实际上是指针比较,而不是字符串比较。对于字符串比较,请参阅 C 标准库中的
strcmp()
。第三,
buff
未初始化 - 没有为其分配内存,所以你很高兴它根本不会崩溃。好吧,还有更多需要检查的地方,但上面列出的已经足以阻止程序正常运行。
建议:尝试将调试
printf
放在read
行下方,以查看实际从端口读取的内容。请记住,从端口读取的数据不能保证以零结尾(请参阅以零结尾的字符串
以供参考),因此您还必须注意这一点(或者在实际的数据后面添加一个零)数据,或以某种方式限制缓冲区上的字符串操作,例如使用strncmp()
而不是strcmp()
)。First,
is assignment, not comparison :) The latter is
==
.Second,
is actually pointer comparison, not string comparison. For string comparison, see
strcmp()
from C standard library.Third,
buff
is left uninitialized - there's no memory allocated for it, so you're happy enough it doesn't crash at all.Well, there is more to check, but listed above is already enough to prevent the program from functioning correctly.
As an advice: try to put a debugging
printf
below theread
line to see what is actually read from the port. And remember, the data read from the port is not guaranteed to be zero-terminated (seezero-terminated strings
for reference), so you have to watch for this also (either add a zero after the actual data, or somehow limit string operations on the buffer, like usingstrncmp()
instead ofstrcmp()
).我看到很多错误:
open_port
函数无法打开端口,则可能会返回-1
,但您仍继续主循环。strcmp
完成的,内存使用memcmp
完成的。您正在比较两个指针,而不是它们的内容。我认为这足以让您开始修复代码:-)
I see many errors:
open_port
function may return-1
if it could not open the port, yet you continue in the main loop.char buffer[1024]
or something like this.strcmp
, memory usingmemcmp
. You're comparing two pointers, not their contents.I think this is enough for you to start fixing the code :-)
此代码将不起作用:
buff 和 msg 是指针。您不能使用 == 或 != 来比较字符串。你必须使用 strcmp()
This code will not work:
buff and msg are pointers. You can't compare the strings using == or !=. You have to use strcmp()
buff
指向哪里?它没有初始化。另外,请记住,在 C 中,您不能使用
==
来比较字符串,否则您将比较字符串的地址。也没有=
,那就是赋值。如果末尾没有 NUL 字符并且长度已知,则要比较字符串,请使用strcmp
或memcmp
。Where dos
buff
point to? it is not initialized.Also, remember that in C you cannot compare strings using
==
, or you would be comparing addresses of strings. And no=
either, that is assignment. To compare strings usestrcmp
ormemcmp
if there is no NUL chars at the end and the lengths are known.