mod-bus的收发程序:
void CComm_testDlg::OnButtonOpen()
{
// TODO: Add your control notification handler code
char temp[80];
int int_temp;
DWORD dwError;
CComm_testDlg *errDlg;
COMMTIMEOUTS timeouts;
CWnd *EditTxt=GetDlgItem(IDC_BUTTON_OPEN);
EditTxt->GetWindowText((LPTSTR)temp,79);
int_temp=strcmp(temp,"打开端口");
if(!int_temp)
{
errDlg=this;
hCom=CreateFile("COM4",GENERIC_READ|GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
if(hCom == INVALID_HANDLE_VALUE)
{
dwError=GetLastError( );
strcpy(temp, "COM4 不存在或已被占用!");
errDlg->MessageBox((LPCSTR)temp, "打开错误!",MB_OK|MB_ICONEXCLAMATION);
return;
}
SetupComm(hCom,512,128);
PurgeComm(hCom,PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR);
//COMMTIMEOUTS CommTimeOuts;
//CommTimeOuts
DCB dcb;
GetCommState(hCom,&dcb);
dcb.BaudRate=4800;
dcb.ByteSize=8;dcb.Parity=NOPARITY;
dcb.StopBits=TWOSTOPBITS;dcb.fBinary=TRUE;dcb.fParity=FALSE;
dcb.fRtsControl=RTS_CONTROL_DISABLE;
dcb.fNull =FALSE;
SetCommState(hCom,&dcb);
timeouts.ReadIntervalTimeout =1000;
timeouts.ReadTotalTimeoutConstant =1000;
timeouts.ReadTotalTimeoutMultiplier =11;
timeouts.WriteTotalTimeoutConstant =10;
timeouts.WriteTotalTimeoutMultiplier =3;
SetCommTimeouts(hCom,&timeouts);
EditTxt->SetWindowText("关闭端口");
}else
{
EditTxt->SetWindowText("打开端口");
if(hCom!=INVALID_HANDLE_VALUE) CloseHandle(hCom);
hCom=INVALID_HANDLE_VALUE;
}
}
void CComm_testDlg::OnButtonS()
{
// TODO: Add your control notification handler code here
char str_temp[80];int i;
unsigned char send_buffer[200];
CString add;
int addr_s,addr_e,function,data_addr,length;
unsigned long written;
CComm_testDlg* errDlg;
errDlg=this;
if(hCom==INVALID_HANDLE_VALUE)
{
strcpy(str_temp,"端口没有打开!");
errDlg->MessageBox((LPCTSTR)str_temp,"串口操作错误",MB_OK|MB_ICONEXCLAMATION);
return;
}
UpdateData(TRUE);
addr_s=string_int(m_addr_start,NULL);
addr_e=string_int(m_addr_end,NULL);
function=string_int(m_function,NULL);
data_addr=string_int(m_data_addr,NULL);
length=string_int(m_length,NULL);
for(i=addr_s;iMessageBox ((LPCTSTR)str_temp,"发送错误",MB_OK|MB_ICONEXCLAMATION);
return;
}
ReadFile(hCom,send_buffer,20,&written,NULL);
unsigned int j;
CWnd *h_temp=GetDlgItem(IDC_EDIT_R);
// LPRECT lprect;
// h_temp->GetWindowRect (lprect);
for(j=0;jSetWindowText (m_receive);
UpdateData();
}
}
|