How to get bitmap file via ethernet

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.

####################################
require ‘socket’

ip_address = “xxx.xxx.xxx.xxx”
text_port = 8500
comm_io = TCPSocket.new(ip_address,text_port) # for CCD controller text

image_port = 8501
image_io = TCPSocket.new(ip_address,image_port) # for binarydata

outmsg = ‘my_command’.upcase + “\r”
comm_io.print outmsg

inmsg = comm_io.gets.chomp.split(/,/)

####################################

Any help appreciated.

Harry

On 5/25/06, Harry [email protected] wrote:

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.

Ryan

On 5/26/06, Ryan L. [email protected] wrote:

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.