473,800 Members | 2,385 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Trying to Retrieve a List of Active Serial/Com Ports in C

Hi,
I tried posting this in comp.lang.C but need more specific help using
winAPI. I am trying to find a way to populate a list of active Com
ports on a
computer. There may be around 30 on one computer and all connected
to
different Buses but I am looking for one in particular that is
emitting packets that I need to monitor.
Is there a function in C (maybe using winAPI) that will return the
list of com ports?

Another question I have is whether I am using CreateFile correctly.
For debugging purposes you can see I have declared "char
activecomports" to contain three com ports that I have on my current
computer, although there is nothing connected to them, I expect the
program to cycle through them anyway.
The program runs until the first action after the do{} loop (nBuf =
read1...) where it halts and does not continue, am I passing in my
com
ports incorrectly or does my "fd" value get messed up somewhere?
Here
is the relevant code I am using:
int fd[2] = {-1,-1};
char comPort[20];
UCHAR *adrs; //This is an unsigned char
time_t TimeSec = 0;
int i,j,udi,k, nBuf, readAgain=0;
int Msg_ID, length, elmThree;
const char *activecomports[3] = {"COM1", "COM3", "COM4"};
for (i=0; i<4;i++)
{
(HANDLE)fd[0] = CreateFile(&act ivecomports[i][0], GENERIC_READ,
0,
0, OPEN_EXISTING, FILE_ATTRIBUTE_ NORMAL, 0 );
if( (HANDLE)fd[0] == INVALID_HANDLE_ VALUE ) {
printf("CreateF ile error");
exit(EXIT_FAILU RE);
}
do {
nBuf = read1( fd[0], (buf[0]+nData[0]), (RS232BUFSIZ-nData[0]) );
if( nBuf 0 ) {
nData[0] += nBuf;
readAgain = ( nData[0] == RS232BUFSIZ );
while( (i = findNextPacket( TimeSec, buf[0], &strt[0],
&nData[0] )) != -1 ) {
Msg_ID = *(buf[0]+i+1);
length = *(buf[0]+i+2);
elmThree = *(buf[0]+i+3);
adrs = buf[0]+i+4;
switch( Msg_ID ) {
default:
continue;
case EVENT_MSG_TYPE:
break;
} } }
} while( readAgain );
}
updateConnectio n( (HANDLE)fd[0] );
int read1( int fd, UCHAR *buf, int bufSiz ) {
int nBytesRead;
ReadFile( (HANDLE)fd, (LPVOID)buf, (DWORD)bufSiz,
(LPDWORD)&nByte sRead, NULL );
return nBytesRead;

}
Thank you so much in advance for any help you can provide!

Jun 21 '07 #1
3 7014
On 21 Jun, 22:44, kud...@gmail.co m wrote:
Is there a function in C (maybe using winAPI) that will return the
list of com ports?
Try QueryDosDevice, e.g.

TCHAR szDevices[65535];
unsigned long dwChars = QueryDosDevice( NULL, szDevices, 65535);
TCHAR *ptr = szDevices;

while (dwChars)
{
int port;
if (sscanf(ptr, "COM%d", &port) == 1)
{
// Add to list of com ports
}
TCHAR *temp_ptr = strchr(ptr, 0);
dwChars -= (DWORD)((temp_p tr - ptr) / sizeof(TCHAR) + 1);
ptr = temp_ptr + 1;
}
Jun 22 '07 #2
On Jun 22, 7:37 am, kelvin.koo...@g ooglemail.com wrote:
On 21 Jun, 22:44, kud...@gmail.co m wrote:
Is there a function in C (maybe using winAPI) that will return the
list of com ports?

Try QueryDosDevice, e.g.

TCHAR szDevices[65535];
unsigned long dwChars = QueryDosDevice( NULL, szDevices, 65535);
TCHAR *ptr = szDevices;

while (dwChars)
{
int port;
if (sscanf(ptr, "COM%d", &port) == 1)
{
// Add to list of com ports
}
TCHAR *temp_ptr = strchr(ptr, 0);
dwChars -= (DWORD)((temp_p tr - ptr) / sizeof(TCHAR) + 1);
ptr = temp_ptr + 1;
}
That is exactly what I was looking for! Thank you!

Jun 22 '07 #3
Here's another question: how does QueryDosDevice( ) work? The function
you helped me with works just fine but I don't exactly know the
hardware performances that take place. Is QueryDosDevice opening all
of my ports and not closing them because I've been getting invalid fd
handle values after I've called QueryDosDevice and ReadFile hangs up
rather than either working or returning an error.

Jun 25 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
7472
by: M. Raab | last post by:
i have written an application that utilizes serail ports. In order not to have to reinvent code, I decided to use John Hind's sample code that he presented in MSDN magazine last year (Serial Comm: Use P/Invoke to Develop a ..NET Base Class Library for Serial Device Communications John Hind - MSDN Magazine - October 2002) it works fine except for one problem. i cannot address any port higher than COM9. I am using an equinox multi-serial...
2
14573
by: Claire | last post by:
I'm using the following code in an attempt to obtain a list of available serial ports. I expected to retrieve the same values as listed in hardware manager in Control panel but I'm not. I'm being returned COM1 and COM3 when I should be receiving COM1 and COM4. I've also a USB to serial converter and although listed in control panel correctly again my query is not returning this device. What am I doing wrong please? public void...
13
4835
by: Al the programmer | last post by:
I need to access the serial ports on my webserver from an asp.net page. I have no problem accessing the serial ports from a windows form application, but the code doesn't work in asp.net. I have been told it is not possible to access the serial ports from asp.net. The application is used to control custom hardware. The hardware is connected to a PC through serial ports. Our customer wants to control the hardware from a remote...
1
1765
by: David | last post by:
I have written an application in VB.NET 2003 that uses the SAX serial component for RS232 communications with hardware. The program sets up 2 serial ports so that it can talk to 2 different hardware devices simultaneaously. When I run the program on a desktop running Windows 2000 it works fine. When I run it on a Laptop using XP I start having problems. It seems that when the ports are initiated, either one will work but not the
3
2997
by: Tom Brown | last post by:
Hey people, I've written a python app that r/w eight serial ports to control eight devices using eight threads. This all works very nicely in Linux. I even put a GUI on it using PyQt4. Still works nicely. Then I put the app on on a virtual Windows machine running inside of vmware on the same Linux box. Vmware only lets me have four serial ports so I run the app against four serial ports using four threads. The app did not respond...
5
9503
by: LongBow | last post by:
Hello, Is there a way, in .NET, to determine what are the avialable Serial (Communications) Ports on a Windows OS and is there a way to determine that port isn't being use other than attempting to opening the Serial Port and catching the associated exception? Thanks If there isn't a way to determine the Serial Port within .NET I would be willing, I guess, to use a WinAPI is that was the only way.
2
3962
by: joaquimfpinto | last post by:
Dear All, I made an app in c# that uses several serial ports. For the serial ports I use a pnp Sunix board, some with 8 serial ports other with 4 or even 2 serial ports. Whenever I use the development computer I don't have ay problem with my application.
2
4321
by: pauland80 | last post by:
Hello, My soft passively listen to a device sending +- 300 bytes of data each second. After several hours of work, the soft abruptly stops receiving data without any error, (while the device sends properly, of course) and I need to restart it (the python soft) to "reactivate" the ports. I read that when the serial port encounters an error (frame error or so, I imagine?) it stop receiving data until the library function...
11
3783
by: kudruu | last post by:
Hi, I am trying to find a way to populate a list of active Com ports on a computer. There may be around 30 on one computer and all connected to different Buses but I am looking for one in particular that is emitting packets that I need to monitor. Is there a function in C (maybe using winAPI) that will return the list of com ports? Another question I have is whether I am using CreateFile correctly. For debugging purposes you can see I...
0
9690
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9551
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10505
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10253
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7576
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5471
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.