Trouble sending arbitrary length strings over socket

  • I am working on writing a socket client on the FANUC side to talk to a Python server. I got the example code to work, but I am having issues with getting a random length string to be passed to and from.


    Code
    tmp_str = 'Connect'
    WRITE file_var(tmp_str::126)
    WRITE('Wrote: ',tmp_str,CR)
    
    
    WRITE('Waiting to read from server...')
    READ file_var(tmp_str::126)
    status = IO_STATUS(file_var)
    WRITE('Read status: ',status,CR)


    works if my Python server echoes the data back


    But if I want the response from my server to be "Connect, 1"


    Then the Karel client is stuck on "waiting to read from server..." because it is expecting more bytes. I tried to use BYTES_AHEAD to sniff how many bytes to read, but that function returned 0. However, if I change my READ to


    Code
    READ file_var(tmp_str::9)


    it works fine. From the manual I thought the first format specifier was the number of bytes to read. This appears to be the length of the string. How do I know a prori the length of the input string buffer?

  • You could use SET_FILE_ATR to read up to a terminator character (ATR_EOL), by default it's Carriage Return.
    Then you'll read a variable length string without specifying the number of bytes:


    Code
    READ file_var(tmp_str)
    instead of
    READ file_var(tmp_str::126)


    Check out the reference manual, chapter 7, file IO.

  • That didn't quite work. Here is the code snippet



    I tried commenting out the first file attribute too. That didn't work either.

Advertising from our partners