无法让C读写串口

发布于 2024-12-11 13:48:16 字数 1265 浏览 0 评论 0原文

这是我的第一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

夜夜流光相皎洁 2024-12-18 13:48:16

首先,

if (buff=msg)

是赋值,而不是比较:)后者是==

其次,

if (buff == msg)

实际上是指针比较,而不是字符串比较。对于字符串比较,请参阅 C 标准库中的 strcmp()

第三,

char *buff;
...
rd=read(fd,buff,sizeof(msg));

buff 未初始化 - 没有为其分配内存,所以你很高兴它根本不会崩溃。

好吧,还有更多需要检查的地方,但上面列出的已经足以阻止程序正常运行。

建议:尝试将调试 printf 放在 read 行下方,以查看实际从端口读取的内容。请记住,从端口读取的数据不能保证以零结尾(请参阅以零结尾的字符串以供参考),因此您还必须注意这一点(或者在实际的数据后面添加一个零)数据,或以某种方式限制缓冲区上的字符串操作,例如使用 strncmp() 而不是 strcmp())。

First,

if (buff=msg)

is assignment, not comparison :) The latter is ==.

Second,

if (buff == msg)

is actually pointer comparison, not string comparison. For string comparison, see strcmp() from C standard library.

Third,

char *buff;
...
rd=read(fd,buff,sizeof(msg));

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 the read 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 (see zero-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 using strncmp() instead of strcmp()).

苍风燃霜 2024-12-18 13:48:16

我看到很多错误:

  • 您应该始终检查返回值是否有错误。如果您的 open_port 函数无法打开端口,则可能会返回 -1,但您仍继续主循环。
  • 您不检查读取的返回,它可能是-1,更可能是因为您在文件上设置了NDELAY。
  • 您没有初始化 buf,它指向您不知道的某个地方,因为它没有初始化。您可能想使用 char buffer[1024] 或类似的东西。
  • 在 C 语言中,比较两个字符串是使用 strcmp 完成的,内存使用 memcmp 完成的。您正在比较两个指针,而不是它们的内容。

我认为这足以让您开始修复代码:-)

I see many errors:

  • You should always check the return value for errors. Your open_port function may return -1 if it could not open the port, yet you continue in the main loop.
  • You do not check the return from read, it may be -1, more likely so since you set NDELAY on the file.
  • You do not initialize buf, it points to somewhere you don't know because it is not initialized. You might want to use char buffer[1024] or something like this.
  • In C, comparing two strings is done using strcmp, memory using memcmp. You're comparing two pointers, not their contents.

I think this is enough for you to start fixing the code :-)

ι不睡觉的鱼゛ 2024-12-18 13:48:16

此代码将不起作用:

while (buff != msg);

buff 和 msg 是指针。您不能使用 == 或 != 来比较字符串。你必须使用 strcmp()

This code will not work:

while (buff != msg);

buff and msg are pointers. You can't compare the strings using == or !=. You have to use strcmp()

往事风中埋 2024-12-18 13:48:16

buff 指向哪里?它没有初始化。

另外,请记住,在 C 中,您不能使用 == 来比较字符串,否则您将比较字符串的地址。也没有=,那就是赋值。如果末尾没有 NUL 字符并且长度已知,则要比较字符串,请使用 strcmpmemcmp

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 use strcmp or memcmp if there is no NUL chars at the end and the lengths are known.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文