Reputation: 1763
Operating system : Ubuntu
Serial Ports Using : USB->Serial adapter (prolific PL2303)
I have a sample serial port read and write programs. While running the applications,I am trying to send 4100 bytes ,write program able to do it in single shot.
n = write (s_port,msg,4100);
Here I am checking 'n' value and it is 4100
I*strong text*n receiving end
n = read(s_port,msg,5000);
Above line is in a loop, I am getting not more than 32 bytes in a single read attempt.So more than 128 read attempts(32+32+32 etc...) to get full bytes(4100)
In sending side If it is able to push the whole bytes in a single shot, why not able to receive in single shot?
Upvotes: 0
Views: 2225
Reputation: 677
It's been 9 years. not sure if I can bring the closure to you ( not me actually, rather the human technology advances in the past decade..)... here you go:
Take Python pyserial
for example, by setting up the timeout for read and the intended bytes to read, it will block at reading process until the timeout or intended bytes have been read whichever occurs earlier:
serial.timeout = 30 # in seconds
received = serial.read(4100)
print(received)
In case 30 seconds has past, but it only collects 1024 bytes, then thats what it returns. Otherwise, if it receives all 4100 within 30 seconds, it gives you all.
Upvotes: 0
Reputation: 17583
Serial communications take time for individual bytes to be transferred depending on the baud rate. CPU is much faster in processing the bytes once received.
With the write the serial communication device interface is able to buffer up the bytes to be sent. The read however has to take the bytes as they are received from the other device so there is a time lag.
Here is an wikipedia article on the RS-232 Serial Communications standard.
Compare the serial port to this wikipedia article on the Parallel port.
The parallel port had one wire per bit so that all of the bits are transferred at the same time where as the serial port transferred the bits serially, one at a time. Parallel ports were used for higher speed transfers such as for printers at a time when serial port speeds were pretty low. Serial port speeds have dramatically improved over the years though still no where near USB speeds.
Upvotes: 2
Reputation: 3795
It's the general nature of serial to USB converters and you'll have to deal with data being received in smaller chunks. I thought the PL2303 had a 64 byte buffer but regardless it's well under what you're trying to transfer in a single read.
Also being asynchronous there are no delimeters to specify the start and end of a chunk of data, so the chip / driver has to make a decision on where to break the data stream depending on the on-chip buffer size and other rules they may implement such as timeouts before forwarding the data as a USB packet.
Upvotes: 0