简体   繁体   中英

Python and arduino Serial Communication error

Python3 Code:

import serial

arduinoData = serial.Serial(4, 9600)

while(1):
    if (arduinoData.inWaiting()>0):
         myData=  arduinoData.readline()
         print (myData)
         arduinoData.open()

Error:

Traceback (most recent call last):
  File "E:/Scripts/Examples/arduinopython.py", line 3, in <module>
    arduinoData = serial.Serial(4, 9600)
  File "C:\Users\Om\AppData\Local\Programs\Python\Python36-32\lib\site-packages\serial\serialwin32.py", line 31, in __init__
    SerialBase.__init__(self, *args, **kwargs)
  File "C:\Users\Om\AppData\Local\Programs\Python\Python36-32\lib\site-packages\serial\serialutil.py", line 171, in __init__
    self.open()
  File "C:\Users\Om\AppData\Local\Programs\Python\Python36-32\lib\site-packages\serial\serialwin32.py", line 47, in open
    if port.upper().startswith('COM') and int(port[3:]) > 8:
AttributeError: 'int' object has no attribute 'upper'

Can anyone help in finding where I am going wrong ?

serial.Serial constructor need this:

arduinoData = serial.Serial("COM4", 9600)

com_number is string and not int.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM