Code:
//---------------------------------------------------------------------------
DWORD WINAPI Datenthread( LPVOID lpParam )
{
while(threading)
{
ReadFile(hComm, buffer, 10, &dwBytesRead, NULL);
WaitForSingleObject(hComm,INFINITE);
GetOverlappedResult(hComm,NULL,&dwBytesRead,FALSE);
Form2->Memo1->Text=dwBytesRead;
}
return 0;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
if(active==false&&ComboBox1->ItemIndex!=-1&&ComboBox2->ItemIndex!=-1)
{
DCB dcbCommPort;
hComm = CreateFile(com,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED, //<-- Da will es nicht so ganz...
NULL);
if(hComm == INVALID_HANDLE_VALUE) Application->MessageBox(" !! Bitte anderen Port wählen !! \n\n Der gewählte Port wird schon\nvon einem anderem Programm\n benutzt oder existiert nicht."," .........::: Portfehler :::.........",MB_OK);
else
{
Form2->Visible=true;
threading=true;
hThread=CreateThread(NULL,0,Datenthread,&dwThrdParam,0,&dwThreadId);
active=true;
Button1->Enabled=false;
Button2->Enabled=true;
StatusBar1->Panels->Items[3]->Text="Status: geöffnet";
}
GetCommTimeouts(hComm,&ctmoOld);
ctmoNew.ReadTotalTimeoutConstant = 100;
ctmoNew.ReadTotalTimeoutMultiplier = 0;
ctmoNew.WriteTotalTimeoutMultiplier = 0;
ctmoNew.WriteTotalTimeoutConstant = 0;
SetCommTimeouts(hComm, &ctmoNew);
dcbCommPort.DCBlength = sizeof(DCB);
GetCommState(hComm, &dcbCommPort);
switch(ComboBox2->ItemIndex)
{
case 0: BuildCommDCB("110,N,8,1", &dcbCommPort); break;
case 1: BuildCommDCB("300,N,8,1", &dcbCommPort); break;
case 2: BuildCommDCB("1200,N,8,1", &dcbCommPort); break;
case 3: BuildCommDCB("2400,N,8,1", &dcbCommPort); break;
case 4: BuildCommDCB("4800,N,8,1", &dcbCommPort); break;
case 5: BuildCommDCB("9600,N,8,1", &dcbCommPort); break;
case 6: BuildCommDCB("19200,N,8,1", &dcbCommPort); break;
case 7: BuildCommDCB("38400,N,8,1", &dcbCommPort); break;
case 8: BuildCommDCB("57600,N,8,1", &dcbCommPort); break;
case 9: BuildCommDCB("115200,N,8,1", &dcbCommPort); break;
case 10: BuildCommDCB("230400,N,8,1", &dcbCommPort); break;
case 11: BuildCommDCB("460800,N,8,1", &dcbCommPort); break;
case 12: BuildCommDCB("921600,N,8,1", &dcbCommPort); break;
default: Application->Terminate(); break;
}
SetCommState(hComm, &dcbCommPort);
}
}
Lesezeichen