Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Full examples of using pySerial package [closed]

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!

like image 302
Gath Avatar asked Mar 24 '09 04:03

Gath


People also ask

What is the use of PySerial?

PySerial is a library which provides support for serial connections ("RS-232") over a variety of different devices: old-style serial ports, Bluetooth dongles, infra-red ports, and so on. It also supports remote serial ports via RFC 2217 (since V2. 5).

Is PySerial full duplex?

Yes serial port hardware is full duplex. Yes, you can use threads to do Rx and Tx at the same time. Alternatively, you can use a single thread loop that does reads with a short timeout and alternates between reading and writing.


2 Answers

Blog post Serial RS232 connections in Python

import time import serial  # configure the serial connections (the parameters differs on the device you are connecting to) ser = serial.Serial(     port='/dev/ttyUSB1',     baudrate=9600,     parity=serial.PARITY_ODD,     stopbits=serial.STOPBITS_TWO,     bytesize=serial.SEVENBITS )  ser.isOpen()  print 'Enter your commands below.\r\nInsert "exit" to leave the application.'  input=1 while 1 :     # get keyboard input     input = raw_input(">> ")         # Python 3 users         # input = input(">> ")     if input == 'exit':         ser.close()         exit()     else:         # send the character to the device         # (note that I happend a \r\n carriage return and line feed to the characters - this is requested by my device)         ser.write(input + '\r\n')         out = ''         # let's wait one second before reading output (let's give device time to answer)         time.sleep(1)         while ser.inWaiting() > 0:             out += ser.read(1)                      if out != '':             print ">>" + out 
like image 140
Framester Avatar answered Nov 11 '22 18:11

Framester


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()             # close port 

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

like image 34
bayda Avatar answered Nov 11 '22 18:11

bayda