Initialisation port serie
Salut,
je souhaite communiquer avec une carte via liaison serie dont on me donne comme caractéristique:
Liaison série asychrone RS232:
9600 à 57600 bauds
8 bit de données
Pas de parité
1 bit de stop
voila donc comment je procède:
Code:
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
| /**************Initialisation du port série**************/
int init_serie()
{
DCB dcb; //configuration du port
DWORD N_ERROR; //n° de l'erreur
BOOL Success;
int TimeoutRead = 100; // timeout en ms
COMMTIMEOUTS tTimeout;
//vérification de l'ouverture
if(hSerie==INVALID_HANDLE_VALUE)
{
//fichier non créé
N_ERROR=GetLastError();
}
//lecture config initial
Success=GetCommState(hSerie,&dcb);
if(!Success)
{
switch(N_ERROR)
{
case 5:
MessageBox(NULL,L"Access is denied",L"Error 5",MB_ICONERROR);
break;
default:
printf("\nErreur %x",N_ERROR);
}
}
//configuration du port
dcb.BaudRate=CBR_19200; //Bits par seconde
dcb.ByteSize=8; //Bits de données
dcb.Parity=NOPARITY; //Parité
dcb.StopBits=ONESTOPBIT; //Bits d'arrêt
dcb.fInX=FALSE; //desactive les Xon-Xoff
dcb.fOutX=FALSE;
/***configurer le timeout***/
tTimeout.ReadIntervalTimeout = MAXWORD;
tTimeout.ReadTotalTimeoutMultiplier = 0;
tTimeout.ReadTotalTimeoutConstant = TimeoutRead; // pas de time out = 0
tTimeout.WriteTotalTimeoutMultiplier = 0;
tTimeout.WriteTotalTimeoutConstant = 0;
SetCommTimeouts(hSerie,&tTimeout);
Success=GetCommState(hSerie,&dcb);
if(!Success)
{
printf("\nErreur %x",N_ERROR);
}
return (Success);
} |
un petit bout du main:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
void main (void)
{
//...
/***Ouverture du port de communication***/
hSerie=CreateFile(TEXT("COM1"), //Nom du port
GENERIC_READ|GENERIC_WRITE, //permet l'ecriture et la lecture sur le port
0,//Mode de partage
NULL, //Option de sécurité par défaut
OPEN_EXISTING,
0, //NULL=synchrone, sinon asynchrone
NULL);
std::cout<<"Connection en cours...\n";
CheckInit=init_serie(); // initialise le port serie
//....
} |
mais sa ne marche pas ... je ne peux pas communiquer avec l'appareil
par contre il existe un logiciel qui permet de le faire, donc si je le lance et que je me connecte avec un fois, je peux ensuite utilisé mon programme car le port doit etre bien configuré.
J'observe avec portmon pour voir ce qui se passe et avec mon programme j'obtient:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| 2 0.00005364 COM_serie.exe IRP_MJ_CREATE Serial1 SUCCESS Options: Open
3 0.00000335 COM_serie.exe IOCTL_SERIAL_GET_BAUD_RATE Serial1 SUCCESS
4 0.00000196 COM_serie.exe IOCTL_SERIAL_GET_LINE_CONTROL Serial1 SUCCESS
5 0.00000168 COM_serie.exe IOCTL_SERIAL_GET_CHARS Serial1 SUCCESS
6 0.00000168 COM_serie.exe IOCTL_SERIAL_GET_HANDFLOW Serial1 SUCCESS
7 0.00000196 COM_serie.exe IOCTL_SERIAL_SET_TIMEOUTS Serial1 SUCCESS RI:65535 RM:0 RC:100 WM:0 WC:0
8 0.00000140 COM_serie.exe IOCTL_SERIAL_GET_BAUD_RATE Serial1 SUCCESS
9 0.00000140 COM_serie.exe IOCTL_SERIAL_GET_LINE_CONTROL Serial1 SUCCESS
10 0.00000140 COM_serie.exe IOCTL_SERIAL_GET_CHARS Serial1 SUCCESS
11 0.00000168 COM_serie.exe IOCTL_SERIAL_GET_HANDFLOW Serial1 SUCCESS
12 0.00002095 COM_serie.exe IRP_MJ_WRITE Serial1 SUCCESS Length 1: AA
13 0.10144977 COM_serie.exe IRP_MJ_READ Serial1 TIMEOUT Length 0:
14 0.00000643 COM_serie.exe IRP_MJ_CLEANUP Serial1 SUCCESS
15 0.00542387 COM_serie.exe IRP_MJ_CLOSE Serial1 SUCCESS Length 1: AA |
et avec le logiciel j'ai:
Code:
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
| 0 0.00006425 WinMonext MÚdip IRP_MJ_CREATE Serial1 SUCCESS Options: Open
1 0.00000531 WinMonext MÚdip IOCTL_SERIAL_GET_TIMEOUTS Serial1 SUCCESS
2 0.00002151 WinMonext MÚdip IOCTL_SERIAL_SET_TIMEOUTS Serial1 SUCCESS RI:-1 RM:2000 RC:0 WM:2000 WC:0
3 0.00000335 WinMonext MÚdip IOCTL_SERIAL_GET_BAUD_RATE Serial1 SUCCESS
4 0.00000307 WinMonext MÚdip IOCTL_SERIAL_GET_LINE_CONTROL Serial1 SUCCESS
5 0.00000279 WinMonext MÚdip IOCTL_SERIAL_GET_CHARS Serial1 SUCCESS
6 0.00000279 WinMonext MÚdip IOCTL_SERIAL_GET_HANDFLOW Serial1 SUCCESS
7 0.00000251 WinMonext MÚdip IOCTL_SERIAL_GET_BAUD_RATE Serial1 SUCCESS
8 0.00000251 WinMonext MÚdip IOCTL_SERIAL_GET_LINE_CONTROL Serial1 SUCCESS
9 0.00000251 WinMonext MÚdip IOCTL_SERIAL_GET_CHARS Serial1 SUCCESS
10 0.00000251 WinMonext MÚdip IOCTL_SERIAL_GET_HANDFLOW Serial1 SUCCESS
11 0.00001006 WinMonext MÚdip IOCTL_SERIAL_SET_BAUD_RATE Serial1 SUCCESS Rate: 9600
12 0.00000587 WinMonext MÚdip IOCTL_SERIAL_SET_RTS Serial1 SUCCESS
13 0.00000587 WinMonext MÚdip IOCTL_SERIAL_SET_DTR Serial1 SUCCESS
14 0.00000447 WinMonext MÚdip IOCTL_SERIAL_SET_LINE_CONTROL Serial1 SUCCESS StopBits: 1 Parity: NONE WordLength: 8
15 0.00000307 WinMonext MÚdip IOCTL_SERIAL_SET_CHAR Serial1 SUCCESS EOF:0 ERR:0 BRK:0 EVT:0 XON:11 XOFF:13
16 0.00000419 WinMonext MÚdip IOCTL_SERIAL_SET_HANDFLOW Serial1 SUCCESS Shake:1 Replace:40 XonLimit:2048 XoffLimit:512
17 0.00000279 WinMonext MÚdip IOCTL_SERIAL_GET_BAUD_RATE Serial1 SUCCESS
18 0.00000251 WinMonext MÚdip IOCTL_SERIAL_GET_LINE_CONTROL Serial1 SUCCESS
19 0.00000279 WinMonext MÚdip IOCTL_SERIAL_GET_CHARS Serial1 SUCCESS
20 0.00000251 WinMonext MÚdip IOCTL_SERIAL_GET_HANDFLOW Serial1 SUCCESS
21 0.00000251 WinMonext MÚdip IOCTL_SERIAL_GET_BAUD_RATE Serial1 SUCCESS
22 0.00000279 WinMonext MÚdip IOCTL_SERIAL_GET_LINE_CONTROL Serial1 SUCCESS
23 0.00000279 WinMonext MÚdip IOCTL_SERIAL_GET_CHARS Serial1 SUCCESS
24 0.00000251 WinMonext MÚdip IOCTL_SERIAL_GET_HANDFLOW Serial1 SUCCESS
25 0.00000978 WinMonext MÚdip IOCTL_SERIAL_SET_BAUD_RATE Serial1 SUCCESS Rate: 19200
26 0.00000559 WinMonext MÚdip IOCTL_SERIAL_SET_RTS Serial1 SUCCESS
27 0.00000531 WinMonext MÚdip IOCTL_SERIAL_SET_DTR Serial1 SUCCESS
28 0.00000419 WinMonext MÚdip IOCTL_SERIAL_SET_LINE_CONTROL Serial1 SUCCESS StopBits: 1 Parity: NONE WordLength: 8
29 0.00000279 WinMonext MÚdip IOCTL_SERIAL_SET_CHAR Serial1 SUCCESS EOF:0 ERR:0 BRK:0 EVT:0 XON:11 XOFF:13
30 0.00000419 WinMonext MÚdip IOCTL_SERIAL_SET_HANDFLOW Serial1 SUCCESS Shake:1 Replace:40 XonLimit:100 XoffLimit:100
31 0.00001285 WinMonext MÚdip IOCTL_SERIAL_GET_COMMSTATUS Serial1 SUCCESS
32 0.00002850 WinMonext MÚdip IRP_MJ_WRITE Serial1 SUCCESS Length 1: AA
33 0.00000838 WinMonext MÚdip IOCTL_SERIAL_GET_COMMSTATUS Serial1 SUCCESS
34 0.00001285 WinMonext MÚdip IOCTL_SERIAL_GET_COMMSTATUS Serial1 SUCCESS
35 0.00000698 WinMonext MÚdip IRP_MJ_READ Serial1 SUCCESS Length 1: AA |
il y a une difference de ligne de ligne c'est sur, mais d'ou sa vient ? qu'est ce que j'oublie car là je respecte bien la config fournie :?