本程序采用简单的同步串行通信,分为几个阶段:
1、打开串口
2、配置串口
3、设置串口输入输出缓存区大小
4、设置串口读写超时(若不设置超时,读写时会等待读写函数返回)
5、发送字符串(每次发送前清空发送缓存区)
6、接收字符(每次接收前清空接收缓存区)
7、关闭串口
bmLightComm.h
1 #pragma once 2 #include "stdio.h" 3 #include "stdbool.h" 4 #include "windows.h" 5 #include "tchar.h" 6 7 class bmLightComm 8 { 9 public: 10 bmLightComm(); 11 ~bmLightComm(); 12 bool openPort(const char* portNum); 13 bool setupPort(DWORD baudrate, BYTE bytesize, BYTE stopbits, DWORD fparity, BYTE parity); 14 bool setBufferAreaSize(DWORD inQueue, DWORD outQueue); 15 bool setComTimeout(DWORD readinttim, DWORD readToltimM, DWORD readToltimC,DWORD writeToltimM, DWORD writeToltimC); 16 bool purgePort(DWORD flags); 17 bool closePort(); 18 19 bool sendBuff(const char* datas, unsigned int len); 20 char readBuff(); 21 22 private: 23 HANDLE mCom; 24 };
bmLightComm.cpp
1 #include "bmLightComm.h" 2 #include <iostream> 3 4 5 bmLightComm::bmLightComm() 6 { 7 } 8 9 10 bmLightComm::~bmLightComm() 11 { 12 } 13 14 bool bmLightComm::openPort(const char* portNum) 15 { 16 printf("open the com %s ", portNum); 17 18 mCom = CreateFileA(portNum, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL); 19 std::cout << GetLastError() << std::endl; 20 if (mCom == INVALID_HANDLE_VALUE) 21 { 22 printf("failed to open com %s %d ", portNum, GetLastError()); 23 return false; 24 } 25 else 26 { 27 printf("cam %s opened ", portNum); 28 return true; 29 } 30 } 31 32 bool bmLightComm::setupPort(DWORD baudrate, BYTE bytesize, BYTE stopbits, DWORD fparity, BYTE parity) 33 { 34 printf("set up port "); 35 36 DCB mDcb; 37 if (!GetCommState(mCom, &mDcb)) 38 { 39 printf("get com state failed %d ", GetLastError()); 40 return false; 41 } 42 43 mDcb.BaudRate = baudrate; 44 mDcb.fParity = fparity; 45 mDcb.Parity = parity; 46 mDcb.StopBits = stopbits; 47 mDcb.ByteSize = bytesize; 48 49 if (!SetCommState(mCom, &mDcb)) 50 { 51 printf("failed to set up com state %d ", GetLastError()); 52 return false; 53 } 54 else 55 { 56 printf("com set up complete "); 57 return true; 58 } 59 } 60 61 bool bmLightComm::setBufferAreaSize(DWORD inQueue, DWORD outQueue) 62 { 63 if (!SetupComm(mCom, inQueue, outQueue)) 64 { 65 printf("failed to set the buffer size %d ", GetLastError()); 66 return false; 67 } 68 else 69 { 70 printf("com set buffer size complete "); 71 return true; 72 } 73 } 74 75 bool bmLightComm::setComTimeout(DWORD readinttim, DWORD readToltimM, DWORD readToltimC, DWORD writeToltimM, DWORD writeToltimC) 76 { 77 COMMTIMEOUTS TimeOuts; 78 TimeOuts.ReadIntervalTimeout = readinttim; 79 TimeOuts.ReadTotalTimeoutMultiplier = readToltimM; 80 TimeOuts.ReadTotalTimeoutConstant = readToltimC; 81 TimeOuts.WriteTotalTimeoutMultiplier = writeToltimM; 82 TimeOuts.WriteTotalTimeoutConstant = writeToltimC; 83 84 if (!SetCommTimeouts(mCom,&TimeOuts)) 85 { 86 printf("failed to set time out %d ", GetLastError()); 87 return false; 88 } 89 else 90 { 91 printf("set time out complete "); 92 return true; 93 } 94 } 95 96 bool bmLightComm::purgePort(DWORD flags) 97 { 98 //PURGE_TXCLEAR,清空发送缓冲区;PURGE_RXCLEAR,清空接收缓冲区; 99 if (!PurgeComm(mCom, flags)) 100 { 101 printf("failed to purge com %d ", GetLastError()); 102 return false; 103 } 104 else 105 { 106 printf("purge com complete "); 107 return true; 108 } 109 } 110 111 bool bmLightComm::closePort() 112 { 113 this->purgePort(PURGE_TXCLEAR | PURGE_RXCLEAR); 114 115 if (!CloseHandle(mCom)) 116 { 117 printf("failed to close port %d ", GetLastError()); 118 return false; 119 } 120 else 121 { 122 printf("close port complete "); 123 return true; 124 } 125 } 126 127 bool bmLightComm::sendBuff(const char* datas, unsigned int len) 128 { 129 this->purgePort(PURGE_TXCLEAR); 130 131 DWORD pWrite; 132 if (!WriteFile(mCom, datas, sizeof(const char)*len, &pWrite, NULL)) 133 { 134 printf("failed to write buff %d ", GetLastError()); 135 return false; 136 } 137 else 138 { 139 printf("write buff complete "); 140 return true; 141 } 142 } 143 144 char bmLightComm::readBuff() 145 { 146 this->purgePort(PURGE_RXCLEAR); 147 148 char buffl; 149 DWORD pRead; 150 if (!ReadFile(mCom, &buffl, sizeof(buffl), &pRead, NULL)) 151 { 152 printf("failed to read buff %d ", GetLastError()); 153 return '