In the PSLab Desktop App we use Python for communication between the PC and PSLab device. The PSLab device is connected to PC via USB cable. The power for the hardware device is provided by the host through USB which in this case is a PC. We need well structured methods to establish communication between PC and PSLab device and this is where pySerial module comes in. We will discuss how to communicate efficiently from PC to a device like PSLab itself using pySerial module.
How to read and write data back to PSLab device?
pySerial is a python module which is used to communicate serially with microcontroller devices like Arduino, RaspBerry Pi, PSLab (Pocket Science Lab), etc. Serial data transfer is easier using this module, you just need to open a port and obtain serial object, which provides useful and powerful functionality. Users can send string (which is an array of bytes) or any other data type all data types can be expressed as byte string using struct module in python, read a specific number of bytes or read till some specific character like ‘\n’ is encountered. We are using this module to create custom read and write functions.
How to Install pySerial and obtain serial object for communication?
You can install pySerial using pip by following command
pip install pyserial |
Once it’s installed we can now import it in our python script for use.
Obtain Serial Object
In Linux
>>> import serial >>> ser = serial.Serial(‘/dev/ttyUSB0’) |
In Windows
>>> ser = serial.Serial() >>> ser.baudrate = 19200 >>> ser.port = ‘COM1’ |
Or
>>> ser = serial.Serial(‘COM1’, 19200) |
You can specify other properties like timeout, stopbits, etc to Serial constructor.
Complete list of parameters is available here. Now this “ser” is an object of Serial class that provides all the functionalities through its interface. In PSLab we obtain a serial object and implement custom methods to handle communication which isn’t directly provided by pySerial, for example if we need to implement a function to get the version of the PSLab device connected. Inside the version read function we need to send some bytes to the device in order to obtain the version string from device as a byte response.
What goes under the hood?
We send some sequence of bytes to PSLab device, every sequence of bytes corresponds to a unique function which is already written in device’s firmware. Device recognises the function and responses accordingly.
Let’s look at code to understand it better.
ser.write(struct.Struct(‘B’).pack(11)) # Sends 11 as byte string ser.write(struct.Struct(‘B’).pack(5)) # Sends 5 as bytes string x = ser.readline() # Reads bytes until ‘\n’ is encountered |
To understand packing and unpacking using struct module, you can have a read at my other blog post Packing And Unpacking Data in JAVA in which I discussed packing and unpacking of data as byte strings and touched a bit on How it’s done in Python.
You can specify how many bytes you want to read like shown in code below, which is showing and example for 100 bytes :
x = ser.read(100) |
After your communication is complete you can simply close the port by:
ser.close() |
Based on these basic interface methods more complex functions can be written to handle your specific needs. More details one how to implement custom methods is available at python-communication-library of PSLab which uses pySerial for communication between Client and PSLab device.
An example of custom read function is suppose I want to write a function to read an int from the device. int is of 2 bytes as firmware is written in C, so we read 2 bytes from device and unpack them in client side i.e on PC. For more such custom functions refer packet_handler.py of PSLab python communication library.
def getInt(self): “”” reads two bytes from the serial port and returns an integer after combining them “”” ss = ser.read(2) # reading 2 bytes from serial object try: if len(ss) == 2: return CP.ShortInt.unpack(ss)[0] # unpacking bytes to make int except Exception as ex: self.raiseException(ex, “Communication Error , Function : get_Int”) |