FORUM Tom's Hardware » Programmation » C / C++ / Java » Envoie d une valeur sur le port com
 

Envoie d une valeur sur le port com

Il y a 421 utilisateurs connus et inconnus. Pour voir la liste des connectés connus, cliquez ici
Ajouter une réponse



 Mot :   Pseudo :  
 
Bas de page
Auteur
 Sujet : Envoie d une valeur sur le port com
 
Plus d'informations

Salut a tous, j ai realise un programme permettant de changer, d incrementer ou de decrementer une variable "Volume"
J arrive a Convertir "Volume" en string, mais je n arrive pas a l envoyer via le port com, car ma fonction Writecom attend que l utilisateur tape quelquechose, moi j aimerais qu elle envoie directement "Volume" en string.
 

Code :
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <windows.h>
  5. #include <commdlg.h>
  6. #include <math.h>
  7. #include <conio.h>
  8. HANDLE g_hCOM = NULL;
  9. DCB g_dcb =
  10.    {
  11.        sizeof(DCB), /* DCBlength */
  12.        9600, // BaudRate */
  13.        TRUE, /* fBinary */
  14.        FALSE, /* fParity */
  15.        FALSE, /* fOutxCtsFlow */
  16.        FALSE, /* fOutxDsrFlow */
  17.        DTR_CONTROL_ENABLE, /* fDtrControl */
  18.        FALSE, /* fDsrSensitivity */
  19.        FALSE, /* fTXContinueOnXoff */
  20.        FALSE, /* fOutX */
  21.        FALSE, /* fInX */
  22.        FALSE, /* fErrorChar */
  23.        FALSE, /* fNull */
  24.        RTS_CONTROL_ENABLE, /* fRtsControl */
  25.        FALSE, /* fAbortOnError */
  26.        0, /* fDummy2 */
  27.        0, /* wReserved */
  28.        0x100, /* XonLim */
  29.        0x100, /* XoffLim */
  30.        8, /* ByteSize */
  31.        NOPARITY, /* Parity */
  32.        ONESTOPBIT, /* StopBits */
  33.        0x11, /* XonChar */
  34.        0x13, /* XoffChar */
  35.        '?', /* ErrorChar */
  36.        0x1A, /* EofChar */
  37.        0x10 /* EvtChar */
  38.    };
  39.       BOOL OpenCom (int fd);
  40.       BOOL CloseCom ();
  41.       BOOL ReadCom (void* buffer, int nBytesToRead, int* pBytesRead);
  42.       BOOL WriteCom (void* buffer, int nBytesToWritten, int* pBytesWritten);
  43.       BOOL OpenCom(int fd)
  44.       {
  45.         char szCom[16];
  46.         sprintf(szCom,"COM%d", fd);
  47.         g_hCOM= CreateFile(szCom, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM, NULL);
  48.         if (g_hCOM == INVALID_HANDLE_VALUE)
  49.         {
  50.             printf("Erreur dóuverture du port COM%d", fd);
  51.             return FALSE;
  52.         }
  53.       }
  54.       BOOL CloseCom()
  55.         {
  56.             CloseHandle(g_hCOM);
  57.         return TRUE;
  58.     }
  59. int main(int argc, char *argv[])
  60. {
  61.     int nChoice,fd,nBytesWritten;
  62.     float volume;
  63.     volume=0;
  64.     char *pointer;
  65.     char buffer[256];
  66.     printf("Number of port COM :" );
  67.     scanf("%d", &fd);
  68.     printf("Configuration of Com%d\r\n" );
  69.         if(!OpenCom(fd)) return -1;
  70.         printf("....OK\r\n" );
  71.    
  72.     do
  73.     {
  74.     /* menu */
  75.           printf("\r\n" );
  76.           printf("Hello, what do you want to do?\r\n" );
  77.           printf("1 : See the volume.\r\n" );
  78.           printf("2 : Change the volume.\r\n" );
  79.           printf("3 : Loop inc+.\r\n" );
  80.           printf("4 : Loop inc-.\r\n" );
  81.           printf("5 : Transmit to COM1.\r\n" );
  82.           printf("6 : EXIT.\r\n" );
  83.           printf("Your Choice : " );
  84.           scanf("%d", &nChoice);
  85.          
  86.           if(nChoice == 1)
  87.           {
  88.               printf("\r\n" );
  89.               printf("The Volume is :%f\r\n", volume);
  90.           }
  91.           if(nChoice == 2)
  92.           {
  93.           printf("\r\n" );           
  94.           printf("New value for the volume :\n\r" );
  95.           scanf("%f", &volume);
  96.           printf("The new volume is : %f\r\n", volume);
  97.           } 
  98.    
  99.    
  100.               if (nChoice == 3)//Robot goes down
  101.           {
  102.           while(volume<201)
  103.                   {
  104.                       printf("volume = %f\n",volume);
  105.                       volume++;
  106.                   }     
  107.           }   
  108.              
  109.               if (nChoice == 4)//robot goes Up
  110.               {
  111.             while(volume>-1)
  112.             {
  113.                 printf("volume = %f\n", volume);
  114.                 volume--;
  115.             }
  116.               }
  117.                 if (nChoice == 5)//transmit to COM1
  118.                 {
  119.               printf("\r\n" );
  120.               pointer = (char *)&volume;
  121.               printf("New Volume to send: %.5f\n", volume);
  122.               printf("pointer values: %c%c%c%c\n", pointer[0], pointer[1], pointer[2], pointer[3]);
  123.               buffer[256]=*pointer;
  124.              
  125.              
  126.           fflush(stdin);
  127.               gets(pointer);
  128.               printf("\r\n" );
  129.               printf("Sending is running...\n" );
  130.               if(WriteCom(pointer, strlen(pointer), &nBytesWritten))
  131.                   printf("%d Octets were send\n", nBytesWritten);
  132.               else
  133.                   printf("Erreur lors de l'envoi.\r\n" );
  134.                   }
  135. //erreur lors de l envoie
  136.           }
  137. while(nChoice != 6);
  138.  
  139.       /* fermeture du port COM et retour */
  140.       CloseCom();
  141.       return 0;
  142. }
  143. BOOL WriteCom(void* buffer, int nBytesToWrite, int* pBytesWritten)
  144. {
  145.      /* écriture sur le port */
  146.     return WriteFile(g_hCOM, buffer, nBytesToWrite, (DWORD*)pBytesWritten, NULL);
  147. }
  148. //Est Solarus oth Mithas et Tenebrae Non Victix Lux


 
si quelqun pouvais me dire si il connait une fonction qui envoie une valeur via le port com je le remercie !


Message édité par araschbab le 15-05-2006 à 13:32:19

G.M.G.V.S.O.
Profil : Modo matériel
Plus d'informations

Aller à :
Ajouter une réponse
  FORUM Tom's Hardware » Programmation » C / C++ / Java » Envoie d une valeur sur le port com
 

Annonces Google
Publicité
Les ressources relatives