Cannot get C to write and read serial port

This is my first C program. Hello world! I'm sure this is not a problem for school programmers these days, but they did not have programming when I was in high school. :)

I want to write to the serial port until the line I'm writing returns to me. Then do other things. My code below works for a few seconds and then claims to see the line and ends even if it could not see the line. It behaves the same regardless of the fact that so, I am obviously something very wrong.

Yes, the serial device / dev / kittens is real and, from the terminal, bash echo lines for / dev / kittens are received (echo) on the serial port when the port is looped.

I would really appreciate 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; } 
+6
source share
4 answers

At first,

 if (buff=msg) 

is an assignment, not a comparison :) Last == .

Secondly,

 if (buff == msg) 

It is a comparison of pointers, not a comparison of strings. For string comparisons, see strcmp() in the C standard library.

Thirdly,

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

buff remains uninitialized - no memory is allocated for it, so you are happy that it does not crash at all.

Well, there is still something to check, but the ones listed above are already enough to prevent the program from functioning correctly.

As a tip: try debugging printf below the read line to see what is actually being read from the port. And remember that data read from the port is not guaranteed to be terminated with zero termination (see zero-terminated strings for reference), so you also need to keep an eye on this (either add zero after the actual data, or somehow restrict the operation of the line to the buffer for example using strncmp() instead of strcmp() ).

+8
source

I see a lot of errors:

  • You should always check the return value for errors. The open_port function may return -1 if it cannot open the port, but you continue in the main loop.
  • You do not check for a return from reading, it may be -1, more likely since you are setting NDELAY in the file.
  • You do not initialize buf, it indicates that you do not know, because it is not initialized. You can use char buffer[1024] or something like this.
  • In C, comparing two strings is done using strcmp , using memcmp . You are comparing two pointers, not their contents.

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

+3
source

This code will not work:

 while (buff != msg); 

buff and msg are pointers. You cannot compare strings using == or! =. You must use strcmp ()

0
source

Where does dos buff point to? it is not initialized.

Also, remember that in C you cannot compare strings using == , or you will compare string addresses. And no = , that is, the purpose. To compare strings, use strcmp or memcmp if there are no NUL characters at the end and the lengths are known.

0
source

Source: https://habr.com/ru/post/900000/


All Articles