arduino - Function works every other time it is called -
my following function reads serial port, processes data , displays info user. interesting thing seems work when called on odd numbered instances. on reads, timeout case occurs.
this function called every 10sec or sooner if called user. no matter how it's called work every other time.
code:
bool sr(bool echo, int&value) { bool valid; byte buf [2]; on calls red returns 0
byte red = serial2.readbytes(buf,2); if(red>0) { here "good data" case svadd gives me sender address while fcode gives type of message sent (there 8 types of messages, we're using types 3 , 6)
valid=true; byte svadd = buf[0]; byte fcode = buf[1]; int read; byte registerl; switch (fcode) { case 3: data case 3 (read-only) appears shown in table: 
serial2.readbytes(buf,1); registerl = buf[0]; (byte i=(registerl/2); i>0 ; i--) { serial2.readbytes(buf,2); read = buf[1] | buf[0] << 8; } for now, registerl should 2 added loop in case
value=read; break; case 6: data case 6 (write read) appears shown in table: 
serial2.readbytes(buf,2); read = buf[1] | buf[0] << 8; serial2.readbytes(buf,2); read = buf[1] | buf[0] << 8; value=read; i know address read past , @ data bits verify correct function written
break; } aquire last 2 crc bytes function (to clear buffer)
serial2.readbytes(buf,2); int crc = buf[1] | buf[0] << 8; if (echo) { serial.println(svadd); serial.println(fcode); switch (fcode) { case 3: serial.println(registerl); serial.println(read); //last value read break; case 6: serial.println(read); //value break; } serial.println(crc); } return valid; } here case when red=0
else { valid=false; serial.println("timeout"); return valid; } } edit (contiguous code)
bool sr(bool echo, int&value) { bool valid; byte buf [2]; byte red = serial2.readbytes(buf,2); if(red>0) { valid=true; byte svadd = buf[0]; byte fcode = buf[1]; int read; byte registerl; switch (fcode) { case 3: serial2.readbytes(buf,1); registerl = buf[0]; (byte i=(registerl/2); i>0 ; i--) { serial2.readbytes(buf,2); read = buf[1] | buf[0] << 8; } value=read; break; case 6: serial2.readbytes(buf,2); read = buf[1] | buf[0] << 8; serial2.readbytes(buf,2); read = buf[1] | buf[0] << 8; value=read; break; } serial2.readbytes(buf,2); int crc = buf[1] | buf[0] << 8; if (echo) { serial.println(svadd); serial.println(fcode); switch (fcode) { case 3: serial.println(registerl); serial.println(read); //last value read break; case 6: serial.println(read); //value break; } serial.println(crc); } return valid; } else { valid=false; serial.println("timeout"); return valid; } }
one thing see read defined int i.e. signed int. wonder if you'd better results, since you're creating through bit-shifting, if you'd define unsigned int. if shift 1 msb position, of sudden (as int) read negative number.
next:
read = buf[1] | buf[0] << 8; serial2.readbytes(buf,2); read = buf[1] | buf[0] << 8; value=read; you realize read being assigned twice consecutively, right? retain last value. that's okay if want discard transmitted address. doesn't do, because of this:
serial.println(read); //addr serial.println(read); //value read not both address , value.
i suggest 2 fishy items first. can't run code right now, closer @ these items might shed light on issue.
finally, function call itself, if want pass pointer in, should read:
bool sr(bool echo, unsigned int * value) but bottom line if you're getting "timeout" every other time, it's because red not > 0, , readbytes statement above didn't work.
Comments
Post a Comment