Envoie d une valeur sur le port com

  • Auteur de la discussion araschbab
  • Date de début

araschbab

Nouveau membre
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.

[cpp]#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <commdlg.h>
#include <math.h>
#include <conio.h>

HANDLE g_hCOM = NULL;

DCB g_dcb =
{
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 */
};


BOOL OpenCom (int fd);
BOOL CloseCom ();
BOOL ReadCom (void* buffer, int nBytesToRead, int* pBytesRead);
BOOL WriteCom (void* buffer, int nBytesToWritten, int* pBytesWritten);


BOOL OpenCom(int fd)
{
char szCom[16];

sprintf(szCom,"COM%d", fd);
g_hCOM= CreateFile(szCom, GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_SYSTEM, NULL);

if (g_hCOM == INVALID_HANDLE_VALUE)
{
printf("Erreur dóuverture du port COM%d", fd);
return FALSE;

}

}


BOOL CloseCom()
{
CloseHandle(g_hCOM);
return TRUE;
}

int main(int argc, char *argv[])
{

int nChoice,fd,nBytesWritten;
float volume;
volume=0;

char *pointer;

char buffer[256];



printf("Number of port COM :");
scanf("%d", &fd);

printf("Configuration of Com%d\r\n");
if(!OpenCom(fd)) return -1;
printf("....OK\r\n");

do
{

/* menu */
printf("\r\n");
printf("Hello, what do you want to do?\r\n");
printf("1 : See the volume.\r\n");
printf("2 : Change the volume.\r\n");
printf("3 : Loop inc+.\r\n");
printf("4 : Loop inc-.\r\n");
printf("5 : Transmit to COM1.\r\n");
printf("6 : EXIT.\r\n");
printf("Your Choice : ");
scanf("%d", &nChoice);

if(nChoice == 1)
{
printf("\r\n");
printf("The Volume is :%f\r\n", volume);
}

if(nChoice == 2)
{
printf("\r\n");
printf("New value for the volume :\n\r");
scanf("%f", &volume);
printf("The new volume is : %f\r\n", volume);
}


if (nChoice == 3)//Robot goes down
{
while(volume<201)
{
printf("volume = %f\n",volume);
volume++;
}
}


if (nChoice == 4)//robot goes Up
{
while(volume>-1)
{
printf("volume = %f\n", volume);
volume--;
}

}

if (nChoice == 5)//transmit to COM1
{
printf("\r\n");
pointer = (char *)&volume;
printf("New Volume to send: %.5f\n", volume);
printf("pointer values: %c%c%c%c\n", pointer[0], pointer[1], pointer[2], pointer[3]);

buffer[256]=*pointer;


fflush(stdin);
gets(pointer);
printf("\r\n");
printf("Sending is running...\n");
if(WriteCom(pointer, strlen(pointer), &nBytesWritten))
printf("%d Octets were send\n", nBytesWritten);
else
printf("Erreur lors de l'envoi.\r\n");
}
//erreur lors de l envoie

}
while(nChoice != 6);

/* fermeture du port COM et retour */
CloseCom();
return 0;
}

BOOL WriteCom(void* buffer, int nBytesToWrite, int* pBytesWritten)
{
/* écriture sur le port */
return WriteFile(g_hCOM, buffer, nBytesToWrite, (DWORD*)pBytesWritten, NULL);
}


//Est Solarus oth Mithas et Tenebrae Non Victix Lux

[/cpp]

si quelqun pouvais me dire si il connait une fonction qui envoie une valeur via le port com je le remercie !
 
Vous devez vous inscrire ou vous connecter pour répondre ici.
Derniers messages publiés
Statistiques globales
Discussions
730 134
Messages
6 718 070
Membres
1 586 394
Dernier membre
Manoushk
Partager cette page
Haut