volkov75
Гость
|
|
« Ответ #4 : 04-05-2007 16:30 » |
|
Параметры hCom = CreateFile(NameCOM,GENERIC_READ | GENERIC_WRITE,NULL,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
Я понимаю, Зачем ты вобще треды сделал? Ты ведь всё-равно не сможешь одновременно читать и писать из/в порт.
Что бы один поток читал другой записывал. Так было организовано в Delphi работа с COM портом. Пример класса работы с COM #include "stdafx.h" #include "SerPort.h" #include <shlwapi.h>
HANDLE hCom; OVERLAPPED ovl; UINT ThreadProc(LPVOID param); bool bThreadstop;
ComPortWin::ComPortWin() { hCom = NULL; NameCOM = "COM1"; ovl.Internal = 0; ovl.InternalHigh = 0; ovl.Offset = 0; ovl.OffsetHigh = 0; ovl.Pointer = 0; } ComPortWin::~ComPortWin() {
} void ComPortWin::GetSerPorts(CArray<CString>* arCOM) { HKEY hKey; long Err; int i = 0; WCHAR szData[80]; CString strTemp; DWORD ErrV,dwType,cbData; Err = RegOpenKey(HKEY_LOCAL_MACHINE,_T("HARDWARE\\DEVICEMAP\\SERIALCOMM"),&hKey); if (Err == ERROR_SUCCESS) { arCOM->RemoveAll(); while(1) { strTemp.Format(_T("\\Device\\Serial%d"),i); ErrV = ::SHGetValue(hKey,_T(""),strTemp,&dwType,&szData,&cbData); if (ErrV == ERROR_SUCCESS) { strTemp = szData; arCOM->Add(strTemp); } else { if (i > 100) break; } i++; } } RegCloseKey(hKey); } bool ComPortWin::Open(CString _NameCOM) { NameCOM = _NameCOM; try { hCom = CreateFile(NameCOM, GENERIC_READ | GENERIC_WRITE, NULL, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hCom == INVALID_HANDLE_VALUE) { throw 0; } } catch(...) { return false; } return true; } bool ComPortWin::Open(int nCom) { NameCOM.Format(_T("COM%d"),nCom); return Open(NameCOM); } bool ComPortWin::SetParamWND(HWND hWnd) { try { if(!GetCommState(hCom,&dcb)) throw 0; COMMCONFIG CC; memcpy(&(CC.dcb),&dcb,sizeof(dcb)); if(!CommConfigDialog(NameCOM,hWnd,&CC)) throw 1; memcpy(&dcb,&(CC.dcb),sizeof(dcb)); if(!SetCommState(hCom,&dcb)) throw 2; } catch(...) { return false; } return true; } bool ComPortWin::GetStatus(LPDCB lpDCB) { try { if(!GetCommState(hCom,&dcb)) throw 0; } catch(...) { return false; } return true; } bool ComPortWin::SetStatus(DCB dcb) { try { if(!SetCommState(hCom,&dcb)) throw 0; } catch(...) { return false; } return true; } bool ComPortWin::SetStatus(CString s_BuildCommDCB) { FillMemory(&dcb,sizeof(dcb),0); dcb.DCBlength = sizeof(dcb); if (!BuildCommDCB(s_BuildCommDCB,&dcb)) { return FALSE; } return TRUE; } void ComPortWin::Close() { try { if(hCom != NULL) { CloseHandle(hCom); hCom = NULL; } } catch(...) {} } bool ComPortWin::Write(LPCVOID pBuffer,unsigned int nBytes) { try { unsigned long dwNumBytesWritten; WriteFile(hCom,pBuffer,nBytes,&dwNumBytesWritten,NULL); // WriteFile(hCom,pBuffer,nBytes,&dwNumBytesWritten,&ovl); } catch(...) { return false; } return true; } BOOL ComPortWin::Purge() { BOOL Tx,Rx; Tx = PurgeComm(hCom,PURGE_TXCLEAR); Rx = PurgeComm(hCom,PURGE_RXCLEAR); return (Tx && Rx); } BOOL ComPortWin::Flush() { return FlushFileBuffers(hCom); }
bool ComPortWin::Read(LPVOID pBuffer,unsigned int nBytes) { try { DWORD dwBytesTransferred; ReadFile (hCom,pBuffer,nBytes,&dwBytesTransferred,NULL); // ReadFile (hCom,pc,1,&dwBytesTransferred,&ovl); } catch(...) { return false; } return true; } bool ComPortWin::Nastr() { try { GetStatus(&dcb); dcb.BaudRate = CBR_9600; dcb.ByteSize = 8; dcb.Parity = ODDPARITY; dcb.StopBits = TWOSTOPBITS; SetStatus(dcb); GetCommTimeouts(hCom,&CommTimeouts); CommTimeouts.ReadIntervalTimeout = 0; CommTimeouts.ReadTotalTimeoutConstant = 0; CommTimeouts.ReadTotalTimeoutMultiplier = 0; CommTimeouts.WriteTotalTimeoutConstant = 0; CommTimeouts.WriteTotalTimeoutMultiplier = 0; SetCommTimeouts(hCom,&CommTimeouts);
// SetCommMask(hCom,EV_RXCHAR); // ovl.hEvent = CreateEvent(NULL,FALSE,FALSE,NULL); } catch(...) { return false; } return true; } //Создание потоковой функции UINT ThreadProc(LPVOID param) { BYTE Buffer[1]; DWORD dwBytesTransferred;
while(!bThreadstop) { ReadFile (hCom,Buffer,sizeof(Buffer),&dwBytesTransferred,NULL); if (dwBytesTransferred) MessageBox(NULL,_T(""),_T(""),MB_OK); MessageBeep(10); Sleep(50); } /* bool bRead; DWORD dwEvtMask; unsigned char pc; HANDLE Handle; OVERLAPPED o;
Handle = m_ComPort.GetHandle(); o = m_ComPort.GetOverlapped(); while(!bThreadstop) { if (WaitCommEvent(Handle,&dwEvtMask,&o)) { if (dwEvtMask & EV_RXCHAR) { m_ComPort.GetChar(&pc); if (pc == '0') {MessageBox(NULL,strTemp,_T(""),MB_OK); strTemp=""; } else strTemp += pc; } Sleep(50); } } */ MessageBox(NULL,_T("Поток закрыт"),_T(""),MB_OK); return 0; }
Пример работы void CProbaDlg::OnBnClickedButton1() { m_ComPort.Open(1); m_ComPort.Nastr(); m_ComPort.SetParamWND(this->m_hWnd); //Запуск потока bThreadstop = false; Thread1 = AfxBeginThread(ThreadProc,NULL); }
void CProbaDlg::OnBnClickedButton2() { bThreadstop = true; m_ComPort.Close(); }
|
|
« Последнее редактирование: 04-05-2007 16:33 от volkov75 »
|
Записан
|
|
|
|