Shop OBEX P1 Docs P2 Docs Learn Events
Uart & 232 protocols string or hex? — Parallax Forums

Uart & 232 protocols string or hex?

GJGJ Posts: 19
edited 2006-07-22 18:03 in General Discussion
Hello

I need to transmitt a series of bytes to a pc.· I am doing it by using the string buffer and building the string with rs232msg.append, then sending the string with sendString(rs232msg.toString()· the problem is, I actually need to transmitt the data as a series of hex numbers and not a string.· How do I build a string of hex numbers?· Or, is it easier to just use sendByte and loop through each number?· What method is more effecient for the Javelin?

Also, I need to ready data coming from the PC.· I need to parse the data.· The PC data format will be start byte = FF, lenght byte = #number of bytes being sent, PC software version number, data bytes (possibly 10 bytes or so), checksum of data bytes.· I know how to use byteAvaliable and receiveByte, but once it is received, how do I parse it so I know what is what?· Wont the received byte be a long sequence of numbers?· I would guess that I need to loop through, look for byte FF then read each byte for the specified number of bytes.· This being the case, any suggestions on how I do this?· The PC is always sending this data in a loop but the Javelin will only look at the data when it is ready.

Comments

  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2006-07-22 18:03
    You can use the Format class to convert ascii bytes to hexbytes

    char[noparse]/noparse hexbytes = new char[noparse][[/noparse]3]; //hold byte as 2 hexnumbers (00-FF) plus closing 0
    char[noparse]/noparse myString = new char[noparse][[/noparse]128]; //your array holding ascii bytes
    int len; //number of bytes in myString

    for (int i=0; i<len; i++) { //send bytes from myString as hexbytes
    · int c = myString[noparse][[/noparse]i]&0xFF;
    · Format.sprintf(hexbytes,"%02x",c)
    · tx.sendByte(hexbytes[noparse][[/noparse]1]);
    · tx.sendByte(hexbytes[noparse][[/noparse]0]);
    }

    The javelin uarts have a 255 byte buffer so the pc may send data at will.
    You just need to check for 0xFF (start character) assuming the remaining
    bytes in the message do never have the value 0xFF

    while (true) {
    · if (rx.byteAvailable()) {
    ··· int c = rx.receiveByte();
    ··· if (c == 0xFF) {
    ····· //start character received.
    ····· //now receive the bytes of the message and process them
    ····· //meaning assemble·bytes into values complying to the message format
    ··· }
    · }
    }

    regards peter
Sign In or Register to comment.