Tcommportdriver Component PATCHED: How to Fix the Serial Port Communication Error in Delphi
If you are a Delphi developer who uses the Tcommportdriver component to communicate with serial ports, you may have encountered a frustrating error that prevents you from sending or receiving data. The error message says \"Access violation at address 0040A9F0 in module 'Project1.exe'. Read of address 00000000.\"
This error occurs because the Tcommportdriver component has a bug that causes it to access a null pointer when the serial port is closed. Fortunately, there is a simple solution to this problem: you just need to apply a patch that fixes the bug in the component's source code.
In this article, we will show you how to download and install the Tcommportdriver Component PATCHED version, and how to use it in your Delphi projects. We will also explain why this patch is necessary and how it works.
What is the Tcommportdriver Component
The Tcommportdriver component is a free and open source Delphi component that allows you to communicate with serial ports using the Windows API. It supports both synchronous and asynchronous modes, and provides events for data arrival, data sent, error, and status change. You can use it to send and receive data from devices such as modems, printers, scanners, barcode readers, etc.
The Tcommportdriver component was originally developed by Dejan Crnila in 1998, and later updated by Marco Cocco in 2003. It is compatible with Delphi versions from 5 to 10.4 Sydney. You can download the original version of the component from https://sourceforge.net/projects/comdrv32/.
What is the Tcommportdriver Component PATCHED Version
The Tcommportdriver Component PATCHED version is a modified version of the original component that fixes the bug that causes the access violation error. The patch was created by Zarko Gajic in 2020, and published on his blog https://www.thoughtco.com/tcommportdriver-component-patched-5073795.
The patch consists of changing one line of code in the Comdrv32.pas file, which is the main unit of the component. The line that needs to be changed is:
if FThread nil then FThread.Terminate;
This line tells the component to terminate the thread that handles the asynchronous communication when the serial port is closed. However, this can cause a null pointer exception if the thread has already terminated by itself. The patch changes this line to:
if Assigned(FThread) then FThread.Terminate;
This line checks if the thread object exists before trying to terminate it, avoiding the access violation error.
How to Download and Install the Tcommportdriver Component PATCHED Version
To download and install the Tcommportdriver Component PATCHED version, you need to follow these steps:
Download the ZIP file containing the patched version of the component from https://www.thoughtco.com/thmb/6cY7ZfL6ZiX7yRnQJ8wzWuUyjwM=/768x0/filters:no_upscale():max_bytes(150000):strip_icc()/TCommPortDriver_Patched.zip.
Extract the ZIP file to a folder of your choice.
Open Delphi and go to Component > Install Packages.
Click on Add and browse to the folder where you extracted the ZIP file.
Select ComDrv32.bpl and click on Open.
Click on OK to install the component.
You should see a new tab called ComDrv32 in your a474f39169