NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Everything Else » Serial client

September 24, 2009
by apurcell
apurcell's Avatar


I wrote a client in C to display the value from the tempsensor project to my terminal. The end goal is to collect the value and graph it using rrdtool. To my surprise the program actually works :), but I am sometimes getting what I think is truncated data. Intermittently it returns 7 ~ or a question mark that is surrounded by a bubble(Im sure there is a better description).

My guess is that this is a timing issue with the MCU printing to the serial port, and when my program fetches the value.

If that is indeed true, what does the forum think is the better approach? Have the client app collect a few samples and attempt to glean the good from the bad? Or, add a sleep timer in the tempsensor code? Say 2-3 seconds before an update? To me the latter seems simplier, but not sure of what caveats or issues that might introduce. Any insight is appreciated.

Regards, Anthony

September 24, 2009
by mrobbins
(NerdKits Staff)

mrobbins's Avatar

Hi Anthony,

If you take a look at some of our PC-side Python code for our digital scale project, you can see that we have a getReading function that will keep reading a line from the serial port and will try to parse a value out of it, and will keep repeating and will only return once it's successfully got a valid, parseable value. You may want to go with an approach like this to handle occasional communication errors.

Also, are you finding that the results are significantly more corrupted than what you see using a terminal program like those described in the "Serial Port Communications" section of our Servo Squirter video? If so, that may point to an issue with your PC-side C code, and you could post it for us all to look at.

Looking forward to seeing the rrdtool graphs! Best,


September 24, 2009
by apurcell
apurcell's Avatar

Thanks I will look over that code. So using kermit, I can connect to the serial port and it outputs flawlessly. Below is the output of my program, and the code respectively. The code was taken from various pieces of documentation I found.

 apurcell@furry:~/nerdkit/Code/test> while : ; do
 > ./port_check 
 > sleep 3
 > done
 the response is 72.94 �
 the response is 73.37 
 the response is 73.41 
 the response is 73.43 
 the response is �
 the response is 73.52 
 the response is 73.51 
 the response is 73.55 
 the response is 7
 the response is 73.70 
 the response is �
 the response is 73.75 
 the response is 73.79 �

You can see its intermittent.

#include <stdio.h>
#include <termios.h>
#include <string.h> 
#include <unistd.h> 
#include <fcntl.h> 
#include <errno.h>

int open_port() {
int fd;

fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY) ;
if (fd == -1) {
perror("unable to open /dev/ttyUSB0");

struct termios options;
//options are defined below

//tcgetaddr(fd, &options);
cfsetispeed(&options, B115200);
cfsetospeed(&options, B115200);

//disable parity bit
options.c_cflag &= ~PARENB;

//stop bit setting
options.c_cflag &= ~CSTOPB;

//masking for data size bits
options.c_cflag &= ~CSIZE;

//8 bits!
options.c_cflag |= CS8;

//disable flow control
options.c_cflag &= ~CRTSCTS;

//apply settings
if(tcsetattr(fd, TCSANOW, &options) != 0) {
perror("somehting bad happened");
//lets talk to the port finally

char buf [BUFSIZ];
char port_response;
port_response = read(fd, buf, BUFSIZ);
//printf(" %s\n",  buf);
return (buf);

int main() {

    int result;
    result = open_port();

    printf("the response is %s\r\n", result);

Post a Reply

Please log in to post a reply.

Did you know that NerdKits also has extra parts available for its customers? Learn more...