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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
CCommPort::~CCommPort()
{
ClosePort();
}
CCommPort::CCommPort(const char * MyPort, int vitesse)
{
if (!OpenPort(MyPort,vitesse)){
AfxMessageBox(CString("Erreur d'ouverture du port ")+MyPort,MB_OK,0);
throw CCommPort::Exception();
}
}
//////////////////////////////////////////////////////////////////////
// Implementation
//////////////////////////////////////////////////////////////////////
int CCommPort::OpenPort(const char * MyPort,int vitesse)
{
COMMTIMEOUTS commtimeouts;
DCB PortDCB;
hCommPort = CreateFile(CString(MyPort),
GENERIC_READ|GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
0,
NULL);
if (hCommPort == INVALID_HANDLE_VALUE){
AfxMessageBox(CString("Invalid Handle"),MB_OK,0);
return 0;
}
//initialisation du port
GetCommState(hCommPort,&PortDCB);
PortDCB.DCBlength=sizeof(DCB);
PortDCB.BaudRate = vitesse;
PortDCB.fBinary = TRUE;
PortDCB.fParity = TRUE;
PortDCB.fOutxCtsFlow = FALSE;
PortDCB.fOutxDsrFlow = FALSE;
PortDCB.fDtrControl = DTR_CONTROL_ENABLE;
PortDCB.fDsrSensitivity = FALSE;
PortDCB.fTXContinueOnXoff = FALSE;
PortDCB.fOutX = FALSE;
PortDCB.fInX = FALSE;
PortDCB.fErrorChar = FALSE;
PortDCB.fNull = FALSE;
PortDCB.fRtsControl = RTS_CONTROL_DISABLE;
PortDCB.fAbortOnError = FALSE;
PortDCB.ByteSize = 8;
PortDCB.Parity = NOPARITY;
PortDCB.StopBits = ONESTOPBIT;
//Renvoie des nouvelles informations dans le registre de votre port série
if (!SetCommState (hCommPort, &PortDCB))
{
AfxMessageBox(CString("Echec initialisation du port"),MB_OK,0);
return 0;
}
//initialisation du mask des messages
if(!SetCommMask(hCommPort, EV_RXCHAR))
{
AfxMessageBox(CString("Mask de communication non défini"), MB_OK,0);
return 0;
}
//initialisation du timeout
commtimeouts.ReadIntervalTimeout = MAXDWORD;
commtimeouts.ReadTotalTimeoutMultiplier = 10;
commtimeouts.ReadTotalTimeoutConstant = 10;
commtimeouts.WriteTotalTimeoutMultiplier = 2*CBR_9600; // r_DCB.BaudRate;
commtimeouts.WriteTotalTimeoutConstant = MAXDWORD;
if(!SetCommTimeouts(hCommPort, &commtimeouts))
{
AfxMessageBox(CString("Timeout de communication non défini"), MB_OK,0);
return 0;
}
return 1;
}
int CCommPort::ClosePort()
{
if (!CloseHandle(hCommPort)){
AfxMessageBox(CString("Erreur de fermeture du Port"),MB_OK,0);
return 0;
}
return 1;
}
DWORD CCommPort::WriteStr(const char *MyChar)
{
DWORD dwNumBytesWritten;
int tmp = strlen(MyChar);
WriteFile(hCommPort,(BYTE *)MyChar,tmp,&dwNumBytesWritten, NULL) ;
return dwNumBytesWritten;
}
DWORD CCommPort::ReadStr(char *MyChar)
{
DWORD lpErrors;
COMSTAT lpStat;
DWORD dwNumBytesRead = 0;
DWORD dwCommStatus = 0;
WaitCommEvent(hCommPort,&dwCommStatus,0);
ClearCommError(hCommPort,&lpErrors,&lpStat);
ReadFile(hCommPort,(BYTE *)MyChar,lpStat.cbInQue,&dwNumBytesRead,NULL);
(char *)MyChar;
MyChar[dwNumBytesRead] = '\0';
return dwNumBytesRead;
} |
Partager