Skip to content

Bi-directional IOStream seems to mix input and output in unhealthy way #39727

@sgaure

Description

@sgaure

Hi,

I posted this on discourse and was advised to post a bug report about this.

The problem is, I have an fpga hooked up to the usb on my Ubuntu-box. The ftdi-driver makes it look like a uart, a serial port, /dev/fpga. It works like this, I send it a short string to start it. I read output for a minute, then send a new short string to stop the output, change some parameters, and restart output, and read for another minute. And so on.

So it looks something like this:

io = open("/dev/fpga",  read=true, write=true)
for i in 1:N
  write(io,"p N=$i "); flush(io) # change parameter
  write(io,"r"); flush(io) # resume
  while <less than a minute passed>
     read!(io,buffer)
     process(buffer)  # mainly a push! on an array
  end
  write(io,"s"); flush(io) # stop
  <spawn some post-processing>
end

Now, the problem is that the fpga has quite some speed, and it manages to write several bytes between the final read! and the stop command at the end of the loop. This is not a problem, these bytes will be ignored by the processing routine.

However, when strace’ing reads and writes it transpires that the string “p N=$i ” at the top of the loop has a few bytes prepended in the (system) write(), and it turns out to be bytes which the fpga has written to me and which I have not yet read (and which I will discard in the loop). These are then written back to the fpga. Occasionally, these bytes will be interpreted by the fpga as a command, and havoc ensues. The serial line is in raw-mode (i.e. with cfmakeraw()), verified by stty, no echo and such stuff.

It looks like the read() and write() shares a buffer. Things work fine when I replace open/read/write with ccall to open/read/write, i.e. when I bypass julia IO.

Discourse thread: https://discourse.julialang.org/t/read-write-on-serial-port-mixes-input-and-output/55486

Metadata

Metadata

Assignees

Labels

ioInvolving the I/O subsystem: libuv, read, write, etc.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions