如何知道机器上有几个串口? rt,谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 HANDLE hCom = CreateFile("COM1", 0, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);if(hCom != -1)// com1 is exist int GetComPortNum(){ CString strCom; int nCom = 0; HANDLE hCom; do { strCom.Format("COM%d", nCom+1); hCom = CreateFile("COM3", 0, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); if(INVALID_HANDLE_VALUE == hCom ) break; nCom++; } while(); return nCom; } 有没有API啊,这个枚举也太丑陋了。至少有个回调吧,回个串口名字、月光饱和什么的。 //本机可用串口找注册表void CanUseCom(){ TRegistry *Reg = new TRegistry; TStringList *ts = new TStringList(); // try { Reg->RootKey = HKEY_LOCAL_MACHINE; // False because we do not want to create it if it doesn't exist Reg->OpenKey("\\hardware\\devicemap\\serialcomm",false); Reg->GetValueNames(ts); Memo1->Lines->Add(" 可用通讯端口"); for (int i = 0; i < ts->Count; i++) { Memo1->Lines->Add("通讯端口: " +Reg->ReadString(ts->Strings[i])); ComboBox1->Items->Add(Reg->ReadString(ts->Strings[i])); } ComboBox1->ItemIndex = 0; ComboBox2->ItemIndex = 8; ocm_name = "COM1"; } __finally { Reg->CloseKey(); delete Reg; delete ts; }} int GetComNum(){ CString strCom; int nCom = 0; int count = 0; HANDLE hCom; do { nCom++; strCom.Format("COM%d", nCom); hCom = CreateFile(strCom, 0, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); if(INVALID_HANDLE_VALUE == hCom ) break; count++; } while(1); return count; } 一般用CreateFile试试看能否成功,如同上面的 不欣赏用打开设备来枚举,因为串口是独占性设备,如果有应用程序打开了,用CreateFile是打不开的,我想用枚举注册表下HARDWARE\\DEVICEMAP\\SERIALCOMM最好,下面代码我测试过:CXXXDlg::OnInitDialog(){//...//...CComboBox* pcombo=(CComboBox*)GetDlgItem(IDC_COMCOMBO); if(pcombo==NULL) return FALSE; HKEY hkey; LONG lres; lres=RegOpenKeyEx(HKEY_LOCAL_MACHINE,_T("HARDWARE\\DEVICEMAP\\SERIALCOMM"),NULL,KEY_QUERY_VALUE|KEY_ENUMERATE_SUB_KEYS|KEY_READ,&hkey);if(lres==ERROR_SUCCESS){ TCHAR keyname[MAX_PATH]; TCHAR value[20]; DWORD wcount=MAX_PATH; DWORD vcount=20; DWORD index=0; DWORD regtype=REG_SZ; while(lres==ERROR_SUCCESS)//&&(lres!=ERROR_MORE_DATA)) { wcount=MAX_PATH; vcount=20; lres=RegEnumValue( hkey, // handle to key to query index++, // index of value to query keyname, // value buffer &wcount, // size of value buffer NULL, // reserved ®type, // type buffer (LPBYTE)value, // data buffer &vcount // size of data buffer ); if(lres==ERROR_SUCCESS) if((vcount>0)&&(wcount>0)) pcombo->AddString(value); } RegCloseKey(hkey); }if(pcombo->GetCount()) //这就是串口数,如果注册表是对的话. pcombo->SetCurSel(0);//(-1,_T("COM3"));//...//...} 关于listctrl的问题 VC 下有没有办法知道某个驱动是否安装 身边人都是过客的时候 已经无心了 刚看了作业门,发现C语言还是那么强大。 如何将bmp的数据保存为gif [来者有分]我要开发一个基于HTTP的chat room,请问什么思路最好? 新建项目中的.dep .dsp .ncb .plg .opt .dsw .mak文件内部的秘密? 关于线程 帮帮忙,在线等,高分,怎样用一个指针,指向不同的类 利用MSComm控件怎么实现读取帧的起始符和终止符!急!!! CTreectrl失去焦点后,选中项的颜色仍然保持不变,怎么实现? VC中的“Stact overflow”问题,请高手明示
// com1 is exist
{
CString strCom;
int nCom = 0;
HANDLE hCom;
do {
strCom.Format("COM%d", nCom+1);
hCom = CreateFile("COM3", 0, 0, 0,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if(INVALID_HANDLE_VALUE == hCom )
break;
nCom++;
} while();
return nCom;
}
至少有个回调吧,回个串口名字、月光饱和什么的。
void CanUseCom()
{
TRegistry *Reg = new TRegistry;
TStringList *ts = new TStringList();
//
try
{
Reg->RootKey = HKEY_LOCAL_MACHINE;
// False because we do not want to create it if it doesn't exist
Reg->OpenKey("\\hardware\\devicemap\\serialcomm",false);
Reg->GetValueNames(ts);
Memo1->Lines->Add(" 可用通讯端口");
for (int i = 0; i < ts->Count; i++)
{
Memo1->Lines->Add("通讯端口: " +Reg->ReadString(ts->Strings[i]));
ComboBox1->Items->Add(Reg->ReadString(ts->Strings[i]));
}
ComboBox1->ItemIndex = 0;
ComboBox2->ItemIndex = 8;
ocm_name = "COM1";
}
__finally
{
Reg->CloseKey();
delete Reg;
delete ts;
}
}
{
CString strCom;
int nCom = 0;
int count = 0;
HANDLE hCom;
do {
nCom++;
strCom.Format("COM%d", nCom);
hCom = CreateFile(strCom, 0, 0, 0,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
if(INVALID_HANDLE_VALUE == hCom )
break;
count++;
} while(1); return count;
}
{
//...
//...
CComboBox* pcombo=(CComboBox*)GetDlgItem(IDC_COMCOMBO);
if(pcombo==NULL)
return FALSE; HKEY hkey;
LONG lres;
lres=RegOpenKeyEx(HKEY_LOCAL_MACHINE,_T("HARDWARE\\DEVICEMAP\\SERIALCOMM"),NULL,KEY_QUERY_VALUE|KEY_ENUMERATE_SUB_KEYS|KEY_READ,&hkey);
if(lres==ERROR_SUCCESS)
{
TCHAR keyname[MAX_PATH];
TCHAR value[20];
DWORD wcount=MAX_PATH;
DWORD vcount=20;
DWORD index=0;
DWORD regtype=REG_SZ;
while(lres==ERROR_SUCCESS)//&&(lres!=ERROR_MORE_DATA))
{
wcount=MAX_PATH;
vcount=20;
lres=RegEnumValue(
hkey, // handle to key to query
index++, // index of value to query
keyname, // value buffer
&wcount, // size of value buffer
NULL, // reserved
®type, // type buffer
(LPBYTE)value, // data buffer
&vcount // size of data buffer
);
if(lres==ERROR_SUCCESS)
if((vcount>0)&&(wcount>0))
pcombo->AddString(value);
}
RegCloseKey(hkey);
}
if(pcombo->GetCount()) //这就是串口数,如果注册表是对的话.
pcombo->SetCurSel(0);//(-1,_T("COM3"));
//...
//...
}