#define _CRT_SECURE_NO_WARNINGS
#include <string.h>
#include <conio.h>
#include "tchar.h"
#include<stdio.h>
#include<windows.h>
int main()
{
HANDLE hCom1;
COMMTIMEOUTS TimeOuts;
DCB dcb1;
DWORD i ;
DWORD wCount =4;
DWORD wCount1;
unsigned char str[8];
hCom1 = CreateFile("COM4",
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
if (hCom1==INVALID_HANDLE_VALUE )
{
printf("read open failed\n");
}
else
{
printf("read open successed\n");
}
SetupComm(hCom1, 20480, 20480);
//COMMTIMEOUTS TimeOuts;
TimeOuts.ReadIntervalTimeout =100;
TimeOuts.ReadTotalTimeoutMultiplier =500;
TimeOuts.ReadTotalTimeoutConstant = 5000;
TimeOuts.WriteTotalTimeoutMultiplier = 500;
TimeOuts.WriteTotalTimeoutConstant = 2000;
SetCommTimeouts(hCom1, &TimeOuts);
//DCB dcb1;
GetCommState(hCom1, &dcb1);
dcb1.BaudRate = 115200;
dcb1.ByteSize = 8;//一个字节8bit
dcb1.Parity = NOPARITY;//无校验
dcb1.StopBits = ONESTOPBIT;//1位停止位
dcb1.fParity = FALSE;
dcb1.fNull = FALSE;
SetCommState(hCom1, &dcb1);
PurgeComm(hCom1, PURGE_TXCLEAR|PURGE_RXCLEAR);
if(!ReadFile(hCom1, str, wCount, &wCount1, NULL))
{
printf("read failed!");
}
else
{
printf("read successed!");
printf("wCount1:%d\n", wCount1);
printf("read data is");
for(i=0;i<wCount1;i++)
{
printf("%02X", str[i]);
}
}
CloseHandle(hCom1);
// system("pause");
}
代码贴上来了,wCount1总是为0,不知道哪里出了问题,有没有大佬帮忙看下 |