How To : store bytes in an int array by USB Serial

*** Platform information:**

  • Hardware: Raspberry Pi 2 B+ and Arduino Mega2560
  • openHAB version: openHAB2

*** Issue of the topic: please be detailed explaining your issue**
Hello. I’m newbies.
I use Arduino and Raspberry. And I connected by serial communication(ttyUSB0).
Arduino sends several bytes of data. like this 90 165 1 1 0 1 …
(there is no space, “;” and “,”)
I want to store bytes in an int array. and i want to check the saved value.
I saw “header1 match”, but not “header2 match”
I did a search last week. But i could not find the answer.
Please help me T.T

*** Please post configurations (if applicable):**

  • Items configuration related to the issue

     String  TestLogData         "Log Data [%s]"    {serial="/dev/ttyUSB0@9600"}
    
  • Rules code related to the issue

     rule "Received Log Data"
     when
             Item TestLogData received update
     then
    
             var List<Integer> LogData = TestLogData.state.toString.getBytes()
             var Number header1
             var Number header2
    
             header1 = LogData.get(0)
             header2 = LogData.get(1)
    
             if (header1 == 0x5A)
             {
                    logInfo("Debug", "header1 match")
             }
    
             if (header2 == 0xA5)
             {
                    logInfo("Debug", "header2 match")
             }
    
  • If logs where generated please post these here using code fences:

What exactly does is the serial string?

In your example, there IS a space.

Do this:

rule "Received Log Data"
when
    Item TestLogData received update
then
    var List<Integer> LogData = TestLogData.state.toString.getBytes()
    var Number header1
    var Number header2

    logInfo("TestLogData", TestLogData.toString)
    header1 = LogData.get(0)
    header2 = LogData.get(1)

    if (header1 == 0x5A) {
        logInfo("Debug", "header1 match")
    }

    if (header2 == 0xA5) {
        logInfo("Debug", "header2 match")
    }
end

I added a logInfo to print the whole string
What does the log show when you receive data?

Thank you for your help.
I wanted to make the example look good.


Arduino send serial data.
0x5A 0xA5 …

But Log data something wrong…t.t

What did I do wrong?

Only the first byte is recognised properly, have you checked that your receiving serial port is configured properly especially the parity and end bits?

Could you tell me how to check?
I just checked “console=serial0, 9600” in “cmdline.txt” file.

and when i change the code
TestLogData.state.toString.getBytes()
-> TestLogData.state.toString.getBytes(“ASCII”)
Log1

only the second data is abnormal. 165 is displayed as 63.
so i think…are they displayed as different values beyond 128??

what do you think???

Beyond 128 it could be that you byte is interpreted as a negative number.
It’s a very long time ago I have done this kind for bit crunching

1 Like

Negative numbers are to be expected when handling bytes > 128
Old post which maybe has relevant handling methods