简体   繁体   中英

Sending serial data with python gives an error on windows but not on linux

I'm using python to send data out to my arduino and for some reason under windows it's giving me an error. Below is my code.

import serial 

ser = serial.Serial("COM3")

ser.write('1')

Here is the error.

  File "C:\\Python25\\lib\\site-packages\\serial\\serialwin32.py", 

line 255, in write raise SerialException("WriteFile failed (%s)" % ctypes.WinError()) serial.serialutil.SerialException: WriteFile failed ([Error 9] The handle is invalid.)

Any idea why it's giving me this?

Are you sure COM3 is a valid serial port on your Windows box? Can you open it with HyperTerminal and send stuff to it?

If it is, another thing to try is to replace it with the fully qualified name, for example:

port = "\\\\.\\COM3"
ser = serial.Serial(port, 38400)

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