 |
 |
| View previous topic :: View next topic |
| Author |
Message |
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Thu Aug 02, 2007 8:56 am |
|
|
...deleted..
Last edited by treitmey on Thu Aug 02, 2007 11:47 am; edited 1 time in total |
|
 |
inservi
Joined: 13 May 2007 Posts: 128
|
|
Posted: Thu Aug 02, 2007 10:42 am |
|
|
Hello,
In this code, you call bgetc() when you are not shure that rx_i!=rx_o (three times):
| Code: | while(rx_i!=rx_o)//do this loop when data is recieved
{
if(rx_state==Looking && bgetc() =='*') rx_state=Found_star;
if(rx_state==Found_star) //only if "Found_star" do we read another char
{
----> if(bgetc() =='#') rx_state=Found_both;//check if next char is # else reset state
// !!! you call bgetc() but you are not shure that rx_i!=rx_o
else rx_state=Looking;
}
if(rx_state==Found_both)//if I found both, read 1 char at a time
{
rx_state=Loop_data;
tx_buf[tx_i]='*';tx_i=(tx_i+1) % BUFF_SZ;
tx_buf[tx_i]='#';tx_i=(tx_i+1) % BUFF_SZ;
}
if(rx_state==Loop_data)
{
-----> rxdata=bgetc();
// !!! you call bgetc() but you are not shure that rx_i!=rx_o
if(rxdata=='\r') rx_state==Looking //found a <cr> reset state
else
{
-----> tx_buf[tx_i]=bgetc();
// !!! you call bgetc() but you are not shure that rx_i!=rx_o
tx_i=(tx_i+1) % BUFF_SZ;
}
}
} |
_________________ in médio virtus |
|
 |
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Thu Aug 02, 2007 10:53 am |
|
|
you are correct., I should break; after each read.
or continue; which ever you like.
Last edited by treitmey on Thu Aug 02, 2007 11:32 am; edited 2 times in total |
|
 |
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Thu Aug 02, 2007 10:59 am |
|
|
How bout this.
| Code: | #define VER_MINOR 07
#define VER_MAJOR 0
#include <18F4525.h>
#use delay(clock=20000000)
#fuses HS,NOWDT,NOLVP,PROTECT,PUT,BROWNOUT
//#use rs232(baud=19200,xmit=PIN_B3,INVERT,stream=DEBUG,DISABLE_INTS)
#use rs232(baud=19200,xmit=PIN_B3,INVERT,stream=DEBUG)
#use rs232(baud=9600,errors,xmit=PIN_C6,rcv=PIN_C7,stream=PC)
#case
#zero_ram
#define INTS_PER_SECOND_TMR2 77
#define BUFF_SZ 32
int8 rx_buf[BUFF_SZ];
int8 rx_i = 0;
int8 rx_o = 0;
int8 tx_buf[BUFF_SZ];
int8 tx_i = 0;
int8 tx_o = 0;
int8 int_count_tmr2;
int8 timer[8];//set a side 8 different 1sec timers
#define tmr_led timer[0]
//=== prototypes====
init();
int8 bgetc();
//======================= Main ==============================
void main() {
enum states {Looking,Found_star,Loop_data};
states rx_state=Looking;
int8 rxdata=0;
init();
while(1)
{
while(rx_i!=rx_o)//do this loop when data is recieved
{
if(rx_state==Looking && bgetc() =='*')
{
rx_state=Found_star;
break;
}
if(rx_state==Found_star) //only if "Found_star" do we read another char
{
if(bgetc() =='#')
{
rx_state=Loop_data;
tx_buf[tx_i]='*';tx_i=(tx_i+1) % BUFF_SZ;
tx_buf[tx_i]='#';tx_i=(tx_i+1) % BUFF_SZ;
}
else rx_state=Looking;
break;
}
if(rx_state==Loop_data)
{
rxdata=bgetc();
if(rxdata=='\r') rx_state==Looking; //found a <cr> reset state
else
{
tx_buf[tx_i]=rxdata;
tx_i=(tx_i+1) % BUFF_SZ;
}
}
}
}
}
init() {
setup_adc_ports(NO_ANALOGS);
fprintf(DEBUG,"Starting\n\r");
fprintf(DEBUG,"test V%u.%02u\n\r",VER_MAJOR,VER_MINOR);
enable_interrupts(GLOBAL);
enable_interrupts(INT_RDA);
enable_interrupts(INT_TIMER2);
setup_timer_2(T2_DIV_BY_16,255,16);
set_timer2(250);
int_count_tmr2 = INTS_PER_SECOND_TMR2;
}
//=======================bgetc============================//
int8 bgetc() {
int8 c;
c=rx_buf[rx_o];
rx_o=(rx_o+1) % BUFF_SZ;
return(c);
}
//=======================int_rda============================//
#int_rda
char serial_isr() {
int t;
rx_buf[rx_i]=fgetc(PC);
rx_i=(rx_i+1) % BUFF_SZ;
}
//=======================timer 2 ISR============================//
#INT_TIMER2 // This function is called every time
void timer2_isr() { // timer 2 overflows (250->0), which is
int8 x;
if(--int_count_tmr2==0){ // approximately 15 times per second for this program.
//fprintf(DEBUG,".");//1 sec heartbeat
for (x=0;x<sizeof(timer);x++){
restart_wdt();
if (timer[x]>0) timer[x]--;//reduce individual timer counts until all==0
}
int_count_tmr2 = INTS_PER_SECOND_TMR2;
}
} |
|
|
 |
Mark Weir
Joined: 11 Sep 2003 Posts: 51 Location: New Zealand
|
|
Posted: Thu Aug 02, 2007 1:38 pm |
|
|
We do send a 0's to each field where necessary to make up the 4 digit number. What I meant about the payload size about 100 bytes is that it can vary between strings but it is always terminated with a <cr> after the three checksum bytes.
Cheers
Mark _________________ Life is too short to avoid asking questions |
|
 |
|
|
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
|