问题描述
- 串口程序发送接收数据问题 (没有据传输)
-
//接收数据
void ReceiveChar()
{BOOL istrue;
DWORD de = 0;
char buffer[MAX] = { 0 };
DWORD data;
BOOL f;
DWORD doe;
COMSTAT coms;
BOOL m;
OVERLAPPED over;
/*********串口事件的设置***************/
istrue = SetCommMask(hComm, EV_RXCHAR | EV_TXEMPTY);
if (istrue)
{
printf("串口事件设置成功!");
}
else
{
printf("串口事件设置失败!请重试");
}
/***************获取串口缓存区数据大小*********************/
istrue = 0;
while (!istrue)
{
istrue = WaitCommEvent(hComm, &de, &over);
}
if (istrue)
{
if (de == EV_RXCHAR)
{
ClearCommError(hComm,&doe,&coms);
if (coms.cbInQue>0)
{
f=ReadFile(hComm, &buffer, coms.cbInQue,& data, &over);
if (!f)
{m = GetOverlappedResult(hComm, &over,& data, true);
while (!m)
{
if (GetLastError() == ERROR_IO_PENDING)
{
m = GetOverlappedResult(hComm,&over,& data, true);
}
}
}} } } for (int i = 0; i <MAX;i++) { printf("%c",buffer[i]); }
}
void WriteChar()
{
BOOL f;
char buffer[] = { "通过串口进行数据发送" };
DWORD data = sizeof(buffer);
DWORD datal, n = 0;
OVERLAPPED over;
f = WriteFile(hComm, &buffer, data, &datal,&over);
if (!f)
{
BOOL m;
m = GetOverlappedResult(hComm, &over, &data, true);
while (!m)
{
if (GetLastError() == ERROR_IO_PENDING)
{m = GetOverlappedResult(hComm,& over, &data, true); n += datal; if (n==data) { break; } } } }
}