reference:
http://shyuanliang.blogspot.tw/2011/01/rs232-read-write.html
http://nigelzeng.iteye.com/blog/1533141
content:
#include <pthread.h>
#include <stdio.h>
#include <termios.h>
#include <fcntl.h>
#include <string.h>
#include <sys/select.h>
#define BUFFER_SIZE 100
#define REQ_INFO 0x12
#define REQ_NA 0x00
char* Message="Hello1 Hello2 Hello3 Hello4 Hello5 Hello6 Hello7 Hello8 Hello9 Hello10";
char incomingBuffer[BUFFER_SIZE];
int fd1;
int fd2;
char csReq[100];//={0x12,0x00};
void* ReaderThread(void* object)
{
int total=0;
int expected=strlen(Message);
struct timeval timeOut;
fd_set fdSet;
int maxFdp=0;
printf("Waiting for data on serial ports\r\n");
printf("total =%d, expected = %d \r\n", total, expected);
while(total<100)
{
timeOut.tv_sec=0;
timeOut.tv_usec=100000; // Every 100 ms
FD_ZERO(&fdSet);
FD_SET(fd1, &fdSet);
// FD_SET(fd2, &fdSet);
maxFdp=fd1+1;
int result=select(maxFdp, &fdSet, (fd_set*)0, (fd_set*)0, &timeOut);
if(result<0) {
printf("A select error occured.. exiting\r\n");
exit(-1);
}
else if(result>0)
{
printf("Data is available on a port\r\n");
int n=0;
if(FD_ISSET(fd1, &fdSet)) {
n=read(fd1, incomingBuffer, BUFFER_SIZE);
printf("Data is available on a port ttyS1 \r\n");
}
// else if(FD_ISSET(fd2, &fdSet)) {
// n=read(fd2, incomingBuffer, BUFFER_SIZE);
// printf("Data is available on a port ttyS3 \r\n");
// }
else {
printf("Unexpected fd from select... exiting\r\n");
exit(-1);
}
incomingBuffer[n]='\0';
int i=strlen(incomingBuffer);
int j=0;
for(j=0;j<i;j++)
printf("Received: 0x%x\r\n", incomingBuffer[j]);
//total+=n;
total++;
}
// else (result==0) just a time out so go round again
}
return 0;
}
int OpenPort(const char* port)
{
printf("Opening serial %s\r\n", port);
int fd=open(port, O_RDWR | O_NOCTTY );
if(fd==-1)
{
printf("Error opening serial port %s... exiting", port);
return -1;
}
fcntl(fd, F_SETOWN/*F_SETFL*/, 0); /* Reads will be blocking */
struct termios options;
bzero((char*)&options, sizeof(options));
// tcgetattr(fd, &options);
// (void)cfsetispeed(&options, B115200); /* (void) is to stop warning in cygwin */
// (void)cfsetospeed(&options, B115200);
// options.c_cflag &= ~CSIZE;
// options.c_cflag |= CS8; /* 8 bits */
// options.c_cflag &= ~CSTOPB; /* 1 stop bit */
// options.c_cflag &= ~PARENB; /* no parity */
// options.c_cflag &= ~PARODD;
options.c_cflag = B115200 | CS8 | CLOCAL | CREAD;
// options.c_cflag &= ~CRTSCTS; /* HW flow control off */
// options.c_cflag |= (CLOCAL | CREAD);
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); //= 0; /* RAW input */
options.c_iflag = 0; /* SW flow control off, no parity checks etc */
options.c_oflag = 0;//&= ~OPOST; /* RAW output */
options.c_cc[VTIME]= 250;//0; //10; /* 1 sec */
options.c_cc[VMIN]= 0; //BUFFER_SIZE;
tcflush(fd, TCIFLUSH);
tcsetattr(fd, TCSANOW, &options);
return fd;
}
int main(int argc, char** argv)
{
int n=0;
fd1=OpenPort("/dev/ttyS2");
// fd2=OpenPort("/dev/ttyS1"); // assuming fd2>fd1 in thread
printf(" %d \r\n", isatty(fd1));
// Start thread to wait for incoming message on ttyS1
// pthread_t tid;
// pthread_create(&tid, (pthread_attr_t*)(0), &ReaderThread, (void*)(0));
sleep(1);
// memset(csReq, 0, sizeof(csReq));
// csReq[0]=0x11;
// csReq[1]=0x01;
// csReq[2]='\0';
// printf("Sending reboot req..., %#x, %d \r\n", *csReq, strlen(csReq));
// n=write(fd1, csReq, strlen(csReq));
// printf("n=%d \r\n", n);
memset(csReq, 0, sizeof(csReq));
csReq[0]=0x12;
csReq[1]=0x00;
printf("Sending get local dev info..., %#x, %d \r\n", *csReq, strlen(csReq));
// n=write(fd1, csReq, strlen(csReq));
// printf("n=%d \r\n", n);
n=write(fd1, csReq, 2);
printf("n=%d \r\n", n);
// n=write(fd1, csReq[1], 1);
// printf("n=%d \r\n", n);
// pthread_join(tid, (void*)(0));
// printf("After sent... \r\n");
if(!fd1)
{
printf(" fd1 is null!!\n");
return 0;
}
int res;
n=0;
int j;
char csRes[256];
char Res;
while(1)
{
printf("read - %d \n", n);
res=read(fd1, csRes, 255);
//res=read(fd1, Res, 1);
if(res == 0)
continue;
csRes[res]=0;
//printf("%d, read : res=%d, csRes=%s \n",n, res,Res);
printf("%d, read: res=%d, csRes=%s, sizeof(char) = %d \n", n, res, csRes, sizeof(char));
for(j=0; j<res; j++)
printf("received: %#x \n", csRes[j]);
n++;
if(n > 100)
break;
}
close(fd1);
// close(fd2);
printf("End of main-thread \n");
return 0;
}