Code:
#include <stdio.h>
#include <termios.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/io.h>
#include <dirent.h>
#include <string.h>
#include <stdlib.h>
#include <mysql/mysql.h>
#include <pthread.h>
#include <unistd.h>
#define _REENTRANT
#define PORT_DEV "/dev/ttyS0" /* This is COM1 port, try ttyS1 for COM2*/
#define BAUDRATE B9600
#define _POSIX_SOURCE 1
#define FALSE 0
#define TRUE 1
#define CR 0x0d
#define LF 0x0a
#define FF 0x0c
#define SX 0x02
#define EX 0x03
#define EVER ;;
#define BUFNUM 128
static char buffer[BUFNUM][256];
static char buf[60][3000];
int i=0,c=0;
int res,fd;
struct bufadmi{
pthread_mutex_t buflock;
pthread_cond_t bufcond;
int bufvol;
};
struct bufadmi ba[BUFNUM];
void *read_port();
void *read_buffer(pthread_t *);
void write_buffer(char *buffer2);
main()
{
DIR *dirp;
struct termios oldtio, newtio;
pthread_t thread1,thread2;
pthread_attr_t thrattr;
int iret1,iret2;
int j=0;
//CHECK ID DIRECTORY EXIST
if((dirp=opendir("log"))==NULL){
printf("Directory <log> does not exist, please create it\n");
exit(0);
}
if((dirp=opendir("log/AFTN"))==NULL){
printf("Directory <log/AFTN> does not exist, please create it\n");
exit(0);
}
if((dirp=opendir("log/ATS"))==NULL){
printf("Directory <log/ATS> does not exist, please create it\n");
exit(0);
}
//OPEN COM PORT
fd = open(PORT_DEV, O_RDONLY | O_NOCTTY | O_NONBLOCK | O_APPEND);
if (fd < 0)
{
perror("Opening port");
exit(-1);
}
else if(fd > 0)
{
perror("Opening port");
//INITIALIZATION SERIAL PORT
tcgetattr(fd,&oldtio);
bzero(&newtio, sizeof(newtio));
newtio.c_cflag = BAUDRATE |CS8 | CLOCAL | CREAD;
newtio.c_iflag = IGNPAR;
newtio.c_oflag = 0;
newtio.c_lflag = 0;
newtio.c_cc[VMIN]=255;
newtio.c_cc[VTIME]=0;
//tcflush(fd, NULL);
tcsetattr(fd,TCSANOW,&newtio);
fcntl(fd, F_SETFL,0);
//START READING FROM PORT
pthread_attr_init(&thrattr);
pthread_attr_setscope(&thrattr, PTHREAD_SCOPE_SYSTEM);
if((iret1 = pthread_create(&thread1,&thrattr,read_port,NULL))){
fprintf(stderr,"pthread1: %s\n",strerror(iret1));
exit(1);
}else printf("pthread1 succesful\n");
if((iret2 = pthread_create(&thread2,&thrattr,(void*)(pthread_t *)read_buffer,&read_port))){
fprintf(stderr,"pthread2: %s\n",strerror(iret2));
exit(1);
}else printf("pthread2 succesful\n");
for(j=0;j<BUFNUM;j++){
pthread_mutex_init(&ba[j].buflock,NULL);
pthread_cond_init(&ba[j].bufcond,NULL);
}
//pthread_join(thread1,NULL);
//pthread_join(thread2,NULL);
pthread_exit(NULL);
}
tcsetattr(fd,TCSANOW,&oldtio);
}
void * read_port(void)
{
char buf[256];
for(EVER){
/*pthread_mutex_lock(&ba[i].buflock);
while(ba[i].bufvol)
pthread_cond_wait(&ba[i].bufcond,&ba[i].buflock);*/
//printf("loop entered\n");
res=read(fd,buf,255);
buf[res]='\0';
//printf("%i\n",res);
//printf("Dit waren 255 chars\n");
if(res > 0){
strcat(buffer[i],buf);
//ba[i].bufvol = 1;
printf("---coming form port---\n%s\n---end---\ni = %i\n",buffer[i],i);
i++;
//printf("Dit waren %d chars, i is nu %d\n",strlen(buf),i);
if(i==BUFNUM){
i=0;
}
/*pthread_cond_signal(&ba[i].bufcond);
pthread_mutex_unlock(&ba[i].buflock);*/
}
}
}
void * read_buffer(pthread_t *read_port)
{
int a=0,b=0,d=0,z=0;
for(EVER){
//printf("a = %i\n",a);
/*pthread_mutex_lock(&ba[a].buflock);
while(ba[a].bufvol)
pthread_cond_wait(&ba[a].bufcond,&ba[a].buflock);*/
if(i==a){}else{
//printf("buffer[a][b] comes in buf[c][d]\n");
buf[c][d]=buffer[a][b];
//printf("%i %i %i %i\n",a,b,c,d);
d++;
if(buffer[a][b]==FF){
printf("FF found\n");
buf[c][d++]='\0';
printf("buffer lengte is %d\n",strlen(buf[c]));
printf("---begin message---\n%s\n---end of message---\n",buf[c]);
write_buffer(buf[c]);
c++;
printf("\nc gets %i",c);
d=0;
}
b++;
if(b==strlen(buffer[a])){
printf("i = %d and a = %d\n",i,a);
b=0;
/*ba[a].bufvol=0;
pthread_cond_signal(&ba[a].bufcond);
pthread_mutex_unlock(&ba[a].buflock);*/
for(z=0;z<256;z++){
buffer[a][z]='\0';
}
a++;
}
if(a==BUFNUM){
a=0;
}
if(d==3000){
d=0;
}
if(c==60){
c=0;
}
}
}
}