向串口发送命令后如何读取返回值
以下是我发命令的代码,麻烦帮我看看怎么读取返回值啊,能不能紧接着发数据的代码读啊,我懂得是在是少,恳请帮忙!!
DCB PortDCBzt;
HANDLE hPortzt;
COMMTIMEOUTS ctozt;
HANDLE hThreadzt;//创建线程
/***初始化*************************/
ctozt.ReadIntervalTimeout = 0;
ctozt.ReadTotalTimeoutMultiplier = 0;
ctozt.ReadTotalTimeoutConstant = 0;
ctozt.WriteTotalTimeoutMultiplier = 0;
ctozt.WriteTotalTimeoutConstant = 0;
SetCommTimeouts (hPortzt, &ctozt);//设定超时
if (hThreadzt)
CloseHandle (hThreadzt);
PurgeComm(hPortzt,PURGE_TXCLEAR|PURGE_RXCLEAR);
/***打开端口*************************/
hPortzt=CreateFile(C综合控制App::ytSerialPort+_T(":"), GENERIC_READ|GENERIC_WRITE,0, NULL, OPEN_EXISTING,0, NULL);
if(hPortzt==INVALID_HANDLE_VALUE)
{
AfxMessageBox(_T( "open com port error ! "));
}
GetCommState (hPortzt, &PortDCBzt); //配置串口
PortDCBzt.DCBlength= sizeof (DCB);
PortDCBzt.BaudRate= CBR_9600;
PortDCBzt.fBinary = TRUE;
PortDCBzt.fParity= FALSE;
PortDCBzt.ByteSize= 8;
PortDCBzt.Parity= NOPARITY;
PortDCBzt.StopBits= ONESTOPBIT;
if(!SetCommState(hPortzt, &PortDCBzt))
{
AfxMessageBox(_T( "Set com port error ! "));
}
/***发命令函数*************************/
unsigned char up_act[8]={0xFF,0x01,0x00,0x51,0x1C,0x1C,0x8A,0x00};
DWORD dwBytesWrite=32;
COMSTAT ComStat;
DWORD dwErrorFlags;
BOOL bWriteStat;
ClearCommError(hPortzt,&dwErrorFlags,&ComStat);
bWriteStat=WriteFile(hPortzt,up_act,dwBytesWrite,& dwBytesWrite,NULL);
if(!bWriteStat)
{
AfxMessageBox(_T("写串口失败!"));
}
PurgeComm(hPortzt, PURGE_TXABORT|
PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR);
至于写数据,看你需要了 什么时候写都可以