Kaz
Kaz

Reputation: 11

Read and write to same serial port with Windows

Is it possible to write and then read the same serial port in one python file? Or with 2 different threads? I've tried it both ways. With 2 different threads, I get "access denied". In the same file, I write, and it shows the #bytes I've written, but when I read, I get 0 bytes. Are the messages stored in a buffer until they are read? Here is the code I'm trying from the same file:

# rwSerialPort.py

import sys, time
import serial.tools.list_ports as portlist
import serial

ports = list( portlist.comports() )
for p in ports:
  print(p)


# This will hold received UART data
data = ""
stopMessage = "STOP\n"

messages = ["This is the first message sent to the Serial Port\n",
            "This is the second message sent to the Serial Port\n",
            "This is the third message sent to the Serial Port\n",
            "STOP\n"]


# Set up serial port for read
serialPort = serial.Serial( port="COM3", baudrate=9600, bytesize=8, timeout=2, stopbits=serial.STOPBITS_ONE )

print( 'Starting Serial Port Send' )

for msg in messages:

    serialPort.write( msg.encode() )
    print('Sent Serial Port: ', msg, '  #bytes: ', len(msg) )
    time.sleep(.5)
    serialPort.rts = False
    serialPort.dtr = False
    data = serialPort.readline()
    #data = serialPort.read(size=50)
    print('Serial Port Received #bytes: ', len(data) )
    print( data.decode() )

print( 'Finished sending messages, now read them' )

while True:

    if serialPort.in_waiting > 0:
        
        # Read data until hit a carriage return / new line
        data = serialPort.readline()
        
        try:
            print('Serial Port Received #bytes: ', len(data) )
            print( data.decode("ASCII") )
            
            if data.decode("ASCII") == stopMessage:
                print('Closing Serial Port')
                serialPort.close()
                break
                
        except:
            print('Unable to print received serial data')
        

print('Closing Serial Port Send')
serialPort.close()


if __name__ == '__main__':
    rwSerialPort()

I've tried with both readline() and read(size=#). I don't get anything back. The loop after the first read/writes is what I was originally using to read back. Nothing works. I'm on a windows 10 laptop with only one serial port, COM3. Is it not possible to write then read back? I'm not connected to any hardware. I've googled and googled and haven't found answers. Thanks for any help!

Upvotes: 1

Views: 2890

Answers (2)

port
port

Reputation: 1

After plugging in an Arduino to USB port I add:

for p in ports:
  print(p)
if ((p[1][-3]).isnumeric()):
    portCOM="COM"+p[1][-3]+p[1][-2]
else:
    portCOM="COM"+p[1][-2]
print(portCOM)

serialPort = serial.Serial( port=portCOM

Upvotes: 0

HWW
HWW

Reputation: 124

Unless you have a loopback plugged in to the port, all data you write to the port sent out to nowhere, the loopback plug connects the port's TX to it's RX, and only then you'll be able to see the data you write out.

Upvotes: 3

Related Questions