Pyserial testing

7k views Asked by At

I am new to Pyserial and Hardware area. I am trying to run the sample applications given in http://pyserial.sourceforge.net/shortintro.html#opening-serial-ports

import serial

ser = serial.Serial(0)  # open first serial port
print ser.portstr       # check which port was really used
ser.write("hello")      # write a string
ser.close()

I have written this program in a python file and running it. Now if I want to test this application to check if I am sending correct string (eg: Hyperterminal or something) how can I do this. Can anyone guide me?

2

There are 2 answers

1
no_name On

virtual serial port

Use virtual serial port for your test.

For Windows I use com0com and socat for Linux. Then, use Putty for visualization of your send.

1
Simon Marcoux On

Another quick way to test a physical serial port is to take a wire/screwdriver, crocodile clamp or anything that you have in hand and bridge the RX and TX (receive and transmit) together. At that point, everything that you send out will be looped back at you. YOu can receive it afterward by using this code snippet here:

import serial

ser = serial.Serial(0, timeout = 1)  # open first serial port
print ser.portstr       # check which port was really used
ser.write("hello")      # write a string
msg = ser.read("100") #read the content of the input buffer until you get 100 byte or a timeout event
print(msg) #print the content you might need to decode it print(decode(msg))
ser.close()

The key aspect again for this code to work properly is to bridge RX and TX together. A lot of tutorial will show you how to do this.