modbus协议-PC端的实现我使用C++。首先开一个线程。void__fastcallTForm1::FormCreate(TObject*Sender){m=newTRead232(true);m->FreeOnTerminate=true;}通过一个按钮实现打开/关闭串口void__fastcallTForm1::Button1Click(TObject*Sender){if(Button1->Caption=="打开通讯端口")//按钮名称是"打开通讯端口"?{No;DCBdcb;StringTemp;Temp="COM"+IntToStr(->ItemIndex+1);//TRadioGroup*;No=();hComm=No,GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,0);if(hComm==INVALID_HANDLE_VALUE){MessageBox(0,"打开串口失败!!","SetError",MB_OK);return;}mState(hComm,&dcb);=CBR_9600;=8;=NOPARITY;=ONESTOPBIT;if(!mState(hComm,&dcb)){MessageBox(0,"打通讯端口设置错误!!","SetError",MB_OK);CloseHandle(hComm);return;}Timer1->Enabled=True;Button1->Caption="关闭串口";Shape1->Brush->Color=clRed;//m->Resume();}else{Button1->Caption="打开通讯端口";CloseHandle(hComm);Shape1->Brush->Color=clBlack;Timer1->Enabled=False;m->Terminate();}}接收由线程TRead232完成void__fastcallTRead232::Execute(){SetName();//----Placethreadcodehere----if(!Terminated)Synchronize(ReadData);}//---------------------------------------------------------------------------void__fastcallTRead232::ReadData(){Form1->Caption="Updatedinathread";StringTemp;charinbuff[1024];DWORDnBytesRead,dwEvent,STATcs;if(hComm==INVALID_HANDLE_VALUE){MessageBox(0,"串口失败!!","SetError",MB_OK);return;}mError(hComm,&dwError,&cs);Form1->Edit1->Text=;if(
modbus协议 PC实现 来自淘豆网m.daumloan.com转载请标明出处.