📄 receive.cpp
字号:
//刚才是串口发,这里是串口收的程序:#include <windows.h>#include <stdio.h>#include <conio.h>#include <mmsystem.h>static HANDLE hComm;static DCB Current_Comm_dcb = {0};static DCB Previous_Comm_dcb = {0};static OVERLAPPED osRead = {0};BOOL ReadABuffer(BYTE * lpBuf, DWORD dwToRead){static DWORD dwRead,dwRes;static BOOL fWaitingOnRead = FALSE;static BOOL fRes;if (!fWaitingOnRead) { // Issue read operation. if (!ReadFile(hComm, lpBuf, dwToRead, &dwRead, &osRead)) { if (GetLastError() != ERROR_IO_PENDING) // read not delayed? // Error in communications; report it. fRes = FALSE; else fWaitingOnRead = TRUE; } else { // read completed immediately// HandleASuccessfulRead(lpBuf, dwRead); fRes = TRUE; }}if (fWaitingOnRead) { dwRes = WaitForSingleObject(osRead.hEvent, INFINITE); switch(dwRes) { // Read completed. case WAIT_OBJECT_0: if (!GetOverlappedResult(hComm, &osRead, &dwRead, FALSE)) // Error in communications; report it. fRes = FALSE; else // Read completed successfully.// HandleASuccessfulRead(lpBuf, dwRead); fRes = TRUE; // Reset flag so that another opertion can be issued. fWaitingOnRead = FALSE; break;// case WAIT_TIMEOUT: // Operation isn't complete yet. fWaitingOnRead flag isn't // changed since I'll loop back around, and I don't want // to issue another read until the first one finishes. // // This is a good time to do some background work.// break; default: // Error in the WaitForSingleObject; abort. // This indicates a problem with the OVERLAPPED structure's // event handle. fRes = FALSE; break; }}return(fRes);}int main(int argc,char *argv[]){BYTE c;printf("Writen by jxj.2001.5. All rights reserved\n\n");printf("Open serial communication port ... ");hComm = CreateFile( "com1", GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);if (hComm == INVALID_HANDLE_VALUE){ printf("error!\n"); getch(); return(1);}else{ printf("successful!\n");}FillMemory(&Previous_Comm_dcb, sizeof(Previous_Comm_dcb), 0);FillMemory(&Current_Comm_dcb, sizeof(Current_Comm_dcb), 0);Previous_Comm_dcb.DCBlength = sizeof(Previous_Comm_dcb);Current_Comm_dcb.DCBlength = sizeof(Current_Comm_dcb);printf("Get previous CommState ... ");if (!GetCommState(hComm, &Previous_Comm_dcb)){ printf("error!\n"); getch(); return(1);}else{ printf("successful!\n");}printf("Set current CommState ... ");Current_Comm_dcb = Previous_Comm_dcb;Current_Comm_dcb.BaudRate = 115200;Current_Comm_dcb.fBinary = TRUE;Current_Comm_dcb.fParity = TRUE;Current_Comm_dcb.StopBits = ONESTOPBIT;Current_Comm_dcb.fDtrControl = DTR_CONTROL_DISABLE;Current_Comm_dcb.fRtsControl = RTS_CONTROL_DISABLE;Current_Comm_dcb.ByteSize = 8;Current_Comm_dcb.Parity = ODDPARITY;Current_Comm_dcb.fOutX = FALSE;Current_Comm_dcb.fInX = FALSE;if (!SetCommState(hComm, &Current_Comm_dcb)){ printf("error!\n"); getch(); return(1);}else{ printf("successful!\n");}//上面跟上一篇文章里都一样。printf("Initialize read ... ");//准备读osRead.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);if (osRead.hEvent == NULL){ printf("error!\n"); getch(); return(1);}else{ printf("successful!\n");}if(!ReadABuffer(&c,1)){//现在这个函数写成有错误才立刻返回,如果没错误,则直到对方发送才返回。printf("Read Com Error!\n");}else{ printf("%c\n",c); getch();} printf("Recover the previous CommState ... ");//恢复串口设置 if (!SetCommState(hComm, &Previous_Comm_dcb)) { printf("error!\n"); getch(); return(1); } else { printf("successful!\n"); } CloseHandle(osRead.hEvent); CloseHandle(hComm); return(0);}//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -