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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
| #include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#define RX_SIZE 4096 //taille InPut
#define TX_SIZE 4096 // taille OutPut
#define MAX_WAIT_READ 5000 //temps max de lecture (en ms)
/*====================================================================================================================================
Declaration des fonctions que main utilisera
======================================================================================================================================*/
// Variable de Configuration des ports coms
DCB PCConf =
{
sizeof(DCB), // DCBlength
9600, // 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
};
// variable configuration des delay atentes des pots com
COMMTIMEOUTS DelAtt =
{
MAX_WAIT_READ, /* ReadIntervalTimeOut */
0, /* ReadTotalTimeOutMultiplier */
MAX_WAIT_READ, /* ReadTotalTimeOutConstant */
0, /* WriteTotalTimeOutMultiplier */
0 /* WriteTotalTimeOutConstant */
};
int NCom = 0, nChoix, nBytesWritten, nBytesRead;
BOOL OpenCOM (int NCom) ; // protocole d'ouverture de port.
BOOL CloseCOM (); // protocole de fermeture
BOOL ReadCOM (void* buffer, int nBytesToRead, int* pBytesRead); //protocole lecture
BOOL WriteCOM (void* buffer, int nBytesToWrite, int* pBytesWritten); //protocole ecriture
HANDLE FPCom=NULL; // déclaration de la variable de stockage du port com (Fichier Port Com).
int main()
{
char buffer[256];
printf ("Port com a ouvrir :\n\n") ;
scanf("%d",&NCom) ;
OpenCOM(NCom);
if(!OpenCOM(NCom)) return -1;
printf("\nPort Com%d ouvert.\n",&NCom);
do
{
/* menu */
printf("\r\n");
printf("1 : Donnée en OutPut.\n");
printf("2 : Donnees en Input.\n");
printf("3 : Quitter.\n");
scanf("%d", &nChoix);
if(nChoix == 1) //OutPut
{
printf("\n");
printf("Donnees a envoyer :\n");
fflush(stdin); // fonction vider le buffer (memoire tampon) dans stdin.h
gets(buffer); // gets() contrairement a scanf n'est pas formate.
printf("\n");
printf("Envoi des donnees...\n");
if(WriteCOM(buffer, strlen(buffer), &nBytesWritten))
{
printf("%d octet(s) envoye(s).\n", nBytesWritten);
}
else
{
printf("Erreur lors de l'envoi.\n");
}
}
if(nChoix == 2) //InPut
{
printf("\n");
printf("Reception de donnees...\n");
if(ReadCOM(buffer, sizeof(buffer)-1, &nBytesRead))
{
buffer[nBytesRead] = '\0';
printf("%d octet(s) recu(s) :\n%s\n", nBytesRead, buffer);
}
else
printf("Erreur lors de la réception.\r\n");
}
}while(nChoix != 3);
CloseCOM(); // on ferme pour quitter proprement
return 0;
}
BOOL OpenCOM(int NCom)
{
char ComN [16];
sprintf(ComN, "COM%d", NCom);
FPCom = CreateFile(ComN, GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM, NULL);
if(FPCom ==INVALID_FILE_ATTRIBUTES)
{
if(GetLastError()==ERROR_FILE_NOT_FOUND)
{
printf ("\nERREUR : Port introuvable\n");
}
printf ("\nERREUR : erreur lors de la connection.\n") ;
}
/*initialisations des parametre de communication (cmd API)*/
SetupComm(FPCom, RX_SIZE, TX_SIZE) ;
if(!SetCommTimeouts(FPCom, &DelAtt) || !SetCommState(FPCom, &PCConf))
{
printf("Erreur lors de la configuration du port COM%d", NCom);
CloseHandle(FPCom);
return FALSE;
}
/* on vide les tampons d'émission et de réception, mise à 1 DTR (cmd API) */
PurgeComm(FPCom, PURGE_TXCLEAR|PURGE_RXCLEAR|PURGE_TXABORT|PURGE_RXABORT);
EscapeCommFunction(FPCom, SETDTR);
return TRUE;
}
BOOL CloseCOM()
{
CloseHandle(FPCom);
return TRUE;
}
BOOL ReadCOM(void* buffer, int nBytesToRead, int* pBytesRead)
{
return ReadFile(FPCom, buffer, nBytesToRead, pBytesRead, NULL);
}
BOOL WriteCOM(void* buffer, int nBytesToWrite, int* pBytesWritten)
{
return WriteFile(FPCom, buffer, nBytesToWrite, pBytesWritten, NULL);
} |