Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

linux + serial port + loop stalling

Status
Not open for further replies.

RoyceyBaby

IS-IT--Management
Mar 23, 2001
22
0
0
Hi,

I am trying to read data in from the serial port, the problem I am having at the moment is to do with using the printf command. It seems to be hanging in my loop and stalls the continuation of the program. I have attached the offending code below.

PS I am trying to learn C, please don't laugh too much at my C program layout :)

Many thanks,

Royce

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <ctype.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>

int main(void)
{
// Setup some vairables
int fd;
int screen;
int bufferlength;
char bufferstring[128];
fd_set fds;
struct timeval tv;
struct termios options;
int length;
void *pointer;
char remember[5000];
char TempBufferString[50] = &quot;\0&quot;;
char *CmdEnd = NULL;
int strCount=0;
int i=0;

// Open the port
fd = open(&quot;/dev/ttyS0&quot;, O_RDWR);

// Set some options
fcntl(fd, F_SETFL, FNDELAY);
tcgetattr(fd, &options);
cfsetispeed(&options, B2400);
cfsetospeed(&options, B2400);
options.c_cflag |= (CLOCAL | CREAD);
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
tcsetattr(fd, TCSANOW, &options);

// Check for errors
if (fd == -1)
{
// Error then report
perror(&quot;open_port: Unable to open /dev/ttyS0 - &quot;);
}
else
write(fd, &quot;LOGI\n&quot;, 5);

while(1)
{
tv.tv_sec=1;
tv.tv_usec=0;

FD_ZERO(&fds);
FD_SET(fd, &fds);

if(select(fd+1, &fds, NULL, NULL, &tv) >0);
{
bufferlength = read(fd, bufferstring, 127);
if (bufferlength > 0)
{
screen = open(&quot;/dev/stdout&quot;, O_WRONLY | O_CREAT,S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);

bufferstring[bufferlength]='\0';

for (i=0;i!=bufferlength;i++)
{
printf(&quot;<%c>&quot;, bufferstring);
}
printf(&quot;*&quot;);
}
}
}
}
 
Hey!

First of all, the while loop never ends!
You have
Code:
 while(1)
statament there and nowhere inside the loop a break; statament.
So, i don't think that the program stalls, it just never exits the loop!

Second, instead of printing every character of the string, why don't use:
Code:
 printf(&quot;<%s>\n&quot;,bufferstring)
???
This should output the whole string...

And third of all, what you will get as output for the for loop will be the same character printed out bufferlength times... I think what you ment was:
Code:
 printf(&quot;<%c>&quot;,bufferstring[i])
Nosferatu
We are what we eat...
There's no such thing as free meal...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top