0

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.

Edited by peter_budo: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)

Attachments
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <time.h>
#include <stdbool.h>
#include <stdlib.h>
#include <math.h>

FILE *prova;
int count_byte;
char contenitore[2];
unsigned int r;
float r_completo;

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;
}

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);
		}
		}	
	}
}

int scriviFile(char *contenuto)
{
	FILE *dove;
	dove=fopen("/var/cosa.txt","a");
	if(dove==NULL)
	{
		return -1;
	}
	fprintf(dove,"%x\n",contenuto);
	fclose(dove);
	return 0;
}

int convertiStatusBatteria()
{
	
}
2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by nezachem
0

Clear ISIG and turn off a canonical mode:

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

PS: and please use code tags.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.