473,320 Members | 1,794 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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(&activecomports[i][0], GENERIC_READ,
0,
0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );
if( (HANDLE)fd[0] == INVALID_HANDLE_VALUE ) {
printf("CreateFile error");
exit(EXIT_FAILURE);
}
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 );
}
updateConnection( (HANDLE)fd[0] );
int read1( int fd, UCHAR *buf, int bufSiz ) {
int nBytesRead;
ReadFile( (HANDLE)fd, (LPVOID)buf, (DWORD)bufSiz,
(LPDWORD)&nBytesRead, NULL );
return nBytesRead;

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

Jun 21 '07 #1
3 6986
On 21 Jun, 22:44, kud...@gmail.com 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_ptr - ptr) / sizeof(TCHAR) + 1);
ptr = temp_ptr + 1;
}
Jun 22 '07 #2
On Jun 22, 7:37 am, kelvin.koo...@googlemail.com wrote:
On 21 Jun, 22:44, kud...@gmail.com 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_ptr - 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
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:...
2
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...
13
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...
1
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...
3
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...
5
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...
2
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...
2
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...
11
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.