本文介绍了尝试检索活动串行/ Com端口列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正试图找到一种方法来填充 计算机上的活动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; } 非常感谢您提供的任何帮助! 解决方案 不在标准C中;标准C对设备一无所知。 有关winAPI的信息,您需要在Windows中询问编程新闻组。 >我的另一个问题是我是否正确使用CreateFile。 CreateFile不是标准C的一部分,所以我们不知道。我好像回想起看到它与winAPI有关,所以 可能是一个适当的地方询问它。 您的代码没有显示fd的定义,也没有关于类型或宏HANDLE的信息 。从语法上看,似乎你在$ *分配后转换了fd [0] *的类型, 似乎是在浪费精力。 那里有很多未定义的东西。看起来很可疑像Windows 具体。 - 那时候我还很年轻,但我也很朦胧。 - Christopher Priest 这些都定义在哪里? (这个新闻组是关于标准C,对于扩展你应该找到 a组关于你的系统)。 将其写入stderr,而不是stdout。另外,添加换行符作为 输出的最后一个字符通常是个好主意。 0表示成功。使用exit(EXIT_FAILURE);相反。 如果(Msg_ID!= EVENT_MSG_TYPE)继续,那么糟糕的是什么。 ? 你不能简单地将其声明为 int read1(HANDLE fd ,LPVOID buf,DWORD bufSiz)? 你确定LPDWORD总是与指向 int的指针相同吗? (如果是的话,你为什么演员?) 不在标准C中;标准C对设备一无所知。 有关winAPI的信息,您需要在Windows中询问编程新闻组。 CreateFile不是标准C的一部分,所以我们不知道。我好像回想起看到它与winAPI一起提到它,所以 可能是一个适当的地方询问它。 您的代码没有显示fd的定义,也没有关于类型或宏HANDLE的信息 。从语法上看,似乎你在$ *分配后转换了fd [0] *的类型, 似乎是在浪费精力。 提前感谢您提供任何帮助! 那里有很多未定义的东西。看起来很可疑像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 acomputer. There may be around 30 on one computer and all connected todifferent Buses but I am looking for one in particular that isemitting packets that I need to monitor.Is there a function in C (maybe using winAPI) that will return thelist of com ports?Another question I have is whether I am using CreateFile correctly.For debugging purposes you can see I have declared "charactivecomports" to contain three com ports that I have on my currentcomputer, although there is nothing connected to them, I expect theprogram 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 comports incorrectly or does my "fd" value get messed up somewhere? Hereis the relevant code I am using:char comPort[20];UCHAR *adrs; //This is an unsigned chartime_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! 解决方案Not in standard C; standard C does not know anything about devices.For information about winAPI, you will need to ask in a Windowsprogramming newsgroup.CreateFile is not part of standard C, so we don''t know. I seemto recall having seen it mentioned in connection witn winAPI, sopossibly that would be an appropriate place to ask about it.Your code showed no definition for fd, and no informationabout the type or macro HANDLE. Syntatically, it appears that youare coverting the type of fd[0] *after* the assignment, whichseems like a waste of effort.Lots of undefined stuff there. Looks suspiciously like Windowsspecific.--I was very young in those days, but I was also rather dim.-- Christopher PriestWhere were all these defined?(This newsgroup is about standard C, for extensions you should finda group about your system).Write that to stderr, not to stdout. Also, adding a newline as thelast character of output is usually a good idea.0 signals success. Use exit(EXIT_FAILURE); instead.What''s so bad with if (Msg_ID != EVENT_MSG_TYPE) continue; ?Can''t you simplily declare it asint read1(HANDLE fd, LPVOID buf, DWORD bufSiz)?And are you sure that LPDWORD is always the same as a pointer toint? (If so, why do you cast?)Not in standard C; standard C does not know anything about devices.For information about winAPI, you will need to ask in a Windowsprogramming newsgroup.CreateFile is not part of standard C, so we don''t know. I seemto recall having seen it mentioned in connection witn winAPI, sopossibly that would be an appropriate place to ask about it.Your code showed no definition for fd, and no informationabout the type or macro HANDLE. Syntatically, it appears that youare coverting the type of fd[0] *after* the assignment, whichseems like a waste of effort.Lots of undefined stuff there. Looks suspiciously like Windowsspecific.-- I was very young in those days, but I was also rather dim. -- Christopher PriestThanks 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 typeand is built with an AutoClose function so I keep fd as an int bothfor the invalid definition, and for a kind of ease while passing itaround to functions (was having trouble passing HANDLE types before). 这篇关于尝试检索活动串行/ Com端口列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 07-30 01:23