Respectech
Respectech

Reputation: 454

Reading serial port in Rebol3

I am using Rebol3 v3.0.99.4.20 that has both the /View and serial functionality.

I am opening a port with:

ser: open serial://ttyUSB0/9600

Then, I set up my asynchronous handler:

ser/awake: func [event /local p][
    p: event/port
    switch event/type [
        lookup  [open p]
        connect [write p to-binary request]
        read [
           result: to-string p/data
           close p
           return true
        ]
        wrote [read event/port]
    ]
    false
]

The problem I have now is that I cannot figure out how to read data from the serial port. I always only get back the last command I wrote to the serial port in ser/data.

For example:

>> ser: open serial://ttyUSB0/9600
>> write ser "debug on^/"
>> read ser
== "debug on^/"

That looks OK so far, but this is how the serial device operates using the Linux command, 'screen':

My input:

debug on

The serial device response:

Debug messages enabled.
>

However, I never can read the "Debug messages enabled." text.

>> read ser
== "debug on^/"

>> wait ser
== none

>> read ser 
== "debug on^/"

>> copy ser/data      
== "debug on^/"

Not sure what I'm missing.

In Rebol2, it is much more straightforward, but not asynchronous:

>> system/ports/serial
== [com1 com2 com4]
>> ser: open/no-wait serial://port3/9600/8/none/1
>> insert ser "debug on^/"
>> copy ser
== "debug on^/Debug messages enabled.^/>"
>> copy ser
== ""

A 2nd copy doesn't return anything because the first copy cleared the serial buffer. If data was streaming to the serial port, additional 'copy commands would return additional data from the serial buffer. But it doesn't work this way in Rebol3.

Upvotes: 3

Views: 343

Answers (2)

Graham Chiu
Graham Chiu

Reputation: 4886

The event loop you provided in your question actually should read the data for you. If you want to keep reading data, you should not exit the loop with return true but do another read in the read event. You should process the data inside the event loop.

Upvotes: 0

Respectech
Respectech

Reputation: 454

Found this info in the archives of a chat group:

ser: open serial://ttyUSB0/9600
written: false
ser/awake: func [evt][
  switch evt/type [
    read [
      attempt [print to-string evt/port/data]
      read evt/port
      return true
    ]
    wrote [
      written: true
      return true
    ]
  ]
  false
]
write ser "debug on^/"
while [not written][
  wait [ser 1]
]
read ser
wait [ser 1]

Upvotes: 2

Related Questions