Se connecter avec
S'enregistrer | Connectez-vous

Envoie d une valeur sur le port com

Dernière réponse : dans Programmation

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.

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

Autres pages sur : envoie valeur port com

Lassé par la pub ? Créez un compte
Lassé par la pub ? Créez un compte