I have connected a PC (Windows2000) to a camera controller and I want
to get image data from the controller.
Two ports are used for this. Port 8500 is for text commands and text
replies.
Port 8501 is used for image data.
When I request image data it streams to the PC via ethernet (8501) in
bitmap format.
I have no problem with the text commands and replies but catching the
binary data confuses me. I would like to get the data in an array and
then write it to a bitmap file. Getting it into an array at this point
has me stuck.
has me stuck.
How much do you know about the protocol used for transmitting the
bitmap data? In my experience when you are reading binary data from a
socket there is usually a length indicator at the beginning to tell
you how much to read. So for example you would read 4 bytes and then
use String#unpack to turn those bytes into a length, which would be
how much you would read next for the rest of the data. For example, I
just wrote this server which serves up a small JPG file:
require ‘socket’
img = nil
File.open(‘tp.jpg’, ‘rb’) do |f|
img = f.read
end
server = TCPServer.new(‘localhost’, 7778)
while (session = server.accept)
session.write([img.length].pack(‘N’))
session.write(img)
end
Then I wrote this client to read it (partially based on your code):
require ‘socket’
image_port = 7778
image_io = TCPSocket.new(‘localhost’, image_port)
len = image_io.read(4).unpack(‘N’)[0]
img = image_io.read(len)
File.open(‘tp2.jpg’, ‘wb’) do |f|
f.write(img)
end
This works fine. I suspect you will need to do something similar.
That was it.
At the beginning of the data was the length indicator (and some other
data). Thanks to you, I was able to capture the image data before the
end of the day Friday and can get on with my code next week.
Thank you.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.