|
@@ -14,16 +14,16 @@ char read_buf[MAX_NUMBER]; |
|
|
* dcb.ByteSize=6,7,8时 dcb.StopBits不能为1 |
|
|
* dcb.ByteSize=6,7,8时 dcb.StopBits不能为1 |
|
|
* dcb.ByteSize=5时 dcb.StopBits不能为2 |
|
|
* dcb.ByteSize=5时 dcb.StopBits不能为2 |
|
|
********************************************************************************************/ |
|
|
********************************************************************************************/ |
|
|
HANDLE InitCOM(LPCTSTR Port, int baud_rate, BYTE date_bits, BYTE stop_bit, BYTE parity) |
|
|
|
|
|
|
|
|
HANDLE Init_COM(LPCTSTR Port, int baud_rate, BYTE date_bits, BYTE stop_bit, BYTE parity) |
|
|
{ |
|
|
{ |
|
|
HANDLE hCom = CreateFile(Port, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);//同步方式打开串口 |
|
|
|
|
|
if (INVALID_HANDLE_VALUE == hCom) |
|
|
|
|
|
|
|
|
HANDLE Handle_Com = CreateFile(Port, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);//同步方式打开串口 |
|
|
|
|
|
if (INVALID_HANDLE_VALUE == Handle_Com) |
|
|
{ |
|
|
{ |
|
|
return INVALID_HANDLE_VALUE; |
|
|
return INVALID_HANDLE_VALUE; |
|
|
} |
|
|
} |
|
|
SetupComm(hCom, 4096, 4096);//设置缓存 |
|
|
|
|
|
|
|
|
SetupComm(Handle_Com, 4096, 4096);//设置缓存 |
|
|
DCB dcb; |
|
|
DCB dcb; |
|
|
if (!GetCommState(hCom, &dcb)) |
|
|
|
|
|
|
|
|
if (!GetCommState(Handle_Com, &dcb)) |
|
|
{ |
|
|
{ |
|
|
cout << "获取串口配置失败" << endl; |
|
|
cout << "获取串口配置失败" << endl; |
|
|
} |
|
|
} |
|
@@ -47,22 +47,20 @@ HANDLE InitCOM(LPCTSTR Port, int baud_rate, BYTE date_bits, BYTE stop_bit, BYTE |
|
|
dcb.fParity = TRUE; //奇偶校验开启 |
|
|
dcb.fParity = TRUE; //奇偶校验开启 |
|
|
dcb.Parity = parity; //校验模式 |
|
|
dcb.Parity = parity; //校验模式 |
|
|
} |
|
|
} |
|
|
cout << SetCommState(hCom, &dcb) << endl; |
|
|
|
|
|
PurgeComm(hCom, PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_TXABORT);//清除缓存 |
|
|
|
|
|
|
|
|
cout << SetCommState(Handle_Com, &dcb) << endl; |
|
|
|
|
|
PurgeComm(Handle_Com, PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_TXABORT);//清除缓存 |
|
|
//设置串口读写时间 |
|
|
//设置串口读写时间 |
|
|
COMMTIMEOUTS CommTimeOuts; |
|
|
COMMTIMEOUTS CommTimeOuts; |
|
|
GetCommTimeouts(hCom, &CommTimeOuts); |
|
|
|
|
|
|
|
|
GetCommTimeouts(Handle_Com, &CommTimeOuts); |
|
|
CommTimeOuts.ReadIntervalTimeout = 5; |
|
|
CommTimeOuts.ReadIntervalTimeout = 5; |
|
|
CommTimeOuts.ReadTotalTimeoutMultiplier = 1; |
|
|
CommTimeOuts.ReadTotalTimeoutMultiplier = 1; |
|
|
CommTimeOuts.ReadTotalTimeoutConstant = 0; |
|
|
CommTimeOuts.ReadTotalTimeoutConstant = 0; |
|
|
CommTimeOuts.WriteTotalTimeoutMultiplier = 10; |
|
|
CommTimeOuts.WriteTotalTimeoutMultiplier = 10; |
|
|
CommTimeOuts.WriteTotalTimeoutConstant = 1000; |
|
|
CommTimeOuts.WriteTotalTimeoutConstant = 1000; |
|
|
if (!SetCommTimeouts(hCom, &CommTimeOuts)) { |
|
|
|
|
|
|
|
|
if (!SetCommTimeouts(Handle_Com, &CommTimeOuts)) { |
|
|
return INVALID_HANDLE_VALUE; |
|
|
return INVALID_HANDLE_VALUE; |
|
|
} |
|
|
} |
|
|
//创建线程,读取数据 |
|
|
|
|
|
//HANDLE hReadCommThread = (HANDLE)CreateThread(NULL, 0, CommProc, &hCom, 0, NULL); |
|
|
|
|
|
return hCom; |
|
|
|
|
|
|
|
|
return Handle_Com; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/********************************************************************************************* |
|
|
/********************************************************************************************* |
|
@@ -135,8 +133,8 @@ int test(void) |
|
|
{ |
|
|
{ |
|
|
string COMM; |
|
|
string COMM; |
|
|
cin >> COMM; |
|
|
cin >> COMM; |
|
|
HANDLE H_Com = InitCOM((LPCTSTR)COMM.c_str(), 9600, 8, 0, 1); |
|
|
|
|
|
if (H_Com == INVALID_HANDLE_VALUE) |
|
|
|
|
|
|
|
|
HANDLE Handle_Com = Init_COM((LPCTSTR)COMM.c_str(), 9600, 8, 0, 1); |
|
|
|
|
|
if (Handle_Com == INVALID_HANDLE_VALUE) |
|
|
{ |
|
|
{ |
|
|
cout << "初始化串口失败" << endl; |
|
|
cout << "初始化串口失败" << endl; |
|
|
getchar(); |
|
|
getchar(); |
|
@@ -152,16 +150,16 @@ int test(void) |
|
|
//生成请求报文 |
|
|
//生成请求报文 |
|
|
//发送请求报文 |
|
|
//发送请求报文 |
|
|
|
|
|
|
|
|
PurgeComm(H_Com, PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_TXABORT);//清除缓存 |
|
|
|
|
|
|
|
|
PurgeComm(Handle_Com, PURGE_RXABORT | PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_TXABORT);//清除缓存 |
|
|
while (1) |
|
|
while (1) |
|
|
{ |
|
|
{ |
|
|
if (true == SendData(H_Com, "ok", 2)) |
|
|
|
|
|
|
|
|
if (true == SendData(Handle_Com, "ok", 2)) |
|
|
{ |
|
|
{ |
|
|
cout << "发送请求报文成功" << endl; |
|
|
cout << "发送请求报文成功" << endl; |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
BOOL bReadOK = ReadFile(H_Com, read_buf, 256, &dwRead, NULL); |
|
|
|
|
|
|
|
|
BOOL bReadOK = ReadFile(Handle_Com, read_buf, 256, &dwRead, NULL); |
|
|
if (bReadOK && (dwRead > 0)) |
|
|
if (bReadOK && (dwRead > 0)) |
|
|
{ |
|
|
{ |
|
|
read_buf[dwRead] = '\0'; |
|
|
read_buf[dwRead] = '\0'; |
|
@@ -170,7 +168,7 @@ int test(void) |
|
|
else |
|
|
else |
|
|
cout << "loss" << endl; |
|
|
cout << "loss" << endl; |
|
|
} |
|
|
} |
|
|
CloseHandle(H_Com); |
|
|
|
|
|
|
|
|
CloseHandle(Handle_Com); |
|
|
getchar(); |
|
|
getchar(); |
|
|
return 0; |
|
|
return 0; |
|
|
} |
|
|
} |