 |
 |
| View previous topic :: View next topic |
| Author |
Message |
Friday Guest
|
|
Posted: Tue Nov 04, 2008 2:04 pm |
|
|
hi,
anyone to answer the newbie ?  |
|
 |
Friday Guest
|
|
Posted: Thu Nov 06, 2008 2:16 am |
|
|
Hi,
could anybody tell me if the use of the switch() routine and multiple if,else if in while(bkbhit) {} loop would cause the loss of some bytes waiting in the buffer ?
despite my corrections ,I still have wrong frames .
I´m always receiving:
1_0_-24 (1 as code)
reception procedure:
| Code: | void Get_Buffer_from_MB()
{
int1 GET_STX,RECEIVING_DATA;
int8 RXbyte,code ;//byte =int8
while(bkbhit)
{
RXbyte=bgetc();
if(RXbyte==STX)
{
BufferIndex=0;
GET_STX=1;
}
else
{ if (GET_STX)
{
RXBuffer[0]=RXbyte;
Code=RXBuffer[0];
BufferIndex++;
GET_STX=0;
RECEIVING_DATA=1;
Switch (code)
{
case 0: BufferSize=1;break;
case 1: BufferSize=1;break;
case 2: BufferSize=2;break;
default: BufferSize=0;break;
}
}
else
{ if ((RECEIVING_DATA==1) & (BufferSize>=1) & (BufferIndex<=BufferSize))
{
RXBuffer[BufferIndex]=RXbyte;
BufferIndex++;
}
}
}
if(RXbyte==ETX) //Check to see if end of frame has arrived.
{
//You now have a complete packet of data in the RX buffer.
YOU_HAVE_DATA=1;
RECEIVING_DATA=0;
}
}
} |
send procedure:
| Code: | //=============Send Procedure=============
void Load_Buffer_MB_to_SM(int8 code,unsigned long freq_ms,int1 HF,int1 ED)
{
//----Load_Buffer Mother Board_to_Stepper Motor
//Codes:
// 0:Enable/Disable Stepper motor Controller (L297) (+ Data=1 bit)
// 1:Half/Full (+ Data=1 bit)
// 2:Send Frequency (+ Data=int16)
// 3:Get Frequency (without Data)
// 4:Emergency Stop (without Data)
unsigned int i,length;
int8 SXBuffer[];
int1 l;
switch(code)
{
case 0:SXBuffer[1]=(int8)ED;length=1;l=1;break;
case 1:SXBuffer[1]=(int8)HF;length=1;l=1;break;
case 2:SXBuffer[1]=(int8)(freq_ms&0x00FF);
SXBuffer[2]=(int8)(freq_ms>>8);
length=2;l=1;break;
default: break;
}
SXBuffer[0]=code;
//Sending
bputc(STX);//send the Start of frame
bputc(SXBuffer[0]);
if(l)
{
for(i=1;i<=Length;i++)
{
bputc(SXBuffer[i]);
}
l=0;
}
bputc(ETX); // send end of frame.
// bputc("\n\r"); //CRLF
}
|
| Code: | #define STX '$'
#define ETX '€' |
|
|
 |
Friday Guest
|
|
Posted: Thu Nov 06, 2008 12:08 pm |
|
|
hi,
I´ve made some corrections and I can get the first byte of the data. (but still false code=RXBUFFEr[0])
TRANSMITTER:
| Code: | void Load_Buffer_MB_to_SM(unsigned int code,unsigned long freq_ms,int1 HF,int1 ED)
{
unsigned int i;
int1 l=0;
unsigned int length=0;
unsigned int SXBuffer[];
//----Load_Buffer Mother Board_to_Stepper Motor
//Codes:
// 0:Enable/Disable Stepper motor Controller (L297) (+ Data=1 bit)
// 1:Half/Full (+ Data=1 bit)
// 2:Send Frequency (+ Data=int16)
switch(code)
{
case 0:{SXBuffer[1]=(unsigned int)ED;length=1;l=1;break;}
case 1:{SXBuffer[1]=(unsigned int)HF;length=1;l=1;break;}
case 2:{SXBuffer[1]=(unsigned int)(freq_ms&0x00FF);
SXBuffer[2]=(unsigned int)(freq_ms>>8);
length=2;l=1;break;}
default: break;
}
SXBuffer[0]=code;
//Sending
bputc(STX); //send the Start of frame
bputc(SXBuffer[0]); //send the code
if(l)
{
for(i=1;i<=Length;i++)
{
bputc(SXBuffer[i]); //send the data
}
l=0;
}
bputc(ETX); // send end of frame.
}
void main() {
int1 freqsent=0;
set_tris_b(0x0f);
enable_interrupts(global);
lcd_init();
lcd_putc("Running...");
delay_ms(1000);
do{
// if bp1 envoi freq
if(bp1 & !freqsent)
{
Load_Buffer_MB_to_SM(2,75,0,0);
lcd_putc("\nFreq is sent");
freqsent=1;
}
|
RECEIVER:
| Code: | void Get_Buffer_from_MB()
{
int1 GET_STX=0,RECEIVING_DATA=0;
unsigned int BufferIndex=0,RXbyte ;//byte =int8
while(bkbhit)
{
RXbyte=bgetc();
if(RXbyte==STX) //Check to see if start of frame has arrived.
{
BufferIndex=0;
GET_STX=1;
}
else
{
if (GET_STX)
{
RXBuffer[0]=RXbyte;
BufferIndex++;
GET_STX=0;
RECEIVING_DATA=1;
if (RXBuffer[0]==0) {BufferSize=1;}
if (RXBuffer[0]==1) {BufferSize=1;}
if (RXBuffer[0]==2) {BufferSize=2;}
}
else { if ((RECEIVING_DATA==1) && (BufferSize>=1) && (BufferIndex<=BufferSize))
{
RXBuffer[BufferIndex]=RXbyte;
BufferIndex++;
}
}
}
if(RXbyte==ETX) //Check to see if end of frame has arrived.
{
//You now have a complete packet of data in the RX buffer.
YOU_HAVE_DATA=1;
RECEIVING_DATA=0;
}
}
}
void Display_RXBuffer_from_MB()
{
int i,HF,LF;
if (BufferSize>0)
{
printf(lcd_putc,"SIZE:%i,FM:",BufferSize);
for(i=0;i<=2;i++)
{
printf(lcd_putc,"%i,",RXBuffer[i]);
}
LF=RXBuffer[1];
HF=RXBuffer[2];
Freq=make16(HF,LF);
printf(lcd_putc,"\nSENT FREQ:%Lu",Freq);
}
BufferSize=0;
}
void main() {
enable_interrupts(int_rda);
enable_interrupts(global);
lcd_init();
lcd_putc("\f");
do {
Get_Buffer_from_MB();
Display_RXBuffer_from_MB();
// Read_Buffer_from_MB(RXBuffer[BufferSize]);
//Excute_MB_Buffered_commands();
} while (TRUE);
}
|
|
|
 |
Friday Guest
|
|
Posted: Thu Nov 06, 2008 12:10 pm |
|
|
my STX and ETX are '<' '>'
I´ll be very pleased if anyone could help me to find the origin of the problem. |
|
 |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Nov 06, 2008 2:07 pm |
|
|
| Quote: | unsigned int SXBuffer[];
case 0:{SXBuffer[1]=(unsigned int)ED;length=1;l=1;break;}
case 1:{SXBuffer[1]=(unsigned int)HF;length=1;l=1;break;}
case 2:{SXBuffer[1]=(unsigned int)(freq_ms&0x00FF);
SXBuffer[2]=(unsigned int)(freq_ms>>8); |
There's no size declaration on your buffer. You've created a pointer.
There's no memory allocated to it. You're probably over-writing other
variables.
Essential C:
http://cslibrary.stanford.edu/101/EssentialC.pdf |
|
 |
MarcosAmbrose
Joined: 25 Sep 2006 Posts: 38 Location: Adelaide, Australia
|
| Re: opinions about my PIC to PIC Code |
Posted: Thu Nov 06, 2008 3:02 pm |
|
|
| Friday wrote: | | I was largely inspired by MarcosAmbrose code (in page 2 (vittorio topic)) |
Hi Friday, I'm happy to see I have "Inspired" someone. Did you try to implement the routines that I posted in the vittorio thread? For you, perhaps this may be easier than re-inventing the wheel. |
|
 |
Friday Guest
|
|
Posted: Thu Nov 06, 2008 6:02 pm |
|
|
| PCM programmer wrote: | | Quote: | unsigned int SXBuffer[];
case 0:{SXBuffer[1]=(unsigned int)ED;length=1;l=1;break;}
case 1:{SXBuffer[1]=(unsigned int)HF;length=1;l=1;break;}
case 2:{SXBuffer[1]=(unsigned int)(freq_ms&0x00FF);
SXBuffer[2]=(unsigned int)(freq_ms>>8); |
There's no size declaration on your buffer. You've created a pointer.
There's no memory allocated to it. You're probably over-writing other
variables.
|
Dear PCM Programmer,
you´ve found it !!!
now it works great,
a big thank you.
thank you also for the link. (I really need it ) |
|
 |
Friday Guest
|
| Re: opinions about my PIC to PIC Code |
Posted: Thu Nov 06, 2008 6:32 pm |
|
|
| MarcosAmbrose wrote: | | Friday wrote: | | I was largely inspired by MarcosAmbrose code (in page 2 (vittorio topic)) |
Hi Friday, I'm happy to see I have "Inspired" someone. Did you try to implement the routines that I posted in the vittorio thread? For you, perhaps this may be easier than re-inventing the wheel. |
Hi Marcos,
firstly, thank you for posting your code and inspiring me.
as you can see in my two programs, I used your code with some changements and removals ,for instance,as I´m using the SISR.C and STISR.C routines , the RDA routine is already written, besides as I chose STX and ETX characters , so I don´t have to use the ESC and make its corresponding functions.other important thing: I can know the length of my received data since the read of the first byte (that I´m calling code).
any suggestion or question is still welcome.  |
|
 |
MarcosAmbrose
Joined: 25 Sep 2006 Posts: 38 Location: Adelaide, Australia
|
| Re: opinions about my PIC to PIC Code |
Posted: Thu Nov 06, 2008 6:44 pm |
|
|
Cool, Glad you got it working. Don't forget to include some sort of checksum byte in your packet to guard against data transmission errors. A checksum byte can be as simple as just ADDing or XORing all your data together.
P.S My name's is not really Marcos. Marcos Ambrose is actually a Nascar driver from Australia. |
|
 |
Friday Guest
|
| Re: opinions about my PIC to PIC Code |
Posted: Sun Nov 09, 2008 1:04 am |
|
|
| MarcosAmbrose wrote: |
Cool, Glad you got it working. Don't forget to include some sort of checksum byte in your packet to guard against data transmission errors. A checksum byte can be as simple as just ADDing or XORing all your data together.
| Quote: | Hi ,
thank you for the advice, I´ll add it. |
P.S My name's is not really Marcos. Marcos Ambrose is actually a Nascar driver from Australia. |
I've added routines in each code to have a whole communication (both of them can receive or transmit) using the protocol above and it is working fine.
So if anybody is interested, I can post them. |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|