How to write integers to port using PySerial

2024/9/19 10:07:12

I am trying to write data to the first serial port, COM1, using PySerial.

import serial
ser = serial.Serial(0)
print (ser.name)
ser.baudrate = 56700
ser.write("abcdefg")
ser.close()

ought to work. However, I need to send 28 bytes of integers constantly; in the form

255 255 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000

on loop, with each integer using one byte of data.

Trying:

import serial
ser = serial.Serial(0)
print (ser.name)
ser.baudrate = 56700
while True:ser.write(255 255 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000 000)
ser.close()

raises a Syntax Error.

How can you write integers to a serial port if write only sends strings? How can I ensure that each number is sent as 8-bits?

There is very little in the documentation for the class serial, so any help will be appreciated.

Answer

First of all, writing 123 12 123 123 123 is not a valid Python syntax.

Create a list or a tuple with your integers: values = (1,2,3,4,5)

Now, we need to convert that data into a binary string that represents our values.

So here how we do it

import structvalues = (1,2,3,4,5)string = b''for i in values:string += struct.pack('!B',i)# Now send the string to the serial port

Depending on how many bytes you want to use per number, you need to pack them differently. See the documentation here: https://docs.python.org/3/library/struct.html

https://en.xdnf.cn/q/72632.html

Related Q&A

Pandas sort columns by name

I have the following dataframe, where I would like to sort the columns according to the name. 1 | 13_1 | 13_10| 13_2 | 2 | 3 9 | 31 | 2 | 1 | 3 | 4I am trying to sort the columns in the f…

Series objects are mutable, thus they cannot be hashed error calling to_csv

I have a large Dataframe (5 days with one value per second, several columns) of which Id like to save 2 columns in a csv file with python pandas df.to_csv module.I tried different ways but always get t…

Python client / server question

Im working on a bit of a project in python. I have a client and a server. The server listens for connections and once a connection is received it waits for input from the client. The idea is that the c…

Segmentation fault during import cv on Mac OS

Trying to compile opencv on my Mac from source. I have following CMakeCache.txt: http://pastebin.com/KqPHjBx0I make ccmake .., press c, then g. Than I make sudo make -j8: http://pastebin.com/cJyr1cEdTh…

Python bug - or my stupidity - EOL while scanning string literal

I cannot see a significant difference between the two following lines. Yet the first parses, and the latter, does not.In [5]: n=""" \\"Axis of Awesome\\" """In […

IOPub Error on Google Colaboratory in Jupyter Notebook

I understand that the below command jupyter notebook --NotebookApp.iopub_data_rate_limit=1.0e10 would let me set the data rate. But on Colab, I cannot run this command since the notebook is already ope…

Python code calls C library that create OS threads, which eventually call Python callbacks

If the one and only Python interpreter is in the middle of executing a bytecode when the OS dispatches another thread, which calls a Python callback - what happens? Am I right to be concerned about th…

Django MTMField: limit_choices_to = other_ForeignKeyField_on_same_model?

Ive got a couple django models that look like this:from django.contrib.sites.models import Siteclass Photo(models.Model):title = models.CharField(max_length=100)site = models.ForeignKey(Site)file = mod…

Logging django.request to file instead of console

I am trying to configure my django settings.py to properly use the python logging facility but Ive stumbled upon a rather strange problem:Even after reading the docs, I simply cant find out how to redi…

Coinbase APIerror(id = ) in python

I want to transfer money between my coinbase accounts. Im storing all of my accounts IDs from client.get_accounts()[data][id] and transferring with the code, tx = client.transfer_money(2bbf394c-193b-5b…