// TODO: Add your control notification handler code here
char buff[200];
CString str;
GetDlgItem(IDC_EDI_BUF) -> GetWindowText(str);
sprintf(buff,"%s",(LPCSTR)str);
//MessageBox(buff);
//然后打开串口,开始通信
HANDLE hCom; //createFile函数返回的句柄
//打开的是端口一
hCom = CreateFile("COM1",GENERIC_READ|GENERIC_WRITE,
0, NULL,OPEN_EXISTING, 0, NULL);
if(hCom == NULL)
{
MessageBox("打开端口一时出现了错误");
}
//设置读写缓冲区的缓冲大小
//改变端口的设备控制块
/*DCB dcb;
GetCommState(hCom,&dcb);
dcb.BaudRate = CBR_9600;
dcb.ByteSize=8;//数据位8位
dcb.Parity = NOPARITY; //有校验
dcb.StopBits=ONESTOPBIT; //一个停止位
if(false==SetCommState(hCom,&dcb))
{
MessageBox("配置端口时出现了错误");
}*/
DCB dcb;
GetCommState(hCom,&dcb);
dcb.BaudRate = CBR_9600;
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.fBinary = TRUE;
dcb.fParity = TRUE;
dcb.fOutxDsrFlow = false;
dcb.fDtrControl = DTR_CONTROL_DISABLE;
dcb.fOutxCtsFlow = false;
//#ifdef LPR_DOHERELARGE
// dcb.fRtsControl = RTS_CONTROL_ENABLE;
//#else
dcb.fRtsControl = RTS_CONTROL_DISABLE;
//#endif
/* dcb.fInX = false;
dcb.fOutX = false;
dcb.XonChar = ASCII_XON;
dcb.XoffChar = ASCII_XOFF;
dcb.XonLim = 100;
dcb.XoffLim = 100;*/
if(false==SetCommState(hCom,&dcb))
{
MessageBox("配置端口时出现了错误");
}
//设置读写操作,将缓冲里面的数据写入到串口里面
BOOL isWrite = false;
DWORD length = 100;
COMSTAT ComStat;
DWORD dwErrorFlags;
ClearCommError(hCom,&dwErrorFlags,&ComStat);
isWrite = WriteFile(hCom,buff,length,&length,NULL);
if(isWrite == false)
{
MessageBox("写入数据时出现了错误");
}
//写完数据然后从缓冲里面读取数据
//过一段时间才读取缓冲里面的数据
::Sleep(100);
//读取缓冲里面的数据
BOOL isRead = false;
char readbuf[100];
DWORD bReadStatus;
memset(readbuf,0,sizeof(readbuf));
isRead = ::ReadFile(hCom,readbuf,100,&bReadStatus,NULL);
//MessageBox(readbuf);
GetDlgItem(IDC_RECV_BUFF) -> SetWindowText(readbuf);
CloseHandle(hCom);
|