尝试检索活动串行/ Com端口列表 [英] Trying to Retrieve a List of Active Serial/Com Ports

查看:61
本文介绍了尝试检索活动串行/ Com端口列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正试图找到一种方法来填充

计算机上的活动Com端口列表。在一台计算机上可能有大约30个并且所有都连接到

不同的公共汽车但是我正在寻找一个特别是那个我需要监视的发送数据包的
。 />
C中是否有一个函数(可能使用winAPI)将返回

的COM端口列表?


我有另一个问题是否正确使用CreateFile。

出于调试目的,您可以看到我已声明char

activecomports在我当前的

计算机上包含三个com端口,虽然没有任何连接,但我希望

程序能够循环使用它们。

程序一直运行到do {}循环之后的第一个动作(nBuf =

read1 ...),它停止并且不继续,我是否通过了我的com />
端口不正确或我的fd是什么?价值搞砸到某个地方?这里

是我正在使用的相关代码:


char comPort [20];

UCHAR * adrs; //这是一个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");

退出(0);

}

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);

elmT hree = *(buf [0] + i + 3);

adrs = buf [0] + i + 4;

开关(Msg_ID){

默认:

继续;

案例EVENT_MSG_TYPE:

休息;

}}}

} 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);

返回nBytesRead;

}


非常感谢您提供的任何帮助!

解决方案

文章< 11 ***** *****************@i13g2000prf.googlegroups .com> ;,

< ku **** @ gmail.comwrote:
< blockquote class =post_quotes>
>我试图找到一种方法来填充
计算机上的活动Com端口列表。一台计算机上可能有大约30个,并且所有都连接到不同的总线,但我正在寻找一个特别是发出需要监视的数据包。
C中是否有功能(可能使用winAPI)将返回
com端口列表?



不在标准C中;标准C对设备一无所知。

有关winAPI的信息,您需要在Windows中询问
编程新闻组。

< blockquote class =post_quotes>
>我的另一个问题是我是否正确使用CreateFile。



CreateFile不是标准C的一部分,所以我们不知道。我好像回想起看到它与winAPI有关,所以

可能是一个适当的地方询问它。


>出于调试目的,您可以看到我声明了char
activecomports在我当前的计算机上包含三个COM端口,虽然没有任何连接,但我希望
程序能够循环使用它们。


(HANDLE)fd [0] = CreateFile(& activecomports [i] [0],GENERIC_READ,0,
0, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);



您的代码没有显示fd的定义,也没有关于类型或宏HANDLE的信息

。从语法上看,似乎你在$ *分配后转换了fd [0] *的类型,

似乎是在浪费精力。


>非常感谢您提供任何帮助!



那里有很多未定义的东西。看起来很可疑像Windows

具体。


-

那时候我还很年轻,但我也很朦胧。

- Christopher Priest




< ku **** @ gmail.comha scritto nel messaggio新闻:11 ********************** @ i13g2000prf.googlegr oups.com ...


char comPort [20];

UCHAR * adrs; //这是一个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 ++)

{



这些都定义在哪里?

(这个新闻组是关于标准C,对于扩展你应该找到

a组关于你的系统)。


(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");



将其写入stderr,而不是stdout。另外,添加换行符作为

输出的最后一个字符通常是个好主意。


exit(0);



0表示成功。使用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;

开关(Msg_ID){

默认值:

继续;

案例EVENT_MSG_TYPE:

休息;



如果(Msg_ID!= EVENT_MSG_TYPE)继续,那么糟糕的是什么。 ?


}}}

} 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);

返回nBytesRead;

}



你不能简单地将其声明为

int read1(HANDLE fd ,LPVOID buf,DWORD bufSiz)?

你确定LPDWORD总是与指向

int的指针相同吗? (如果是的话,你为什么演员?)


6月21日下午2:09,罗伯... @ ibd.nrc-cnrc.gc.ca(沃尔特) Roberson)

写道:


文章< 1182455915.107661.103 ... @ i13g2000prf.googlegroups .com> ;,


< kud ... @ gmail.comwrote:


我试图找到一种方法来填充活动的Com端口列表在一台

的电脑上。在一台计算机上可能有大约30个并且所有都连接到

不同的公共汽车但是我正在寻找一个特别是那个我需要监视的发送数据包的
。 />
C中是否有一个函数(可能使用winAPI)将返回

的com端口列表?



不在标准C中;标准C对设备一无所知。

有关winAPI的信息,您需要在Windows中询问
编程新闻组。


我的另一个问题是我是否正确使用CreateFile。



CreateFile不是标准C的一部分,所以我们不知道。我好像回想起看到它与winAPI一起提到它,所以

可能是一个适当的地方询问它。


出于调试目的,您可以看到我已声明char

activecomports在我当前的

计算机上包含三个com端口,虽然没有任何连接,但我希望

程序能够循环使用它们。

(HANDLE)fd [0] = CreateFile(& activecomports [i] [0],GENERIC_READ,0,

0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);



您的代码没有显示fd的定义,也没有关于类型或宏HANDLE的信息

。从语法上看,似乎你在$ *分配后转换了fd [0] *的类型,

似乎是在浪费精力。
< blockquote class =post_quotes>
提前感谢您提供任何帮助!



那里有很多未定义的东西。看起来很可疑像Windows

具体。


-

那时候我还很年轻,但我也很朦胧。

- Christopher Priest



感谢您的输入...

fd在同一个定义中document as public:int fd [2] = {-1,-1};

CreateFile返回类型HANDLE,它是另一个winAPI定义的类型

并且是用AutoClose函数所以我将fd保持为int两个

用于无效定义,并且为了一种轻松的方式传递它

到函数(传递HANDLE类型时遇到问题)之前)。


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 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:

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(0);
}
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!

解决方案

In article <11**********************@i13g2000prf.googlegroups .com>,
<ku****@gmail.comwrote:

>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?

Not in standard C; standard C does not know anything about devices.
For information about winAPI, you will need to ask in a Windows
programming newsgroup.

>Another question I have is whether I am using CreateFile correctly.

CreateFile is not part of standard C, so we don''t know. I seem
to recall having seen it mentioned in connection witn winAPI, so
possibly that would be an appropriate place to ask about it.

>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.

(HANDLE)fd[0] = CreateFile(&activecomports[i][0], GENERIC_READ, 0,
0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );

Your code showed no definition for fd, and no information
about the type or macro HANDLE. Syntatically, it appears that you
are coverting the type of fd[0] *after* the assignment, which
seems like a waste of effort.

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

Lots of undefined stuff there. Looks suspiciously like Windows
specific.

--
I was very young in those days, but I was also rather dim.
-- Christopher Priest



<ku****@gmail.comha scritto nel messaggio news:11**********************@i13g2000prf.googlegr oups.com...

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++)
{

Where were all these defined?
(This newsgroup is about standard C, for extensions you should find
a group about your system).

(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");

Write that to stderr, not to stdout. Also, adding a newline as the
last character of output is usually a good idea.

exit(0);

0 signals success. Use exit(EXIT_FAILURE); instead.

}
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;

What''s so bad with if (Msg_ID != EVENT_MSG_TYPE) continue; ?

} } }
} 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;
}

Can''t you simplily declare it as
int read1(HANDLE fd, LPVOID buf, DWORD bufSiz)?
And are you sure that LPDWORD is always the same as a pointer to
int? (If so, why do you cast?)


On Jun 21, 2:09 pm, rober...@ibd.nrc-cnrc.gc.ca (Walter Roberson)
wrote:

In article <1182455915.107661.103...@i13g2000prf.googlegroups .com>,

<kud...@gmail.comwrote:

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?


Not in standard C; standard C does not know anything about devices.
For information about winAPI, you will need to ask in a Windows
programming newsgroup.

Another question I have is whether I am using CreateFile correctly.


CreateFile is not part of standard C, so we don''t know. I seem
to recall having seen it mentioned in connection witn winAPI, so
possibly that would be an appropriate place to ask about it.

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.
(HANDLE)fd[0] = CreateFile(&activecomports[i][0], GENERIC_READ, 0,
0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0 );


Your code showed no definition for fd, and no information
about the type or macro HANDLE. Syntatically, it appears that you
are coverting the type of fd[0] *after* the assignment, which
seems like a waste of effort.

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


Lots of undefined stuff there. Looks suspiciously like Windows
specific.

--
I was very young in those days, but I was also rather dim.
-- Christopher Priest

Thanks for your input...
fd is defined in the same document as a public: int fd[2] = {-1,-1};
CreateFile returns type HANDLE which is another winAPI defined type
and is built with an AutoClose function so I keep fd as an int both
for the invalid definition, and for a kind of ease while passing it
around to functions (was having trouble passing HANDLE types before).


这篇关于尝试检索活动串行/ Com端口列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆