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 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
| #include <cstdlib>
#include <iostream>
#include <windows.h>
#include "gestion_fichier.h"
/*=============================================================================
Définition de constantes
=============================================================================*/
#define RX_SIZE 4096 /* taille tampon d'entrée */
#define TX_SIZE 4096 /* taille tampon de sortie */
using namespace std;
/*=============================================================================
Fonctions du module.
=============================================================================*/
bool WriteData(HANDLE handle, BYTE*, DWORD, DWORD*);
bool ReadData(HANDLE, BYTE*, DWORD, DWORD*,UINT);
/******************************************************************************
main : point d'entrée du programme.
******************************************************************************/
int main(int argc, char *argv[])
{
//variables utilisées dans l'appel de ReadFile et WriteFile
DWORD* dwWritten;
BYTE data[RX_SIZE];
UINT timeout=1000;
// int toto = 1000;
//opening a COM port
HANDLE hMasterCOM = CreateFile("\\\\.\\COM3", GENERIC_READ|GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,NULL);
if(hMasterCOM == INVALID_HANDLE_VALUE)
{
cout<<"Erreur lors de l'ouverture du port COM"<<endl;
return FALSE;
}
//Preparing an open COM port for data transmission
PurgeComm(hMasterCOM, PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|
PURGE_RXCLEAR);
/* affectation taille des tampons d'émission et de réception */
SetupComm(hMasterCOM, RX_SIZE, TX_SIZE);
//saving the COM port's Original State
DCB dcbMasterInitState;
GetCommState(hMasterCOM, & dcbMasterInitState);
//Setting up a DCB Structure to set the new COM State
DCB dcbMaster = dcbMasterInitState;
dcbMaster.BaudRate= 921600; // debit (bauds)
dcbMaster.Parity= NOPARITY; // disable parity checking
dcbMaster.ByteSize= 8; // number of bits/byte, 4-8
dcbMaster.StopBits= ONESTOPBIT;// 0,1,2 = 1, 1.5, 2
SetCommState(hMasterCOM, &dcbMaster);
//http://msdn.microsoft.com/en-us/library/ms810467.aspx
// Time out
COMMTIMEOUTS CommTimeouts;
memset( &CommTimeouts, 0, sizeof(CommTimeouts));
CommTimeouts.ReadIntervalTimeout =MAXDWORD;//1;//1000;
CommTimeouts.ReadTotalTimeoutMultiplier =0;//1;// 1000;
CommTimeouts.ReadTotalTimeoutConstant = 0;//4000;
CommTimeouts.WriteTotalTimeoutMultiplier = 0;//1000;
CommTimeouts.WriteTotalTimeoutConstant = 0;//2000;
SetCommTimeouts(hMasterCOM,&CommTimeouts);
//attente pour que les modifications soient prises en compte
Sleep(60);
//si int toto est mis en commentaire : pas de plantage
//int toto = 1000;
printf("MAXDWORD: %ld \r\n", MAXDWORD);
//lancement du FPGA
data[0]=65;
//data[0]=90;
data[1]=0;
//si length et *dwRead sont declaré au debut : 4 donnees erronées
DWORD length=2;
DWORD* dwRead;
if(WriteData(hMasterCOM,data,length,dwWritten)==true)
{
cout<<"ecriture terminee: "<<*dwWritten<<endl;
}
Sleep(20);
//stop the FPGA
data[0]=0;
length=1;
if(WriteData(hMasterCOM,data,length,dwWritten)==true)
{
cout<<"FPGA Arrete"<<endl;
}
//initialisation du tableau contenant les data
for(int i=0;i<RX_SIZE;i++)
{
data[i]=0;
}
//Reading data
cout<<"init tableau et lancement lecture"<<endl;
ReadData(hMasterCOM,data,sizeof(data)-1,dwRead,timeout);
cout<<"lecture terminee: nb octets lus: "<<*dwRead<<endl;
//affichage tableau
int compare=0;
int error=0;
for(int i=0;i<*dwRead;i++)
{
if(data[i]!=compare)
{
// cout<<"erreur sur ligne : "<<i<<endl;
// printf("i-2: %ld \r\n", data[i-2]);
// printf("i-1: %ld \r\n", data[i-1]);
// printf("i : %ld \r\n", data[i]);
// printf("i+1: %ld \r\n", data[i+1]);
// printf("i+2: %ld \r\n", data[i+2]);
// printf("i+13: %ld \r\n", data[i+3]);
//printf("probleme ligne: %i",i);
error++;
}
compare++;
if(compare==14)compare=0;
//cout<<"data[0]: "<<data[i]<<endl;
// printf("%d octet(s) recu(s) : %ld \r\n", *dwRead, data[i]);
}
//Closing the COM port
CloseHandle(hMasterCOM);
hMasterCOM = INVALID_HANDLE_VALUE;
cout<<"ERROR: "<<error<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
/******************************************************************************
WriteData : ecriture sur le port COM.
retour : vrai si l'opération a réussi, faux sinon.
******************************************************************************/
bool WriteData(HANDLE handle, BYTE* data, DWORD length, DWORD* dwWritten)
{
bool success= false;
if (!WriteFile(handle, (LPCVOID)data, length, dwWritten, NULL))
{
if (GetLastError() == ERROR_IO_PENDING)
success = true;
}
else
success = true;
if (*dwWritten != length)
success = false;
return success;
}
/******************************************************************************
WriteData : lecture sur le port COM.
retour : vrai si l'opération a réussi, faux sinon.
******************************************************************************/
bool ReadData(HANDLE handle, BYTE* data, DWORD length, DWORD* dwRead, UINT timeout)
{
bool success= false;
if (!ReadFile(handle, data, length, dwRead, NULL))
{
if (GetLastError() == ERROR_IO_PENDING)
success = true;
}
else
success = true;
return success;
} |
Partager