1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#include <windows.h>
#include <cstdio>
#include <iostream>
using namespace std;
int main (void)
{
HANDLE hDevice;
DCB dcb;
char szBuff[7];
DWORD read = 0;
int bStatus;
hDevice = CreateFile(TEXT("COM1"),GENERIC_READ,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
if (hDevice==INVALID_HANDLE_VALUE){
return -1;
}
dcb.DCBlength = sizeof(DCB);
GetCommState(hDevice,&dcb);
//printf("%d",dcb.ByteSize);
dcb.ByteSize = 8;
dcb.BaudRate = CBR_9600;
dcb.StopBits = ONESTOPBIT;
dcb.Parity = NOPARITY;
bStatus=SetCommState(hDevice,&dcb);
if (bStatus==0){
cout<<"opération Failed"<<endl;
}
if (hDevice==INVALID_HANDLE_VALUE){
return -1;
}
COMMTIMEOUTS timeOuts;
GetCommTimeouts(hDevice,&timeOuts);
//printf("%d\n",timeOuts.WriteTotalTimeoutMultiplier);
timeOuts.ReadIntervalTimeout = 0;
timeOuts.WriteTotalTimeoutConstant = 1000;
timeOuts.WriteTotalTimeoutMultiplier = 10;
bStatus = SetCommTimeouts(hDevice, &timeOuts);
if (bStatus==0){
cout<<"opération Failed"<<endl;
}
if (hDevice==INVALID_HANDLE_VALUE){
return -1;
}
bStatus = ReadFile(hDevice,szBuff,sizeof(szBuff),&read,NULL);
if (bStatus==0){
cout<<"opération Failed"<<endl;
}
//printf("%d\n",read); //affiche toujours 0
DWORD i;
if (szBuff!=""){
for (i=0;i<read;i++)
{
cout<<(unsigned char)szBuff[i];
}
}
else {
cout<<"buffer vide"<<endl;
}
CloseHandle(hDevice);
system("PAUSE");
return 0;
} |
Partager