mhammond / pywin32

Python for Windows (pywin32) Extensions
4.97k stars 791 forks source link

serial communications device using parity #1357

Open kooper99 opened 5 years ago

kooper99 commented 5 years ago

The parity bit is not transmitted over the wire (checked with scope). I reduced the demo code form the win32comport_demo.py to:

from win32file import * # The base COM port and file IO functions.
from win32event import * # We use events and the WaitFor[Multiple]Objects functions.
import win32con # constants.
import msvcrt # For the getch() function.

import threading
import sys
import time

port = "\\\\.\\COM28"

handle = CreateFile(port,
                   win32con.GENERIC_READ | win32con.GENERIC_WRITE,
                   0, # exclusive access
                   None, # no security
                   win32con.OPEN_EXISTING,
                   win32con.FILE_ATTRIBUTE_NORMAL | win32con.FILE_FLAG_OVERLAPPED,
                   None)

# Tell the port we want a notification on each char.
SetCommMask(handle, EV_RXCHAR)
# Setup a 4k buffer
SetupComm(handle, 4096, 4096)
# Remove anything that was there
PurgeComm(handle, PURGE_TXABORT | PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR )
# Setup for overlapped IO.
timeouts = 0xFFFFFFFF, 0, 1000, 0, 1000
SetCommTimeouts(handle, timeouts)
# Setup the connection info.
dcb = GetCommState( handle )
dcb.BaudRate = CBR_9600
dcb.ByteSize = 8
dcb.Parity = EVENPARITY
dcb.StopBits = ONESTOPBIT
SetCommState(handle, dcb)

for i in range(0xE0,0xFF):
    packet = bytearray()
    packet.append(i)

    overlapped = OVERLAPPED()
    overlapped.hEvent = CreateEvent(None, 1, 0, None)

    WriteFile(handle, packet, overlapped)    

    time.sleep(.61)

Python: 3.6

mhammond commented 5 years ago

pywin32 is a thin wrapper around the win32 API. I suspect that if you ported that same example to c++ you would have the same issues, and that it's probably just not using the API "correctly". I'm certainly not in a position to diagnose this.