Why does the following mouse click code results in Windows error 997?
Question: I am doing a mini project where a switch press results in a mouse click or in
other words the switch acts as an alternative to my left mouse button.

My code is

    
    
    import pyautogui , time , serial
    
    port = serial.Serial('COM3',9600)
    flag=0
    
    while 1:
        reading=port.read()
        x=ord(reading)
        if x==1 and flag==0:
            flag=1
            pyautogui.mouseDown()
        elif x==2:
            flag=0
            pyautogui.mouseUp()
    

The serial transmission is such that it receives a

> 0 - Switch not pressed  
>  1 - Switch pressed  
>  2 - switch just left

A 2 is always transmitted in between a 1 and a 0.

My problem is that when I run this code the following error occurs after one
mouse click.

> Traceback (most recent call last):
>
> File "C:\Python27\LeftClick.py", line 12, in pyautogui.mouseUp()
>
> File "C:\Python27\lib\site-packages\pyautogui__init__.py", line 234, in
> mouseUp
>
> platformModule._mouseUp(x, y, 'left')
>
> File "C:\Python27\lib\site-packages\pyautogui_pyautogui_win.py", line 415,
> in _mouseUp
>
> _sendMouseEvent(MOUSEEVENTF_LEFTUP, x, y)
>
> File "C:\Python27\lib\site-packages\pyautogui_pyautogui_win.py", line 480,
> in _sendMouseEvent raise ctypes.WinError()
>
> WindowsError: [Error 997] Overlapped I/O operation is in progress.


Answer: In the comments above, you say that if you take serial out, the program works
as expected, if I read it correctly. You also say that mouse movement works OK
on COM3, but if removing the serial comms fixes the issue, then I'd say that's
a fairly strong indication that COM3 may be the culprit, it should certainly
be investigated.

My suggestion: Use a different COM port for your serial device. You can change
the number used by going into:

`Device Manager > Ports (COM & LPT) > Right Click your device > Properties >
Port Settings > Advanced > COM Port Number`

