[En-Nut-Discussion] Timing problem with a rs485 line? or something else...
Ovidiu
ovidiu256 at gmail.com
Tue Apr 3 16:34:39 CEST 2007
Hello,
I am trying to make a character (byte) oriented simple protocol over the
serial line using two ethernut 1 boards connected together with a rs485
line (using the extension port), but I'm having serious problems.
the code must look like this, otherwise it's not working:
wait(2); //PROBLEM!!!!
fread(&octet,sizeof(char),1,stream);
(the wait is done by repeating the _delay_ms(10) function from libc, I can't
use NutSleep() cause other proceses will also read/write the serial line)
and I'm trying to read a structure like this:
typedef struct
{
char cmd;
char id;
int val;
} msg485_t;
msg485_t msg;
with the function:
read485(&msg,sizeof(msg485_t),rs485);
I dont know what is happening, maybe is a problem with the buffers or maybe
some echo on the line...
Thank you,
Ovidiu
PS:
1)Can Anyone, pleeeaaasseee help me before I throw my computer through the
window.
2)The rest of the code:
The function that is not working correctly:
void read485(void *buf,int marime,FILE *stream)
{
char octet;
char *buffer;
buffer=buf;
// printf("\nLength:%i\n",marime);
for(;;)
{
fread(&octet,sizeof(char),1,stream);
if(octet==_ESCAPE)
{
// printf("Escape received\n");
wait(2); //PROBLEM!!!!
fread(&octet,sizeof(char),1,stream);
if(octet==_START)
{
// printf("Start received\n");
int pozitie=0;
for(pozitie=0;pozitie<marime;pozitie++)
{
wait(2);
fread(&octet,sizeof(char),1,stream);
wait(2);
// printf("A char came for pozition:%i and the char
is:%c\n",pozitie,octet);
if(octet==_ESCAPE)
{
printf("Am primit un caracter escape!\n");
fread(&octet,sizeof(char),1,stream);
wait(2);
if(octet==_START)
{
pozitie=0;//when an error occurs (a start in the
midle of the message), resincronize on the current start
}
else if(octet==_ESCAPE)
{
buffer[pozitie]=_ESCAPE;
}
else
{
break;
}
}
buffer[pozitie]=octet;
// printf("Buffer at pozitie=%i is:%s\n",pozitie,buffer);
}
if(pozitie==marime) break;
}
}
}
}
The function I used to send the data:
void write485(void *m_orig,int marime,FILE* stream)
{
char m_final[20];
char *m_original;
m_original=m_orig;
printf("Original message:%s length:%i\n",m_original,marime);//debugging
only
m_final[0]=_ESCAPE;
m_final[1]=_START;
int i=0;
int pozitie=2;
for(i=0;i<marime;i++)
{
printf("i=%i pozitie=%i
m_original[i]=%c\n",i,pozitie,m_original[i]);//debug only
if(m_original[i]==_ESCAPE)
{
printf("Found an escape char!");//debug only
m_final[pozitie]=_ESCAPE;
pozitie++;
m_final[pozitie]=_ESCAPE;
pozitie++;
}
else
{
m_final[pozitie]=m_original[i];
pozitie++;
}
}
printf("Final message:%s\n",m_final);
fwrite(m_final,pozitie,1,stream);//the fwrite itself
fflush(stream);
}
void wait(int ms)
{
int i;
for(i=0;i<ms;i++)
{
_delay_ms(10);
}
}
More information about the En-Nut-Discussion
mailing list