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
|
#include <vcl.h>
#include <stdio.h>
#include <windows.h>
#include <windef.h>
#include <stdlib.h>
#include <iostream.h>
#include <limits.h>
#pragma hdrstop
//---------------------------------------------------------------------------
#pragma argsused
int main(int argc, char* argv[])
{
HANDLE handle;
DCB dcb;
BOOL PortReady;
BOOL fSuccess;
DWORD nblu=0,nbecrit;
/*--------------------------------------------------------------------*/
/* Ouverture du port de communication pour la base de collecte */
/*--------------------------------------------------------------------*/
handle = CreateFile(
"COM5",
GENERIC_READ | GENERIC_WRITE , // acces pour lire et ecrire sur le port
0,
NULL,
OPEN_EXISTING,
0,
NULL
) ;
if(handle == INVALID_HANDLE_VALUE)
{
printf("Impossible d'ouvrir le port (erreur %d)\n", GetLastError());
return 0;
}
printf("Ouverture du port reussie");
return 0;
PortReady=SetupComm(handle,5000,5000);
fSuccess = GetCommState(handle, &dcb);
if (!fSuccess)
{
printf("probleme 1");
getchar();
}
dcb.BaudRate = 38400 ; /* vitesse */
dcb.ByteSize =8 ; /* nombre de bits */
dcb.Parity=NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.fNull=FALSE;
dcb.fRtsControl=RTS_CONTROL_ENABLE;
dcb.fInX=FALSE;
dcb.fOutX=FALSE;
dcb.fDtrControl=DTR_CONTROL_ENABLE;
/*-----------------------------------------------------------*/
/* on applique les changements au port COM1 */
/*-----------------------------------------------------------*/
PortReady=SetCommState(handle,&dcb);
if(!SetCommState(handle,&dcb))
{
printf("probleme 1");
getchar();
}
char buf;
ReadFile(handle,&buf,sizeof(buf),&nblu,NULL);
printf ("octets lus : %h ", buf );
CloseHandle(handle); |
Partager