Read a Hex number from serial and convert to GA or I/O

Discussion in 'Pascal Logic Code Examples' started by Urban Intelligence, Feb 10, 2014.

  1. Urban Intelligence

    Urban Intelligence

    Joined:
    Aug 23, 2010
    Messages:
    6
    Likes Received:
    0
    Location:
    melbourne
    Hi,

    Does anyone know if it possible to read a hex value from the serial port and convert to either a Group Address level or A System IO?

    Here is my logic so far...

    I have tried the HexStringToInt function but the value read from the serial port is not a string it's a number? right?

    HEX STRING BEING SENT = AA 1A 55

    Code:
    readSerial(1, ReceivedString,#$55);
    
    if (ReceivedString[1] = #$AA) then
      begin
        ...
        //Set value of 2nd byte in string to a variable
        value := ReceivedString[2];
        //Convert value to GA Level or SystemIO
        [code goes here]
        WriteLn('Recognised Command');
      end
    else
      begin
        WriteLn('Unrecognised Command');
      end;
    I worked out that if I send a valid HEX string I get "Recognised Command" in the logic window and if I send an invalid string I get "Unrecognised Command" in the logic window so I know that the if statement is comparing the value of ReceivedString[1] with #$AA...

    I want to save the "1A" as a group address or system IO level... can be done?
     
    Last edited by a moderator: Feb 10, 2014
    Urban Intelligence, Feb 10, 2014
    #1
  2. Urban Intelligence

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,552
    Likes Received:
    179
    Location:
    Adelaide, Australia
    You need to extract the hex characters you are after before calling the HexStringToInt function

    define a string variable in the Types section:

    hexString: array[0..2] of char;

    then extract the approriate hex characters:

    copy(hexString, receivedString, 4, 2);

    then convert it

    value := hexStringToInt(hexString);
     
    Ashley, Feb 10, 2014
    #2
  3. Urban Intelligence

    Urban Intelligence

    Joined:
    Aug 23, 2010
    Messages:
    6
    Likes Received:
    0
    Location:
    melbourne
    I wrote this as poer your instructions but it seems to have made no difference. I would have thought this would set the value of a system IO tot eh hex value of "BlindInt"??

    Code:
    readSerial(1, ReceivedString,#$55);
    
    if (ReceivedString[1] = #$AA)then
      begin
        copy(hexString, ReceivedString, 4, 2);
        BlindInt := hexStringToInt(hexString);
        SetIntSystemIO("Blind", BlindInt);
        WriteLn('Recognised Command');
      end
    else
      begin
        WriteLn('Unrecognised Command');
      end;
     
    Urban Intelligence, Feb 13, 2014
    #3
  4. Urban Intelligence

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,552
    Likes Received:
    179
    Location:
    Adelaide, Australia
    I think the confusion here might be your idea of a hex string. I think what you have really is a binary string. A hex string is a sequence of ascii characters in the range 0-9 and A-F where each pair of characters represents a single 8 bit byte. In your case it appears you are actually receiving the 8 bit bytes, not the ascii hex equivalent, so you are receiving a 3 byte string. If this is the case, you just extract it directly:

    value := ord(receivedString[2]);
     
    Ashley, Feb 13, 2014
    #4
  5. Urban Intelligence

    Urban Intelligence

    Joined:
    Aug 23, 2010
    Messages:
    6
    Likes Received:
    0
    Location:
    melbourne
    Thanks Ashley

    Nailed it! Works perfect
     
    Urban Intelligence, Feb 13, 2014
    #5
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.