D
D
Dmitry2015-08-10 16:32:38
Python
Dmitry, 2015-08-10 16:32:38

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
>>>

What could I have done wrong?
Windiws 8.1 system, Python 3.4, PySerial 2.6. In the terminal, the port is perfectly opened, written and read.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
anelyubin, 2015-08-10
@Beser4

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")

S
slepowl, 2015-08-11
@slepowl

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 question

Ask a Question

731 491 924 answers to any question