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
| #include "windows.h"
#include "stdio.h"
#include <QDebug>
#define RX_SIZE 4096 /* taille tampon d'entrée */
#define TX_SIZE 4096 /* taille tampon de sortie */
#define MAX_WAIT_READ 500 /* temps max d'attente pour lecture (en ms) */
/* Handle du port COM ouvert */
HANDLE g_hCOM = NULL;
/* Délais d'attente sur le port COM */
COMMTIMEOUTS g_cto =
{
MAX_WAIT_READ, /* ReadIntervalTimeOut */
0, /* ReadTotalTimeOutMultiplier */
MAX_WAIT_READ, /* ReadTotalTimeOutConstant */
0, /* WriteTotalTimeOutMultiplier */
0 /* WriteTotalTimeOutConstant */
};
/* Configuration du port COM */
DCB g_dcb =
{
sizeof(DCB), /* DCBlength */
38400, /* BaudRate */
TRUE, /* fBinary */
FALSE, /* fParity */
FALSE, /* fOutxCtsFlow */
FALSE, /* fOutxDsrFlow */
DTR_CONTROL_ENABLE, /* fDtrControl */
FALSE, /* fDsrSensitivity */
FALSE, /* fTXContinueOnXoff */
FALSE, /* fOutX */
FALSE, /* fInX */
FALSE, /* fErrorChar */
FALSE, /* fNull */
RTS_CONTROL_ENABLE, /* fRtsControl */
FALSE, /* fAbortOnError */
0, /* fDummy2 */
0, /* wReserved */
0x100, /* XonLim */
0x100, /* XoffLim */
8, /* ByteSize */
NOPARITY, /* Parity */
ONESTOPBIT, /* StopBits */
0x11, /* XonChar */
0x13, /* XoffChar */
'?', /* ErrorChar */
0x1A, /* EofChar */
0x10 /* EvtChar */
};
BOOL engine::OpenCOM(int nId)
{
/* variables locales */
char szCOM[16];
qDebug("ok1");
/* construction du nom du port, tentative d'ouverture */
sprintf(szCOM, "COM%d", nId);
qDebug("ok2");
/* conversion char vers WCHAR */
wchar_t *WszCOM = (wchar_t *)malloc( sizeof( wchar_t ));
qDebug("ok3");
mbstowcs( WszCOM, szCOM, 16 );
qDebug("ok4");
/*fin de conversion*/
//g_hCOM = CreateFile(WszCOM, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM, NULL);
g_hCOM=CreateFile(WszCOM,GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,CREATE_NEW|OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
qDebug("ok5");
if(g_hCOM == INVALID_HANDLE_VALUE)
{
// printf("Erreur lors de l'ouverture du port COM%d", nId);
return FALSE;
}
qDebug("ok6");
/* affectation taille des tampons d'émission et de réception */
SetupComm(g_hCOM, RX_SIZE, TX_SIZE);
qDebug("ok7");
/* configuration du port COM */
if(!SetCommTimeouts(g_hCOM, &g_cto) || !SetCommState(g_hCOM, &g_dcb))
{
//printf("Erreur lors de la configuration du port COM%d", nId);
CloseHandle(g_hCOM);
return FALSE;
}
qDebug("ok8");
/* on vide les tampons d'émission et de réception, mise à 1 DTR */
PurgeComm(g_hCOM, PURGE_TXCLEAR|PURGE_RXCLEAR|PURGE_TXABORT|PURGE_RXABORT);
EscapeCommFunction(g_hCOM, SETDTR);
return TRUE;
} |
Partager