Shop OBEX P1 Docs P2 Docs Learn Events
uart problems — Parallax Forums

uart problems

JiggsJiggs Posts: 26
edited 2004-08-14 04:58 in General Discussion
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.
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

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2004-08-12 22:18
    You probably have a global variable temp also

    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
  • J. A. StreichJ. A. Streich Posts: 158
    edited 2004-08-13 04:43
    The parent post said...
    ... if the rc value is 296, 40 shows up on the other end of the line
    A byte is 8 bits from 0 to 255... 296 requires 5 bits.
    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:
    tx.sendByte(temp);
    



    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:
    Peter said...
    To send a larger number than 255:

    int myTemp = getTemp();

    tx.sendByte(myTemp);

    tx.sendByte(myTemp>>>8);
    Post Edited (GameMaster) : 8/13/2004 4:51:12 AM GMT
  • JiggsJiggs Posts: 26
    edited 2004-08-14 03:42
    So if I add temp and temp>>>8 on the receiving end, will I get the original value?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ~Jiggs
  • J. A. StreichJ. A. Streich Posts: 158
    edited 2004-08-14 04:18
    Errr, that's what peter said, but I think that is backward now that I think about it...
    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.
  • JiggsJiggs Posts: 26
    edited 2004-08-14 04:21
    I've been working with your suggestions. 296>>>8 = 1.15625, which the Javelin calls 1. Unfortunaltely, this result is produced with a wide range of numbers. How is the computer on the receiving end supposed to differentiate 296 from, say, 300? Even if this wasn't an issue, how can I get the receiving computer to assemble temp and temp>>>8 into a single, 9 bit number? I very much appreciate your help, but I'm afraid I'm still quite lost.

    *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
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2004-08-14 04:29
    On the receiver end you have code

    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
  • JiggsJiggs Posts: 26
    edited 2004-08-14 04:44
    Thanks Peter and GameMaster, everything works fine now, without having to subtract 100.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ~Jiggs
  • J. A. StreichJ. A. Streich Posts: 158
    edited 2004-08-14 04:58
    Jiggs said...
    I've been working with your suggestions. 296>>>8 = 1.15625, which the Javelin calls 1. Unfortunaltely, this result is produced with a wide range of numbers. How is the computer on the receiving end supposed to differentiate 296 from, say, 300? Even if this wasn't an issue, how can I get the receiving computer to assemble temp and temp>>>8 into a single, 9 bit number? I very much appreciate your help, but I'm afraid I'm still quite lost.
    Don't think about your number as a number -- think about it as a string of zeros and ones.
    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:
    0000 0000 1001 0100 (0)          (>>>1)
    0000 0000 0100 1010 (00)        (>>>2)
    0000 0000 0010 0101 (000)      (>>>3)
    0000 0000 0001 0010 (1000)    (>>>4)
    0000 0000 0000 1001 (0100 0) (>>>5)
    ...
    0000 0000 0000 0001 (0010 1000) (>>>8)
    
    



    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:
    int lowbyte = rx.receiveByte();
    int midbyte = rx.receiveByte();
    int highbyte = rx.receiveByte();
    int myTemp = lowbyte + (midbyte<<8) + (highbyte<<16);
    
    



    (code modified from peter's post)
Sign In or Register to comment.