Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get C to write and read serial port

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;
}
like image 340
Potential Customer Avatar asked Oct 24 '11 22:10

Potential Customer


2 Answers

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()).

like image 182
vines Avatar answered Nov 14 '22 21:11

vines


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 :-)

like image 40
hochl Avatar answered Nov 14 '22 21:11

hochl