hi,
i have a static buffer of 32kb
data is put into it and while data is going in, i want to read into it and what i have read must be cleared?
can somebody help me please
platform is linux, language is c
hi,
i have a static buffer of 32kb
data is put into it and while data is going in, i want to read into it and what i have read must be cleared?
can somebody help me please
platform is linux, language is c
What are you even asking? Your question does not really follow. You can look at memset if you want, but your question is too general to really be answered. Your topic for this thread does not even follow with what you ask.
I also don't fully understand your question, but assuming that it is related to your other post on parallel threads and serial ports etc. it sounds to me like you might be looking for a circular buffer.
A circular buffer is like a loop of memory with a head pointer and a tail pointer. The writing process writes data to the memory location pointed to by the head pointer and then increments the head pointer. The reading process reads data pointed to by the tail pointer and then increments the tail pointer. When either pointer reaches the top of the memory allocated to the buffer it wraps round back to the bottom of the memory allocation. If the head pointer ever overtakes the tail pointer then your buffer has overflowed and you are in trouble!
I'm sure that you can find examples somewhere on the web but its not that complicated. Just make sure that you can process the incoming data as fast as (preferably faster than) it is read into the buffer.
this is the right thing, but i think the pointers are my problem, i just got into c, I need it for my project, i'm doing my internship.
thanx a lot
Post some of your code that you've tried so far, then we can give more precise help.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
there is smething wrong with the message pointer, it does not write to file
Code:#include <stdio.h> #include <termios.h> #include <fcntl.h> #include <sys/stat.h> #include <sys/types.h> #include <sys/io.h> #include <string.h> #include <stdlib.h> #include <pthread.h> #define PORT_DEV "/dev/ttyS0" /* This is COM1 port, try ttyS1 for COM2*/ #define BAUDRATE B9600 #define _POSIX_SOURCE 1 #define FF 0x0c int fd,res; static char buffer[3000]; int x=0,y=0,k=0; void read_port_con(); void read_message_con(); void write_to_file(char *message); main() { int i=0; struct termios oldtio,newtio; pthread_t thread1,thread2; int iret1,iret2; //OPEN COM PORT fd = open(PORT_DEV, O_RDWR | O_NOCTTY | O_NONBLOCK); 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 | ICRNL; newtio.c_oflag = 0; newtio.c_lflag = ICANON; tcflush(fd, TCIFLUSH); tcsetattr(fd,TCSANOW,&newtio); fcntl(fd, F_SETFL, 0); //START READING FROM PORT iret1 = pthread_create(&thread1,NULL,(void*)&read_port_con,NULL); pthread_join(thread1,NULL); iret2 = pthread_create(&thread2,NULL,(void*)&read_message_con,NULL); pthread_join(thread2,NULL); } } void write_to_file(char *message){ FILE *fp; fp=fopen("test.txt","a"); fputs(message,fp); close(fp); } void read_port_con() { char buf[1]; int res; printf("First read\n"); res=read(fd,buf,1); buf[res]=0; printf("Waiting for chars\n"); printf("%s",buf); while(res>0){ res=read(fd,buf,1); buf[res]=0; printf("%s",buf); buffer[x]=buf[0]; x++; if(x==2999){ x=0; } } } void read_message_con() { char message[5000]; message[k]=buffer[y]; k++; if(y==2999){ y=0; } if(buffer[y]==FF){ k=0; write_to_file(message); } }
Last edited by lectrolux; 05-07-2003 at 03:22 AM.
the port reading works, it's the buffer and the writing to file that is the problem,
sorry for having no code tags
Normally the reading from port into buffer is ok, but then it goes wrong, something with passing through the message?? It does not write to file, function is never started.
thx I'll try it, but in reading the port I also have to loop the buffer, this program gets constantly messages, the purpose is to read radar messages, the program never shots down, else there is data lost and that is out of the question.
I'm a noob in c pointers, and don't really know how to pass through arrays or pointers to other functions.
the threads don't loop al the time, you have to build in a loop for yourself, i find that strange , i know threads in java, it's different