RoyceyBaby
IS-IT--Management
- Mar 23, 2001
- 22
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] = "\0";
char *CmdEnd = NULL;
int strCount=0;
int i=0;
// Open the port
fd = open("/dev/ttyS0", 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("open_port: Unable to open /dev/ttyS0 - "
;
}
else
write(fd, "LOGI\n", 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("/dev/stdout", O_WRONLY | O_CREAT,S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
bufferstring[bufferlength]='\0';
for (i=0;i!=bufferlength;i++)
{
printf("<%c>", bufferstring);
}
printf("*"
;
}
}
}
}
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] = "\0";
char *CmdEnd = NULL;
int strCount=0;
int i=0;
// Open the port
fd = open("/dev/ttyS0", 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("open_port: Unable to open /dev/ttyS0 - "
}
else
write(fd, "LOGI\n", 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("/dev/stdout", O_WRONLY | O_CREAT,S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
bufferstring[bufferlength]='\0';
for (i=0;i!=bufferlength;i++)
{
printf("<%c>", bufferstring);
}
printf("*"
}
}
}
}