Shop OBEX P1 Docs P2 Docs Learn Events
Serial Communication : 8bit — Parallax Forums

Serial Communication : 8bit

dkwondkwon Posts: 13
edited 2005-10-27 10:47 in General Discussion
Hello,

I am doing serial communcation between pc and javelin.
Since using sendByte funtion, I can only send 8bit data. Is there any way to get higher resolution data?

Thanks in advance.

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2005-10-25 14:24
    You can send an int (16bits) by sending the lowbyte, then the highbyte.

    int c;

    txUart.sendByte(c);

    txUart.sendByte(c>>>8); //notice the unsigned shift

    On the receiving side (eg. pc), the program must·know when to expect an integer.

    You could send a synchronize value that is never an existing value, for example 0xFFFF.

    regards peter
  • dkwondkwon Posts: 13
    edited 2005-10-26 12:54
    I still have a problem.

    How can i send negative value?

    In PC side java, how can i convert the data from javelin?
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2005-10-26 13:25
    Negative values are send in the same way.

    A signed integer of 16bits has dedicated b15 as the sign bit.

    On the receive end:

    int L = receiveByte(); //assuming you send low byte first

    int H = receiveByte();

    int value = (H<<8)|(L&0xFF); //16bits value

    regards peter
  • dkwondkwon Posts: 13
    edited 2005-10-26 16:16
    It is beautiful. I finally succeeded it. However, I still have a problem with negative value.

    Can you give me an example of negative values also for Javelin side and PC side?

    How can I know it is negative in PC side?
    Do I have to send additional byte to notify it is negative like 0:negative and 1: positive?

    For synchronization, you mention that I can use 0xFFFF. But javelin complains the number is out of something.
    Please give me how can I synchronize PC and javelin.

    Thanks alot!!
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2005-10-26 16:49
    Integers in the javelin are always 16bits signed.

    For unsigned 16bits values use short.

    Example:

    int p = -32768;

    short q = 0x8000;

    int r = (short)0x8000;

    p,q and r all have the same value, namely 0x8000,
    but p and r are treated as signed.

    positive values 0 to 32767 (hex 0x0000-0x7FFF)
    negative values -32768 to -1 (hex 0x8000 to 0xFFFF)

    if -32768 is never a valid value for your·data, you can use 0x8000 to synchronize.


    On the receive end (your pc program), ints may be 32bits.
    In that case assembling a 16bits value always is positive.

    Then you can do this:
    int L = receiveByte(); //lowbyte
    int H = receiveByte(); //highbyte
    int R = (H<<8)|(L&0xFF); //16bits value
    int result = (R >= 0x8000) ? R-65536 : R; //make negative int

    If your pc program has a 16bits integer type (like int16) you could use that.

    regards peter


    Post Edited (Peter Verkaik) : 10/26/2005 5:01:05 PM GMT
  • dkwondkwon Posts: 13
    edited 2005-10-27 10:47
    Thanks alot peter!
Sign In or Register to comment.