vc串口枚举类

串口枚举类

 

 

一般计算机都带有串口,不同的计算机上所带有的串口数可能不同,比如一台计算机扩展了8个串口设备,或者零时插入一根USB转232的线,系统的串口数目可能发生变化,这种变化可能是固定的,也可能是变化的,在软件上要考虑这种变化,要能够随时探测系统当前究竟有多少个串口,为此写了个串口枚举类。每个串口设备均会在注册表中注册,通过读取注册表中的值就可以知道当前的串口数目和串口设备号等,源码如下:

 

#ifndef __CCOMPORTENUM_H__

 

#define __CCOMPORTENUM_H__

 

 

typedef struct __comport_struct

 

{

 

       int num ;

 

       CString name ;

 

} COMPORT_DESC ;

 

 

class CComPortEnum  

 

{

 

public :

 

       CComPortEnum ();

 

       virtual ~ CComPortEnum ();

 

 

public :

 

       int GetNumber ( CString & csstr );

 

       int EnumAllComPort ( void );                        // 枚举所有的串口,返回总串口数

 

       CString & GetComPortNameFromPortNum ( int num );  // 从串口设备号得到串口名

 

       CString & GetComPortNameFromListNum ( int listnum ); // 从列表选择号得到串口名

 

       int GetPortNumFromComPortName ( CString & name );       // 从串口名得到串口设备号

 

       int GetPortNumFromListNum ( int listnum );                // 从列表选择号得到串口设备号

 

       int GetComPortNumber ( void )                            { return m_nComPortNum ; }

 

public :

 

       int m_nComPortNum ;

 

       CList < COMPORT_DESC , COMPORT_DESC &> m_listComPort ;

 

};

 

 

#endif

 

 

为了篇幅不至于过长,贴出类中的主要函数实现代码.

 

// 枚举所有的串口,返回总串口数

 

int CComPortEnum :: EnumAllComPort ( void )

 

{

 

       int i , listcnt ;

 

       COMPORT_DESC comdesc , * pcomdesc ;;

 

 

       if ( m_listComPort . GetCount () != 0)

 

              m_listComPort . RemoveAll ();

 

 

       HKEY hKey ;

 

       LPCTSTR data_Set = "HARDWARE//DEVICEMAP//SERIALCOMM//" ;
       LONG ret0 = RegOpenKeyEx ( HKEY_LOCAL_MACHINE , data_Set , 0, KEY_READ , & hKey );

 

       if ( ret0 != ERROR_SUCCESS )

 

       {

 

              AfxMessageBox ( " 错误:无法打开有关的 hKEY" );

 

              m_nComPortNum = 0;

 

              return 0;

 

       }

 

 

       DWORD dwIndex = 0;

 

       LONG Status ;

 

       CHAR Name [25];

 

       UCHAR szPortName [80];

 

       DWORD dwName ;

 

       DWORD dwSizeofPortName ;

 

       DWORD Type ;

 

 

       do

 

       {

 

              dwName = sizeof ( Name );

 

              dwSizeofPortName = sizeof ( szPortName );

 

 

              Status = RegEnumValue ( hKey , dwIndex ++, Name , & dwName , NULL , & Type ,

 

                                                        szPortName , & dwSizeofPortName );

 

              if (( Status == ERROR_SUCCESS )||( Status == ERROR_MORE_DATA ))

 

              {

 

                     comdesc . name = ( LPCTSTR ) szPortName ;

 

                     comdesc . num = GetNumber ( comdesc . name );

 

 

                     if ( m_listComPort . GetCount () == 0)                           // 排序插入

 

                            m_listComPort . AddHead ( comdesc );

 

                     else

 

                     {

 

                            listcnt = m_listComPort . GetCount ();

 

                            for ( i =0; i < listcnt ; i ++)

 

                            {

 

                                   pcomdesc = & m_listComPort . GetAt ( m_listComPort . FindIndex ( i ));

 

                                   if ( comdesc . num < pcomdesc -> num )

 

                                   {

 

                                          m_listComPort . InsertBefore ( m_listComPort . FindIndex ( i ), comdesc );

 

                                          goto _insert_out ;

 

                                   }

 

                            }

 

                            m_listComPort . AddTail ( comdesc );

 

                     }

 

_insert_out :

 

                     m_nComPortNum ++;

 

              }

 

       } while (( Status == ERROR_SUCCESS )||( Status == ERROR_MORE_DATA ));

 

       RegCloseKey ( hKey );

 

       return 0;

 

}

 

 

// 从列表选择号得到串口名

 

CString & CComPortEnum :: GetComPortNameFromListNum ( int listnum )

 

{

 

       COMPORT_DESC * comdesc ;

 

       if ( listnum > m_listComPort . GetCount ())

 

              return costName ;

 

 

       comdesc = & m_listComPort . GetAt ( m_listComPort . FindIndex ( listnum ));

 

       if ( comdesc == NULL )

 

              return costName ;

 

       return comdesc -> name ;

 

}

 

 

// 从列表选择号得到串口设备号

 

int CComPortEnum :: GetPortNumFromListNum ( int listnum )

 

{

 

       COMPORT_DESC * comdesc ;

 

       if ( listnum > m_listComPort . GetCount ())

 

              return -1;

 

       comdesc = & m_listComPort . GetAt ( m_listComPort . FindIndex ( listnum ));

 

       if ( comdesc == NULL )

 

              return -1;

 

       return comdesc -> num ;

 

}

 

 

类中串口名和串口设备号归纳为一个数据结构,并存放于链表中,串口名可以输出到 CComBox 中,选择对应的号通过 GetPortNumFromListNum()函数就可以得到串口设备号。

你可能感兴趣的:(vc串口枚举类)