4

I have a simple program written in C which uses termios to send a basic string to the Raspberry Pi UART and attempts to read and output the response. The Rx and Tx pins on the Raspberry Pi are connected with a jumper so whatever is sent should be immediately received.

Despite the program outputting that it successfully sent and received 5 characters for the chosen string ('Hello'), trying to print the contents of the buffer just produces one or two garbage characters.

The program:

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <fcntl.h> #include <termios.h> int main(int argc, char* argv[]) { struct termios serial; char* str = "Hello"; char buffer[10]; if (argc == 1) { printf("Usage: %s [device]\n\n", argv[0]); return -1; } printf("Opening %s\n", argv[1]); int fd = open(argv[1], O_RDWR | O_NOCTTY | O_NDELAY); if (fd == -1) { perror(argv[1]); return -1; } if (tcgetattr(fd, &serial) < 0) { perror("Getting configuration"); return -1; } // Set up Serial Configuration serial.c_iflag = 0; serial.c_oflag = 0; serial.c_lflag = 0; serial.c_cflag = 0; serial.c_cc[VMIN] = 0; serial.c_cc[VTIME] = 0; serial.c_cflag = B115200 | CS8 | CREAD; tcsetattr(fd, TCSANOW, &serial); // Apply configuration // Attempt to send and receive printf("Sending: %s\n", str); int wcount = write(fd, &str, strlen(str)); if (wcount < 0) { perror("Write"); return -1; } else { printf("Sent %d characters\n", wcount); } int rcount = read(fd, &buffer, sizeof(buffer)); if (rcount < 0) { perror("Read"); return -1; } else { printf("Received %d characters\n", rcount); } buffer[rcount] = '\0'; printf("Received: %s\n", buffer); close(fd); } 

Outputs:

Opening /dev/ttyAMA0 Sending: Hello Sent 5 characters Received 5 characters Received: [garbage] 

I can't see any major problem with the code myself, but I might be wrong. I can successfully send and receive characters using PuTTY connected with the same settings, so it can't really be a hardware problem. Although I haven't tried it in PuTTY, trying to connect with anything less than 115200 baud with this program will result in nothing being received.

Where am I going wrong?

2
  • Besides the applicable answer by @parkydr, you might have issues when not looped back and connecting to a real device. Zeroing out the termios members is bad coding practice. The proper POSIX method it to bit-wise clear or set each necessary flag without modifying any other bits or structure members. In your code the tcgetattr() call is essentailly superfluous. You should be checking the return code from tcsetattr() just like tcgetattr(). Commented Jun 23, 2013 at 23:13
  • @sawdust This was generally just to make sure I had the basics working before writing the program properly, I'll take into account your advice though. Commented Jun 24, 2013 at 0:54

1 Answer 1

5
int wcount = write(fd, &str, strlen(str)); int rcount = read(fd, &buffer, sizeof(buffer)); 

In these lines, buffer/str are already pointers. You are passing a pointer to a pointer.

The lines should be:

int wcount = write(fd, str, strlen(str)); int rcount = read(fd, buffer, sizeof(buffer)); 
Sign up to request clarification or add additional context in comments.

1 Comment

I knew I'd have made a stupid error somewhere, works perfectly, thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.