# how to read and write data with pyserial at same time

How can i make pyserial read and write at same time or at same program? Right now i only know how to either write or to read but not both simultaneiously. So how can i do it? I tried this simple code:

int incomingByte = 0;

void setup() {

Serial.begin(9600); // opens serial port, sets data rate to 9600 bps

}

void loop() {

Serial.println(incomingByte);

}


and python program is:

import serial
ser = serial.Serial('/dev/ttyACM0',9600)
ser.write("333")
ser.close()
ser.open()
print data
ser.close()


It's not entirely clear what you mean by "write and read at the same time" because that can refer to different contexts. It can be confusing because people tend to mix the meanings of full-duplex/half-duplex when switching back-and-forth in discussion of the hardware and software layers.

True full-duplex serial communications requires hardware level support. Separately, to make use of full-duplex communications, your serial library and program itself must be written to support full-duplex.

Most simpler two-way serial devices will only respond after receiving a command. For example, you send a command (write), and then expect a response (read). This is sometimes referred to as half-duplex and this is what your current program example is doing. However, you shouldn't be closing and reopening the serial port.

If the serial device you are working with is such that you can write at any time and you can independently expect to receive something from the device at any time, then you are working with a full duplex serial device. In that case, your options are either multi-threading or doing writing/reading sequentially without the reads blocking your program indefinitely.

Multithreaded programming with python is possible on most arduinos. (Again, this gets confusing. Another answer here indicated that arduinos do not support multithreading. That is true for most arduinos at the hardware level. But there are ways to simulate multithreading at the software level, and python does this.) A multi-threaded solution to your problem can get complex. Here are some examples though:

If multi-threading is not available to you, then you can use a single thread where the writes are before reads, and the reads are done only if bytes are available or with a short timeout. Psuedo program loop:

1. Is there data to write? If so, write it.