08.08.2019

Python Serial Windows Example

Python Serial Windows Example Average ratng: 5,0/5 7355 reviews
  1. Python Serial Doc

Factory Automation with Python Stories about Robots, Serial Ports, and Barcode Readers - Duration: 30:05. PyCon 2017 11,852 views. Please note that this code will work with both Python 2.7 and Python 3.0. The first line import serial imports the pySerial module so that your program can use it. ComPort = serial.Serial('COM24') opens the serial port named COM24. In Windows, Please give the COM number corresponding to your Serial port or USB to Serial Converter instead of COM24.

Serial

Can someone please show me a full python sample code that uses pyserial, i have the package and am wondering how to send the AT commands and read them back!

Willi MentzelPython Serial Windows Example
11.8k11 gold badges57 silver badges74 bronze badges
Gath

Python Serial Doc

Gath
5823 gold badges11 silver badges14 bronze badges

closed as off-topic by Drew, miken32, Machavity, doelleri, CerebralFartOct 24 '16 at 17:51

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • 'Questions asking us to recommend or find a book, tool, software library, tutorial or other off-site resource are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it.' – Drew, miken32, Machavity, doelleri, CerebralFart
If this question can be reworded to fit the rules in the help center, please edit the question.

4 Answers

FramesterFramester
11.8k36 gold badges108 silver badges175 bronze badges

use https://pythonhosted.org/pyserial/ for more examples

Vatev
6,2751 gold badge23 silver badges34 bronze badges
baydabayda
10.6k6 gold badges33 silver badges48 bronze badges
engasoengaso

I have not used pyserial but based on the API documentation at https://pyserial.readthedocs.io/en/latest/shortintro.html it seems like a very nice interface. It might be worth double-checking the specification for AT commands of the device/radio/whatever you are dealing with.

Specifically, some require some period of silence before and/or after the AT command for it to enter into command mode. Music center for pc 1.0.02 download. I have encountered some which do not like reads of the response without some delay first.

Paul OsbornePaul OsborneWindows python serial port
3,2485 gold badges19 silver badges18 bronze badges

Not the answer you're looking for? Browse other questions tagged pythonmodempyserial or ask your own question.

Next in Idle create a new window and create the below program.
from time import sleep
import serial
ser = serial.Serial('/dev/tty.usbmodem1d11', 9600) # Establish the connection on a specific port
counter = 32 # Below 32 everything in ASCII is gibberish
while True:
counter +=1
ser.write(str(chr(counter))) # Convert the decimal number to ASCII then send it to the Arduino
print ser.readline() # Read the newest output from the Arduino
sleep(.1) # Delay for one tenth of a second
if counter 255:
counter = 32

Two things to keep in mind. To determine what serial port your Arduino is connected to look at the bottom right corner of your Arduino sketch. Whatever that is should be what is in quotes in line 3 of the Python program.
You can also change the baud rate in line 3 of the Python program and line 2 of the Arduino program as long as they stay the same.
Once you run the program it will print out the majority of ASCII characters. By first sending them to the Arduino, which will in turn send it back to the computer that Python then prints out.