fab@c++ | Salut,
Mon code récupère une chaîne qu'il stocke dans un fichier texte. Voici le genre de chaîne que je récupère via le port série :
Code :
- 111111111111111111111111111111111111111111111111 196 202 179 255 1111111111111111111111111111111111111111111111111111111111111111111111 197 201 181 255 1111111111111111111111111111111111111111111111
|
Et voici mon code c++ . Attention, il possède une partie SDL. Si t'as des questions, n'hésite pas à poster
Code :
- #include <conio.h>
- #include <string.h>
- #include <windows.h>
- #include <SDL/SDL.h>
- #define RX_SIZE 16000
- #define TX_SIZE 16000
- #define MAX_WAIT_READ 1000
- HANDLE g_hCOM = NULL;
- COMMTIMEOUTS g_cto ={
- MAX_WAIT_READ,0,MAX_WAIT_READ,0,0
- };
- DCB g_dcb ={
- sizeof(DCB),9600,TRUE,FALSE,FALSE,FALSE,DTR_CONTROL_ENABLE,FALSE,FALSE,FALSE,FALSE,FALSE,FALSE,RTS_CONTROL_ENABLE,FALSE,0,0,0x100,0x100,8,NOPARITY,ONESTOPBIT,0x11,0x13,'?',0x1A,0x10
- };
- BOOL OpenCOM (int nId);
- BOOL CloseCOM ();
- static BOOL ReadCOM (void* buffer, int nBytesToRead, int* pBytesRead);
- int main(int argc, char *argv[]){
- //SDL ici
- SDL_Surface *ecran = NULL, *rectangle_1 = NULL, *rectangle_2 = NULL, *rectangle_3 = NULL, *rectangle_4 = NULL;
- SDL_Rect position_1, position_2, position_3, position_4;
- SDL_Init(SDL_INIT_VIDEO);
- ecran = SDL_SetVideoMode(480,480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF );
- rectangle_1 = SDL_CreateRGBSurface(SDL_HWSURFACE, 240,240, 32, 0, 0, 0, 0);
- rectangle_2 = SDL_CreateRGBSurface(SDL_HWSURFACE, 240,240, 32, 0, 0, 0, 0);
- rectangle_3 = SDL_CreateRGBSurface(SDL_HWSURFACE, 240,240, 32, 0, 0, 0, 0);
- rectangle_4 = SDL_CreateRGBSurface(SDL_HWSURFACE, 240,240, 32, 0, 0, 0, 0);
- position_1.x = 0; position_1.y = 240;
- position_2.x = 240; position_2.y = 240;
- position_3.x = 0; position_3.y = 0;
- position_4.x = 240; position_4.y = 0;
- SDL_WM_SetCaption("electronic eye", NULL);
- //fin SDL
- static const unsigned short int nId=4; //relatif à l'affichage
- static unsigned char buffer[200]; //le buffer qui contient la chaîne récupérée via le port série
- static int nBytesRead;
- static unsigned short int i=0;
- static unsigned short int sensor[10] = {0};
- static unsigned char sensor_1[1];
- static unsigned char sensor_3[]="1111111111 ";
- FILE *fichier = NULL;
- if(!OpenCOM(nId)) return -1;
- while(1) { //boucle de la SDL
- if(ReadCOM(buffer, sizeof(buffer)-2, &nBytesRead)){ //ON lit le port série
- buffer[nBytesRead] = '\0';
- fichier = fopen("test.txt", "w+" ); //on enregistre dans le fichier
- fputs(buffer, fichier);
- fclose(fichier);
- fichier = fopen("test.txt", "r+" );
- if (fichier != NULL){ // on récupère et on affiche
- fscanf(fichier, "%s %d %d %d %d", &sensor_1[0], &sensor[0], &sensor[1], &sensor[2], &sensor[3]);
- fclose(fichier);
- if(sensor[0]>255 || sensor[0]<0 || sensor[0]==111) sensor[0]=((sensor[1]+sensor[2]+sensor[3])/3);
- if(sensor[1]>255 || sensor[1]<0 || sensor[1]==111) sensor[1]=((sensor[0]+sensor[2]+sensor[3])/3);
- if(sensor[2]>255 || sensor[2]<0 || sensor[2]==111) sensor[2]=((sensor[1]+sensor[0]+sensor[3])/3);
- if(sensor[3]>255 || sensor[3]<0 || sensor[3]==111) sensor[3]=((sensor[1]+sensor[2]+sensor[0])/3);
- SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, sensor[0],sensor[0],sensor[0]));
- SDL_FillRect(rectangle_1, NULL, SDL_MapRGB(ecran->format, sensor[0],sensor[0],sensor[0]));
- SDL_BlitSurface(rectangle_1, NULL, ecran, &position_1);
- SDL_FillRect(rectangle_2, NULL, SDL_MapRGB(ecran->format, sensor[1],sensor[1],sensor[1]));
- SDL_BlitSurface(rectangle_2, NULL, ecran, &position_2);
- SDL_FillRect(rectangle_3, NULL, SDL_MapRGB(ecran->format, sensor[2],sensor[2],sensor[2]));
- SDL_BlitSurface(rectangle_3, NULL, ecran, &position_3);
- SDL_FillRect(rectangle_4, NULL, SDL_MapRGB(ecran->format, sensor[3],sensor[3],sensor[3]));
- SDL_BlitSurface(rectangle_4, NULL, ecran, &position_4);
- SDL_Flip(ecran);
- }
- }
- i++;
- }
- CloseCOM();
- SDL_Quit();
- return 0;
- }
- BOOL OpenCOM(int nId){
- char szCOM[16];
- sprintf(szCOM, "COM%d", nId);
- g_hCOM = CreateFile(szCOM, GENERIC_READ|GENERIC_WRITE, 0, NULL,
- OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM, NULL);
- if(g_hCOM == INVALID_HANDLE_VALUE){
- printf("Erreur lors de l'ouverture du port COM%d", nId);
- return FALSE;
- }
- SetupComm(g_hCOM, RX_SIZE, TX_SIZE);
- 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;
- }
- PurgeComm(g_hCOM, PURGE_TXCLEAR|PURGE_RXCLEAR|PURGE_TXABORT|PURGE_RXABORT);
- EscapeCommFunction(g_hCOM, SETDTR);
- return TRUE;
- }
- BOOL CloseCOM(){
- CloseHandle(g_hCOM);
- return TRUE;
- }
- static BOOL ReadCOM(void* buffer, int nBytesToRead, int* pBytesRead){
- return ReadFile(g_hCOM, buffer, nBytesToRead, pBytesRead, NULL);
- }
|
|