Invalid Port Number 8002

Serial Computer PortImage via WikipediaIf you are developing codes using VB6 and is facing this error, this may help you.

First of all, in a typical code, it looks like this.

    MSComm1.CommPort = 26

    MSComm1.Settings = "57600,N,8,1"

    MSComm1.RThreshold = 1

    MSComm1.SThreshold = 1

    MSComm1.DTREnable = True

    MSComm1.RTSEnable = True

    MSComm1.PortOpen = True

Thus, if you are able to find out which line the error is triggered, you may be better off.

If it is triggered from the following line.

MSComm1.CommPort = 26

This is because using 'MS Comm Control 6.0' activeX control, you can only set a maximum port number of 16. Thus, the example above with port number = 26 is considered invalid.

If it is triggered from the following line.

MSComm1.PortOpen = True

Then it means that the port is not existence.

Ok, so the bigger problem is that sometimes you may be in a situation where the port number has to be higher than 16, especially when you are dealing with serial-USB connection (the COM Port number is automatically assigned).

How to solve it ?

Two solutions:

1. Using Win32 API instead of ActiveX object to make serial port connection.

And thanks to this forum, and check out this forum for exact solution.

Mainly it involves usage of createFile API.

The API creates or opens a file or I/O device.

The most important parameter is the 'lpFileName'. Which is the exact name of the resource to be opened in Windows system. For instance, if COM Port 1, the name is 'COM1' as listed in 'Control Panel/System/Device Manager/'

Check out a sample code by Riaan Aspeling (from planet source code) is promising, but it doesn't make use of events. It is using timer.

2. Manually change the port to a number less than or equal to 16.

It can be done, only if the situation permits.

Then you can keep using ActiveX control (MS Comm Control 6.0).
Zemanta Pixie