uart problems
Jiggs
Posts: 26
Hi again
I need to send te value of an rcTime() readout over serial. This works ine if the value is one or two digits, but three digits do not transmit properly. For example, if the rc value is 296, 40 shows up on the other end of the line. The relevant code is shown below.
The value is then sent like so:
Any ideas?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
~Jiggs
I need to send te value of an rcTime() readout over serial. This works ine if the value is one or two digits, but three digits do not transmit properly. For example, if the rc value is 296, 40 shows up on the other end of the line. The relevant code is shown below.
public static int getTemp() { CPU.writePin(CPU.pin11,true); CPU.delay(1000); int temp = CPU.rcTime(32767,CPU.pin11,false); return(temp); }
The value is then sent like so:
getTemp(); tx.sendByte(temp)
Any ideas?
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
~Jiggs
Comments
otherwise you would have gotten a compiler error.
You need to transmit the return value from getTemp:
option1:
int myTemp = getTemp();
tx.sendByte(myTemp);
option2:
tx.sendByte(getTemp());
To send a larger number than 255:
int myTemp = getTemp();
tx.sendByte(myTemp);
tx.sendByte(myTemp>>>8);
regards peter
Post Edited (Peter Verkaik) : 8/12/2004 10:21:21 PM GMT
29610 = 1 0010 10002
the extra bit is lost, leaving the lower 8 buts 0010 10002 which is 40 decimal. So when you send byte in the line:
So, the 40 is the same variable and the correct output for what you asked it to do.
Peter's last bit is the important part of his post: Post Edited (GameMaster) : 8/13/2004 4:51:12 AM GMT
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
~Jiggs
sending temp will give you the lower 8, and sending temp>>>8 will give you the upper 1 (and 7 extra leading zeros. So, it seems that it should be
temp is 0000 0001 0010 1000
temp as a byte come out to be 0010 1000
temp>>>8 is 0000 0001
So, to get 0000 0001 0010 1000 ... It looks like you should send 'temp>>>8' and then 'temp'... But I suppose that depends on the endianess of the system. Try both, and see which yeilds the correct result.
*EDIT*
Now that I think of it, perhaps I should simply subtract 100 from the original value, and add it back on the other end.
*EDIT*
I was able to get it to work by simply subtracting 100 from the original rc value and sending it. However, this of course only works for a limited range of values. I would still like to know how to "chop" 9 bit and higher values into smaller peices to be reassembled on the other end. Thanks again for your help guys.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
~Jiggs
Post Edited (Jiggs) : 8/14/2004 4:36:14 AM GMT
int lowbyte = rx.receiveByte();
int highbyte = rx.receiveByte();
int myTemp = lowbyte + (highbyte<<8);
The problem is of course synchroniing on the first byte.
If you send all your values as 2 byte values that should
be no problem.
regards peter
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
~Jiggs
One byte is 0000 0000(bin) to 1111 1111)bin), or 0 (dec) to 25(dec)
The result is exactly one, when there is only one in the high byte, the statment 'X>>>Y' shift the bits of the varible X, Y places to the right.
You start with a bit string
0000 0001 0010 1000
So fist you send the last 8.
0010 1000
then you do >>>8 your bit string:
0000 0001 0010 1000
Each time stuff falls off the end, and zeros are pushed on the front:
Then you send the that result (which again sends the lower 8)
0000 0001
And put them back together as peter said.
To answer your question about bigger things... If for some reason you get values larger than 2 bytes can hold (2 bytes can hold values from 0 to 216 - 1) 65,535)... If it was the case that you'd expect vales that large, you'd send:
temp
temp>>>8
temp>>>16
Then you'd have to expect the three results on the recieving end, and do the inverse:
(code modified from peter's post)