I’m trying to connect to my phone from my Windows 7 PC using PySerial with the following code:
import wmi
import serial
c = wmi.WMI()
modem = c.query("SELECT * FROM Win32_POTSModem").pop()
ser = serial.Serial(modem.AttachedTo, modem.MaxBaudRateToSerialPort)
try:
ser.write('at \r\n')
print ser.readline()
finally:
ser.close()
But get the following error on the write call:
Traceback (most recent call last):
File "D:\Alasdair\Documents\Python Scripts\Phone Interface\test.py", line 14, in <module>
ser.write('at \r\n')
File "C:\Python26\Lib\site-packages\serial\serialwin32.py", line 255, in write
raise SerialException("WriteFile failed (%s)" % ctypes.WinError())
SerialException: WriteFile failed ([Error 6] The handle is invalid.)
I’ve tried connecting with TeraTerm and that works fine, so it’s not a problem with the connection to the phone itself.
I’ve been searching around for ages trying to find a solution but haven’t come up with anything that works. Any ideas?
I’m on windows 7 64 bit, with python 2.6, and it’s giving me the same error.
When using a similar program using a c library, the same port responds correctly. What happens here? Sounds like a bug in either pyserial or ctypes. Are you using 64 bit too?
the source code for writing in pyserial looks very simple
perhaps a problem with 64 bit ctypes?
Update:
Definitly a 64 bit problem atleast for me. I just installed an x86 version of python (3.1 this time), and it now works fine. Apperantly 64 bit ctypes can only import 64 bits libraries. Sounds very strange not being able to reach operating system libraries though.