Hi to all, I have a problem and can't understand why this happen... I'll explain all now. I'm not a very good programmer, I'm just starting and I'm doing a program that must run on linux like a daemon. The job of this program is to take all the characters that are sent via RS232 and print all in a file. I've made the program and all work until I don't recive the 0x03. At this point begins the strange:

here there is the function to set the serial port: (I've copied and pasted it from internet)

int initport (int fd)
{
	struct termios options;
	tcgetattr(fd, &options);
	cfsetispeed(&options,B9600);
	cfsetospeed(&options,B9600);
	options.c_cflag|=(CLOCAL|CREAD);
	options.c_cflag&= ~PARENB;
	options.c_cflag&=~CSTOPB;
	options.c_cflag&=~CSIZE;
	options.c_cflag|=CS8;
	tcsetattr(fd,TCSANOW,&options);
	return 1;
}

then there is the main:

int main ()
{
	count_byte=0;
	
	char c;
	prova=fopen("/dev/ttyUSB0","r");
while (1)
	{
	c=getc(prova);
	if(count_byte==0)
	{
		contenitore[0]=c;
		count_byte++;
	}else{
		if(count_byte==1)
		{
			contenitore[1]=c;
			count_byte=0;
			printf("\ncontenuto:0x %x 0x%x\n",contenitore[0],contenitore[1]);
			r=(contenitore[0]<<8)+(unsigned int)(contenitore[1]);
			r_completo=(((float)(r)/10)-509)/6.45;
		//	r_completo=r_completo*100;
			printf("%f",r_completo);
			scriviFile(r_completo);
		}
		}	
	}
}

when i send (for example) $19$56$0a$0d the result is:
contenuto:0x19 0x56
the buffer is then dirtied by the 0x0a character but this isn't the problem

if i send $19$04$0a$0d the result is:
contenuto:0x19 0xa
but what must compare is:
contenuto: 0x19 0x4

In the end the most strange problem that happen is when is send $19$03, than the result is:
after the first time i send the two characters it doesn't happen something and the second time that this two characters are send the result is:
risultato: 0xa 0xa

I've made my own opinion but it isn't possible: the 0x03 character interrupts everything???? I must have for result:
contenuto:0x19 0x3
I can't understand why it happens, please help me. Thanks to all and sorry if my English is bad.

Clear ISIG and turn off a canonical mode:

options.c_lflag &= ~(ISIG | ICANON);

PS: and please use code tags.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.