KAREL READ statement not working

  • Hey guys!
    I am trying to communicate to a remote plc via a socket, and have been able to send messages from my robot to the plc, but am having trouble communicating the other direction. I can tell that data has been received by the robot, but when I try to capture it and use it I get an unitialized data warning and the program crashes. Here's the relevant section of code:


    Code
    WHILE i = 0 DO                          -- i is always 0  when I reach this point
    BYTES_AHEAD(output, i, i2)        -- i is set to the number of bytes in the buffer, this loop runs until a message is received; output is the ID for "S3:"; I am able to write to this channel without an issue
                                                         -- and I have printed out the value of i to prove to myself that a message is received, it stays 0 until I send a message from my tcp client, at which point i becomes
                                                         -- the length of my message, including the "\n\r" eol constant I append (so if I send the string "payload\n\r", i becomes 9), then the loop exits
    DELAY 250                                 -- gate my loop
    ENDWHILE
    READ output(msg)                      -- read my input, it used to be "READ output(msg, CR) but that did not work either"   ||   msg is defined as STIRNG[128]
    WRITE(msg, CR)                         -- write the message I received, this is the line where it crashes because it says msg is uninitialized (prior to this segment of code I added "msg = 'some text'" and
                                                     -- "WRITE(msg, CR)", which prints 'some text' without an issue... the KAREL manual says if READ fails it sets the variables it is reading to to [i]uninitialized[/i] but
                                                     -- I did not see anything about how to fix the issue)


    I am not totally sure how the READ statement works or how to debug it, since I don't believe there is a way to get a status message from it as there is for functions such as MSG_CONNECT or BYTES_AHEAD (in my case, BYTES_AHEAD puts a status code in i2, though I currently do not use this value). Does anyone know what I am doing wrong?


    Thanks!

  • Hey guys, I got it!
    I just had to do

    Code
    READ output(msg::i)


    to specify how many bytes I wanted to read. I had thought that READ defaulted to reading a full line or to the end of the buffer (whichever comes first), but it looks like maybe that is not the case? I want to take a further look at that, but this works for now.

Advertising from our partners