modbus协议_PC实现编号:_______________本资料为word版本,可以直接编辑和打印,感谢您的下载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,"串口失败!!”,"SetErr
modbus协议 PC实现 来自淘豆网m.daumloan.com转载请标明出处.