views:

57

answers:

1

I'm using the ruby serial port gem. After I open up the port I send the data I want like this.

sp.write [200.chr, 30.chr, 7.chr, 5.chr, 1.chr, 2.chr, 0.chr, 245.chr].to_s

It doesn't work, but if I put it in a loop of around 200 times:

200.times do
  sp.write [200.chr, 30.chr, 7.chr, 5.chr, 1.chr, 2.chr, 0.chr, 245.chr].to_s
end

It works. Any ideas on why this is happening?

+1  A: 

It can be related to some kind of buffer that only gets flushed (ie: sends data) when:

  1. You're explicitly sending a CRLF at the end of your string
  2. It (the buffer) is full

Just for testing, would it be wrong for you to try to add \r\n at the end of the string?

Cesario
Tried adding that to the end, but it doesn't seem to help. I'm monitoring the data that's being sent, and it doesn't look like it's buffering it, b/c it actually sends the string 200 times.
Seth Archer
Looking into it further, it seems that the hardware is getting the data, but it responds with the wrong code for a few times. Then eventually responds correctly. So I don't think it has anything to do with the loop, thanks for the help though.
Seth Archer