PySerial seems to have a read() limit - python

I'm trying to send a large stream of data via serial, and using the PySerial library for my purposes. However, the program freezes whenever I need to read more than somewhere between 10,000 and 15,000 bytes.
s = ser.read(10000) works, however s = ser.read(15000) does not.
I've tried flushing the buffer, I've tried reading in one byte at a time with a loop, I've even tried opening and closing the port after calls of less bytes to try and receive all the data I'm sending - but no luck.
Any idea how to receive more data without the program freezing?

Related

(Python) How does one implement a buffer to hold bytes from a socket?

I'm new to using buffers and I've just set up a server and client. When I send data over a socket, my understanding is it is sent as bytes. I'd like to send some arbitrary list of values between 1-4096 by using i.e. _conn.sendall(str(list).encode()). However, I don't know how to handle the data on the client side to put it into a buffer. My first attempt was using the io module and creating a buffer like buffer = io.BytesIO() and then writing to it from the socket.
data = socket.recv(buffer_size)
buffer.write(bytes(data))
However, I'm not sure if this is correct or if the buffer is written to at all. I've tried printing out the buffer by using print(data.getbuffer()) but nothing prints on my terminal not even if I set up an exception handler.

Issue with receiving a byte from ATMEGA2560

I m trying to receive a byte from Atmega2560 at an unexpected time ( using USART ) on my pc. So how do I ensure that i don't miss the byte in my python code ( which has may functions running)
You didn't say what operating system you are using or how the ATmega2560 is connected to the computer, but the drivers in your operating system responsible for receiving the serial data from the ATmega2560 will almost certainly have a buffer for holding incoming bytes, so you don't need to worry about constantly reading from the serial port in your Python program. Just read when you get around to it, and the byte should be waiting for you in the buffer.
It's easy to test that this is the case: send a byte from the AVR, purposely wait a few seconds, then read the byte and make sure it was received correctly.

Pyserial failed to read full line after sending data

I'm developping a script with Pyserial to send data to a microcontroller, the microcontroller then read the data, process them, and send back some debug information to my python script.
My python script was working without any issue when i was just reading the data from the microcontroller. Now that I need to send data to the microcontroller and start reading just after that, the data i'm reading are not complete.
I should receive something like [TAG1],10,11,12,[TAG1],13,14,15\n but sometimes I don't received the beginning of the data but only the end like 1,12,[TAG1],13,14,15\n
I'm basically doing:
serial.write(dataOut)
dataIn = serial.read(sizeOfTheDataToReceive)
The issue does not come from the microcontroller I'm sure of that, if I'm using putty to send/receive my data I always see the full data.
I tried to add some delay in my microcontroller code to send the data 10s after receiving the data from python, but still it's not working everytime.
Do you have any idea what can cause it ? The COM port is opened when the python script start and is closed at the end of the script.
You need to clear your read and write buffers:
serial.flushInput()
serial.flushOutput()
Then read the data byte-wise:
serial.write(dataOut)
time.Sleep(0.3)
s=''
while serial.inWaiting()>0:
b=serial.read(1)
# time.Sleep(0.1)
s += b

Sending images over socket in Python 3

I am trying to send an image (screenshot) over socket from the client to the server. In Python 2 I was able to use the read() and write() function in order to read and write binary data as well as StringIO. But all of them disappeared in Python 3. I was playing around with PIL, but I can't get the test program running.
CLIENT
image = ImageGrab.grab()
s.send(image.tobytes())
I create a screenshot using GrabImage and save it as image. After that I send the image as binary over the socket to the server.
SERVER
data = conn.recv(4194304)
img = Image.frombytes('RGB', (1366, 768), data)
img.save('screenshot.jpg')
However, If I run the script I get an error message:
ValueError: not enough image data
I think I'm missing something decisive, but I can't figure it out.Thank you, chrizator.
It's likely that the call to .recv() is returning before all the data is retrieved; the parameter is a maximum size, not an exact size. You'll need to call .recv() in a loop and append the data until the entire image is received. This implies that you'll need some way to know WHEN the entire data is received - common strategies for this are:
Keep reading until you see some particular terminating character or character sequence. Not directly applicable in this case, since the raw image data could accidentally contain any particular sequence of bytes whatsoever.
Send the length (perhaps as a decimal number with a terminator, or a fixed-size binary value) ahead of the data; keep reading until you've received that many bytes.
Close the socket after sending the data; keep reading until you get a zero-byte result.

PyVISA read closes before transfer has finished

I am writing a code in python to communicate with scopes through pyvisa.
Sometimes happens that during the transfer of data from the scope to the pc via ethernet connection, not all the data are transferred.
I open the connection with the scope as a SOCKET connection, as indicated in the manual:
inst = visa.ResourceManager().open_resource("TCPIP0::<ip_address>::<port>::SOCKET")
Everything runs properly except for data transfer.
I ask for data via the command inst.write('channel1:data?') as reported in the manual and then I read the data with inst.read(). But if I compare the number of points indicated in the data header with the length of the data array I obtain from the read() method I get a different result, not all the data is transferred. I tried to enable termination characters to the read operations and they work, but when I read data I get a warning from VISA saying that the string does not end with any termination character.
Is there a way to tell peeves when stop reading? Is there a way to force the read time to be longer?
Thanks

Categories

Resources