Answer the question
In order to leave comments, you need to log in
PySerial. Why does an error occur when writing to the COM port?
Here it was necessary to write a simple script for communication with the modem. I installed the PySerial library, and when I try to write to the port, I get the following bug report:
import serial
>>> ser = serial.Serial('COM4', baudrate=115200, parity=serial.PARITY_NONE, stopbits=serial.STOPBITS_ONE, bytesize=serial.EIGHTBITS, xonxoff=1)
>>> ser.write("at")
Traceback (most recent call last):
File "<pyshell#13>", line 1, in <module>
ser.write("at")
File "C:\Python34\lib\site-packages\serial\serialwin32.py", line 283, in write
data = to_bytes(data)
File "C:\Python34\lib\site-packages\serial\serialutil.py", line 76, in to_bytes
b.append(item) # this one handles int and str for our emulation and ints for Python 3.x
TypeError: an integer is required
ser.open()
>>> ser.isOpen()
True
>>> ser.write('AT')
Traceback (most recent call last):
File "<pyshell#30>", line 1, in <module>
ser.write('AT')
File "C:\Python34\lib\site-packages\serial\serialwin32.py", line 283, in write
data = to_bytes(data)
File "C:\Python34\lib\site-packages\serial\serialutil.py", line 76, in to_bytes
b.append(item) # this one handles int and str for our emulation and ints for Python 3.x
TypeError: an integer is required
>>>
Answer the question
In order to leave comments, you need to log in
Perhaps because the port needs to be opened first. stackoverflow.com/questions/676172/full-examples-o...
Try writing as a byte string:
ser.write(b"at")
In general, he says that he expects a number.
try writing like this:
ser.write("AT".encode('cp1251'))
Also, try to close the port first, and then open it. those. at the beginning of the script ser.close() then ser.open()
Try to set a timeout in the port settings. timeout=5 (for example)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question