Serial Port Monitor Code

0 views
Skip to first unread message

Kayleigh Telega

unread,
Aug 3, 2024, 5:24:45 PM8/3/24
to layflavholharm

I'm trying to find a basic example, tutorial, or blog post on how to write a printer port monitor. I downloaded the Windows DDK and dug through localmon, but it appears that this sample is much more complex than just the nuts and bolts basics and from my understanding it is a bit different than an OEM port monitor because of how it handles the registry key and port enumeration. Does anyone know of a blog post, tutorial, or even book that walks the reader through the basic code to get one up and going? I've found a few links talking about the conceptual stuff, but nothing that is hands on code.

But my opinion is that a good tutorial in this area is not available on the Internet (I would be glad to find somebody who can show me that I'm wrong). So, when I had to deal with this task I was forced to do it the hard way: I've read carefully the MSDN explanations starting from this point: -us/library/ff561109.aspx. In parallel with reading MSDN I also checked the code in DDK you mentioned and try to understand it. I'm sure this solution could also work for you.

The one thing that regularaly trips people up is EnumPorts, the spooler allocates enough memory for ALL the ports, not just yours. So you need to make sure you fill any strings from the end of the spoolers buffer, don't put them straight after your structures.

The code in RedMon is much easier to read than the localmon example, it's worth looking at before you start. It's quite nice because you can compile it in VS, you don't need to use the DDK to build it.

I have been over that exact same territory for a serial printer. About the best example I found was this article in Dr Dobbs Journal. The good part is that both a serial port driver and the user-space control program are covered and the project can also be used as an example of how to set up Visual Studio to compile a driver. This is also something a little difficult to find information about. The article discusses an old NT style driver, which worked well for me on XP.

Some of the most clearly written and understandable driver code I came across was Mark Russinovich's sample code. Although Microsoft withdrew all of the source when they purchased Sysinternals, some of the best examples can still be found cached here and there.

I am experiencing a problem and I am not sure what has happened. I am using a Lolin(Wemos) D1 R2 Mini board with ESP8266. I have used these boards (6 different ones) for several weeks while building a project. I had very successful results up until today. I needed to modify/verify some code and now they are not connecting to the serial port COM6. I had them connected about 4-5 days ago without issue and today I have issues. I can connect NODEMCU boards without issue. I have rebooted the computer multiple times, uninstalled the Arduino IDE and reinstalled a full release. I tried a Arduino Nightly release edition (5/2/23) all give the same issue. I know the USB port and cable are good. I have also switched physical ports on computer. I am not using USB Hub. I know the code is not the problem for it has been in use for several weeks without issue.
Error message upon trying to upload:

Using the serial monitor, the IDE will scan for a few seconds then come back with the error message:
"Port monitor error command 'open' failed. Invalid serial port. Could not connect to COM6 serial port."

Yeap have done all of the above previously during first installation. Similar to OP, I actually had no issues with my board previously. Was working for the last few weeks and only faced an issue today while trying to upload an updated code.

A fatal esptool.py error occurred: Cannot configure port, something went wrong. Original message: PermissionError(13, 'A device attached to the system is not functioning.', None, 31)esptool.py v3.0
Serial port COM3

Hello team! Thanks for the suggestions thus far. I have tried to Uninstall all com ports, and then uninstall drivers using CH341SER.EXE to install driver directly and for two or three tries, received, timed out waiting on header...then it went back to the OP error messages.

Serial monitor says "Port Monitor Error: Command 'open' failed. Invalid serial port. Could not connect to serial port. ((joking: I wonder if I throw some capt'n crunch at it if it will work - get it serial port))

an update of the CH340 driver to ver. 3.8.2023.2 broke my com port.
did a rollback and now everything works again. had to disable auto updates for drivers, as the new driver version came back with next windows update.

Thanks for wrapping this up so nicely. I have experienced the same problem. The driver v.3.6.2021.12 from your link works fine with both the legacy (1.8.19) and the new (2.1.1) IDE. Interestingly, too old and too new driver versions only work with the legacy version, but not with the new IDE. There was an issue reported about this kind of problems (Include Windows device drives in IDE 2.0.x as in earlier versions Issue #1663 arduino/arduino-ide GitHub) where it is mentioned that for 2.x.x IDE versions, driver dependencies should be handled by "maintainers of platforms for boards" at board installation time. Too bad that Windows updates seem to break this concept...

I am new to Arduino and don't know a lot of things about Arduino and how thing should or shouldn't go, so I am sorry:D For context I am trying to do this project to graduate on high school. I've been having some problems since I started. The last one was with Serial Monitor where should have been written text, but nothing was there. My classmate who knows Arduino pretty well recommended me to reinstall Arduino IDE. After first reinstall, there was some avrdude error after trying to upload and error message saying "Port monitor error: command 'open' failed: Invalid serial port. Could not connect to COM4 serial port." Of course I tried other port or other Arduino but nothing changed. I also tried some things like install driver and stuff like this. So i tried another reinstall. After this I thought something changed because the avrdude error wasn't there but now i realised it showed after upload so I tried it and it is still there and nothing changed.

Before the reinstall was everything alright, except the Serial Monitor, and now when i plug in the USB there is message saying "Connecting to 'Arduino Nano' on 'COM4'..." for like 20 seconds and after that the error message "Port monitor error: command 'open' failed: Invalid serial port. Could not connect to COM4 serial port." shows."

I also tried to use old computer with Windows 10 and everything works. Computer recognise the COM port, can connect and I am able to upload. So it is something with the computer but I don't really know. I am using Windows 11.

I have the 3.8.2023.2 version of the driver reported there as being problematic installed on my computer and I don't have any problems using Serial Monitor and uploading to an Arduino board with the CH340 chip, so it doesn't affect all users universally.

One of the affected users shared the workaround they found, which I interpret as being to "roll back" to a restore point from before the driver update, then disable driver updates to prevent Windows from going back to the problematic version of the driver:

Another user shared their workaround of obtaining an older version of the driver from an alternative source and installing that driver. I don't have any reason to suspect ill intent behind that, but for security reasons I always strongly recommend obtaining drivers exclusively from the device manufacture's website and avoiding unofficial sources for drivers. Unfortunately I didn't find a way to install an older version of the driver obtained from the manufacture's website ( -ic.com/downloads/CH341SER_EXE.html). So I don't have any advice on how you could perform that particular workaround in a best practices manner.

My experience is that the device will be reinstalled automatically by Windows. However, it is maybe possible that the driver removal will actually have been accomplished by the above instructions, in which case it will be necessary to reinstall the driver. If this happens to you, please let me know and I'll provide instructions for installing the driver.

Although I tried uninstalling the driver, I tried again and followed your instructions exactly. Now everything works without any problem. The Arduino connects and can upload. I guess it was because I didn't check the box in the uninstall popup window earlier. Thank you all.

Hey, i found a 2018 driver version someone archived on sparkfun that seems to work great. Just made an account to post this link. I couldn't get it to work even after downloading the latest version of the driver from the manufacturers website... But this driver worked flawlessly and Arduino IDE is happy again

Hi, I'm another newbie and had the same problem as jakubtan, but the suggested procedure has not cured it. The driver reinstalled, but still could not upload. I then tried downloading and installing the driver from the manufacturers site, but still no success. However, I can upload to a Uno fine. I'd be grateful for any further suggestions.

Unfortunattely steps did not work out for me - every time there was the same result. I tryed the steps mentioned in @ptillisch post, tryed installing driver with WCH distributive - nothing worked - out of 2 nanos, one perfectly established connection with IDE, second failed to. So I assumed installing previous version of driver was an option and it worked. As I had a new W11 installation there was no option to rollback, so I:

The functions that LOCALMON exports are incorporated into Localspl.dll, the Local Print Provider. Port monitors are divided into two DLLs: a port monitor server DLL, and a port monitor user interface DLL.

LOCALMON (Localmon.dll) print monitor samples for previous versions of Windows have been archived as outlined below. LOCALMON (Localmon.dll) print monitor sample code is not available in the Windows Driver Kit (WDK) 10 samples on GitHub.

c80f0f1006
Reply all
Reply to author
Forward
0 new messages