MFC串口通信
这是我写的代码,帮忙看下为什么通信不成功?客户端:voidCDeviceDlg::OnOK(){//TODO:AddextravalidationhereHANDLEhC...
这是我写的代码,帮忙看下为什么通信不成功?
客户端:
void CDeviceDlg::OnOK()
{
// TODO: Add extra validation here
HANDLE hCom;
hCom =CreateFile("COM4",GENERIC_WRITE|GENERIC_READ,NULL,0,OPEN_EXISTING,0,NULL);
if(hCom==INVALID_HANDLE_VALUE)
{
AfxMessageBox("无法打开串口 ! ");
}
DCB dcb;
GetCommState(hCom,&dcb);
dcb.BaudRate = CBR_115200 ; //波特率为 9600
dcb.ByteSize = 8 ; // 数据位为 8
dcb.StopBits = ONESTOPBIT ; //停止位为 1
dcb.Parity = NOPARITY ; //无校验
SetCommState(hCom, &dcb) ;
SetupComm(hCom, 1024, 1024);
COMMTIMEOUTS TimeOuts;
TimeOuts.ReadIntervalTimeout=1000;
TimeOuts.ReadTotalTimeoutMultiplier=500;
TimeOuts.ReadTotalTimeoutConstant=5000;
TimeOuts.WriteTotalTimeoutMultiplier=500;
TimeOuts.WriteTotalTimeoutConstant=2000;
SetCommTimeouts(hCom,&TimeOuts);
PurgeComm(hCom,PURGE_TXCLEAR|PURGE_RXCLEAR);
BYTE cmd_s = 'S', cmd_e = 'E', cmd_wait = 0;
DWORD dwRead = 0, dwWrite = 0;
BYTE data[10] = {1, };
WriteFile(hCom, &cmd_s, 1, &dwWrite, NULL);
do{
ReadFile(hCom, &cmd_wait, 1, &dwRead, NULL);
}while('A'!= cmd_wait);
WriteFile(hCom, data, 10, &dwWrite, NULL);
cmd_wait = 0 ;
do {
ReadFile(hCom, &cmd_wait, 1, &dwRead, NULL);
}while('A'!= cmd_wait) ;
// WriteFile(hCom, &cmd_e, 1, &dwWrite, NULL) ;
CloseHandle(hCom) ;
}
服务器:
void CHostDlg::OnOK()
{
// TODO: Add extra validation here
//前面的一样 省略了端口是COM3
BYTE cmd_s = 'S', cmd_e = 'E', cmd_wait = 0,cmd_a='A';
DWORD dwRead = 0, dwWrite = 0;
BYTE data[10] = {0, };
// WriteFile(hCom, &cmd_s, 1, &dwWrite, NULL);
do{
ReadFile(hCom, &cmd_wait, 1, &dwRead, NULL);
}while('S'!= cmd_wait);
WriteFile(hCom, &cmd_a, 1, &dwWrite, NULL);
cmd_wait = 0 ;
MessageBox("1111","1111",MB_OK); //验证
do {
ReadFile(hCom, data, 10, &dwRead, NULL);
}while('1'!= data[0]) ;
buf.Format(TEXT("%d",data[0]));
m_edit.SetWindowText(buf);
// WriteFile(hCom, &cmd_e, 1, &dwWrite, NULL) ;
CloseHandle(hCom) ;
} 展开
客户端:
void CDeviceDlg::OnOK()
{
// TODO: Add extra validation here
HANDLE hCom;
hCom =CreateFile("COM4",GENERIC_WRITE|GENERIC_READ,NULL,0,OPEN_EXISTING,0,NULL);
if(hCom==INVALID_HANDLE_VALUE)
{
AfxMessageBox("无法打开串口 ! ");
}
DCB dcb;
GetCommState(hCom,&dcb);
dcb.BaudRate = CBR_115200 ; //波特率为 9600
dcb.ByteSize = 8 ; // 数据位为 8
dcb.StopBits = ONESTOPBIT ; //停止位为 1
dcb.Parity = NOPARITY ; //无校验
SetCommState(hCom, &dcb) ;
SetupComm(hCom, 1024, 1024);
COMMTIMEOUTS TimeOuts;
TimeOuts.ReadIntervalTimeout=1000;
TimeOuts.ReadTotalTimeoutMultiplier=500;
TimeOuts.ReadTotalTimeoutConstant=5000;
TimeOuts.WriteTotalTimeoutMultiplier=500;
TimeOuts.WriteTotalTimeoutConstant=2000;
SetCommTimeouts(hCom,&TimeOuts);
PurgeComm(hCom,PURGE_TXCLEAR|PURGE_RXCLEAR);
BYTE cmd_s = 'S', cmd_e = 'E', cmd_wait = 0;
DWORD dwRead = 0, dwWrite = 0;
BYTE data[10] = {1, };
WriteFile(hCom, &cmd_s, 1, &dwWrite, NULL);
do{
ReadFile(hCom, &cmd_wait, 1, &dwRead, NULL);
}while('A'!= cmd_wait);
WriteFile(hCom, data, 10, &dwWrite, NULL);
cmd_wait = 0 ;
do {
ReadFile(hCom, &cmd_wait, 1, &dwRead, NULL);
}while('A'!= cmd_wait) ;
// WriteFile(hCom, &cmd_e, 1, &dwWrite, NULL) ;
CloseHandle(hCom) ;
}
服务器:
void CHostDlg::OnOK()
{
// TODO: Add extra validation here
//前面的一样 省略了端口是COM3
BYTE cmd_s = 'S', cmd_e = 'E', cmd_wait = 0,cmd_a='A';
DWORD dwRead = 0, dwWrite = 0;
BYTE data[10] = {0, };
// WriteFile(hCom, &cmd_s, 1, &dwWrite, NULL);
do{
ReadFile(hCom, &cmd_wait, 1, &dwRead, NULL);
}while('S'!= cmd_wait);
WriteFile(hCom, &cmd_a, 1, &dwWrite, NULL);
cmd_wait = 0 ;
MessageBox("1111","1111",MB_OK); //验证
do {
ReadFile(hCom, data, 10, &dwRead, NULL);
}while('1'!= data[0]) ;
buf.Format(TEXT("%d",data[0]));
m_edit.SetWindowText(buf);
// WriteFile(hCom, &cmd_e, 1, &dwWrite, NULL) ;
CloseHandle(hCom) ;
} 展开
展开全部
1、代码太多,没耐心看
2、基本功能没描述,看了也白看
2、基本功能没描述,看了也白看
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
网上有SerialPort的类,封装好了,你直接用就行 ,功能别堆在一个函数里写
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
1,最好下载一个模拟串口工具和串口调试器助手试试
2,串口没有打开混叠读写,不支持双工,所以服务器和客户端要读写协调一致
3,有些串口设置完毕后,通信建立前,需要发送握手信号:
EscapeCommFunction(hCom, SETDTR)
不知道你的串口硬件是怎么设置的
2,串口没有打开混叠读写,不支持双工,所以服务器和客户端要读写协调一致
3,有些串口设置完毕后,通信建立前,需要发送握手信号:
EscapeCommFunction(hCom, SETDTR)
不知道你的串口硬件是怎么设置的
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询