Protocole RS232 en C++ ou C

angeva Messages postés 5 Date d'inscription mercredi 16 avril 2003 Statut Membre Dernière intervention 8 janvier 2004 - 17 avril 2003 à 15:08
luc84fr Messages postés 1 Date d'inscription dimanche 20 juillet 2003 Statut Membre Dernière intervention 4 octobre 2003 - 4 oct. 2003 à 18:37
Au secour!!!
je commence la programmation au boulot et je dois créer un logiciel permettant la communication entre un equipement quelconque informatisé et un PC par une lliaison RS232.J'utilise DevCpp pour programmer ou Turbo C++. J'espere que quelqu'un pourra m'aider parce que je ne m'en sort pas!merci d'avance. :question)

4 réponses

crocejf2000 Messages postés 260 Date d'inscription lundi 27 janvier 2003 Statut Membre Dernière intervention 27 août 2008 1
17 avril 2003 à 16:51
Utilise CreateFile pour passer par le port com. Mais cherche bien, ya pas mal de doc sur l'UART et RS232
Hart
0
GodFa69 Messages postés 6 Date d'inscription lundi 7 avril 2003 Statut Membre Dernière intervention 26 novembre 2003
18 avril 2003 à 19:20
l'open source; cela devrai etre obligatoire
0
GodFa69 Messages postés 6 Date d'inscription lundi 7 avril 2003 Statut Membre Dernière intervention 26 novembre 2003
18 avril 2003 à 19:25
dsl pour le 1er message g foire :)

sinon pour ton pb voici la réponse en c++
pour ce qui est d param d fcts je te laisse le loisir de choisir ce qui te convienne le mieu

// destructeur
CComm::~CComm()
{
// fermeture de la voie de communication
CloseHandle(VoieDeCom);
}

// fonction d'initialisation
void CComm::CCommInit(bool paritee,int start,int stop,int vitesse,int donnees,CString port)
{
// configuration de la ligne série:
VoieDeCom=CreateFile(port,GENERIC_READ|GENERIC_WRITE,0,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);

// recuperation de l'état actuel du port
GetCommState(VoieDeCom,¬reDcb);
// init de la taille des données à transférer
notreDcb.ByteSize=donnees; notreDcb.BaudRate=CBR_9600; // init de la vitesse de communication

if (paritee == false)
notreDcb.Parity = NOPARITY; // pas de parité
else notreDcb.Parity = EVENPARITY; // parité
notreDcb.fBinary=false; //""""""""""""""""""""""""""""""""""
notreDcb.fParity=false;
if (stop == 1)
notreDcb.StopBits = ONESTOPBIT; // 1 bit de stop
else if(stop == 2)
notreDcb.StopBits = TWOSTOPBITS; // 2 bits de stop
else AfxMessageBox("Erreur, le nombre de bits de stop doit être compris entre 1 et 2",MB_ICONSTOP);
//message d erreur au cas le nombre de bits de stop soit //différents de 1 ou 2

// initialisation de certains paramètres du port
SetCommState(VoieDeCom,¬reDcb);
// On vide les canaux de communication
// PurgeComm(VoieDeCom,PURGE_TXABORT | PURGE_RXABORT);
PurgeComm(VoieDeCom,PURGE_TXCLEAR | PURGE_RXCLEAR);

}

// fonction envoyer
void CComm::Envoyer(const CString& Envoi)
{
unsigned long tmp;
tmp=Envoi.GetLength();
// envoie des données sur la ligne
WriteFile(VoieDeCom,Envoi,Envoi.GetLength(),&tmp,NULL);
}

// fonction recevoir 2 caractères
CString CComm::Recevoir()
{
DWORD nbcarlu;
//CString tramerecu;
char tramerecu[8];
ReadFile(VoieDeCom,/*(LPVOID*)*/(char*)tramerecu,2,&nbcarlu,NULL);// reception des données sur la ligne
tramerecu[nbcarlu]='\0';
return tramerecu; // retour de l adresse du tableau
}

A savoir qu il existe ossi un activeX nommé Mscomm
0
luc84fr Messages postés 1 Date d'inscription dimanche 20 juillet 2003 Statut Membre Dernière intervention 4 octobre 2003
4 oct. 2003 à 18:37
Voici un source trouvé sur le web et fonctionnant avec DEV-C++ v5

////////////////////////////////////
// Base class for SERIAL_COMMUNICATION, nonoverlapped
//
// File : serial.h
//
// Author : L. Saint-Marcel
// lstmarcel@yahoo.fr
//
// Date : 06/31/01
////////////////////////////////////

#ifndef __SERIAL_H_LSM__
#define __SERIAL_H_LSM__

#include <windows.h>
#include <commctrl.h>

#define DEFAULT_READ_TIMEOUT 500 // milliseconds

#ifdef SERIAL_ERROR
#undef SERIAL_ERROR
#endif

#ifdef SERIAL_SUCCESS
#undef SERIAL_SUCCESS
#endif

#define SERIAL_ERROR -1
#define SERIAL_SUCCESS 0

#define DEFAULT_PORT_NAME "COM1"

class serialCL{

public :
serialCL();
~serialCL();
int open(char* gszPort = DEFAULT_PORT_NAME,
long speed = 9600,
int parity = NOPARITY,
int bitslength = 8,
int stopbit = ONESTOPBIT);

int close();

// send a request to read length bytes, do not wait for them...
int read(unsigned char * buffer,
unsigned long & length);

// read length Bytes (wait for them), return only when read =length or read error
int forceRead(unsigned char * lpBuf,
unsigned long &length,
int retry=-2); // infinite retry

int write(unsigned char * buffer,
unsigned long length);

int status();

private :
char gszPort_[256];
HANDLE CommPortHandle; //Holds the handle to the communications port returned by windows.
COMMTIMEOUTS CommPortTimeOut; // This data structure is programmed with timeout parameters for the serial port
DCB CommPortDCB; // This data structure is programmed with the serial port parameters.
short PortInitSuccessful; // This data member is assigned 1 if the serial port is successfully initialized, otherwise it is 0.
unsigned long BytesTransferred; // Holds the number of bytes transferred during a serial port read or write operation. This is here just as a placeholder for windows API functions.

};

#endif

////////////////////////////////////
// Base class for SERIAL_COMMUNICATION, nonoverlapped
//
// File : serial.cpp
//
// Author : L. Saint-Marcel
// lstmarcel@yahoo.fr
//
// Date : 06/31/01
////////////////////////////////////

#include <stdio.h>
#include "serial.h"

///////////////////////////////////////
// serialCL - Constructor, destructor
///////////////////////////////////////
serialCL::serialCL()
{
CommPortHandle = 0;
}

serialCL::~serialCL()
{

}

//////////////////////////////////
// open
//////////////////////////////////
int serialCL::open(char * gszPort, long speed, int parity, int bitslength, int stopbit)
{

CommPortTimeOut.ReadIntervalTimeout = 2;
CommPortTimeOut.ReadTotalTimeoutMultiplier = 1;
CommPortTimeOut.ReadTotalTimeoutConstant = 1;
CommPortTimeOut.WriteTotalTimeoutMultiplier = 0;
CommPortTimeOut.WriteTotalTimeoutConstant = 0;

PortInitSuccessful = 1;
strcpy(gszPort_, gszPort);
// close serial port if already opened
if(CommPortHandle != 0) close();

// Code for opening CommPortHandle using CreateFile goes here
// CommPortHandle = CreateFile(gszPort_,GENERIC_READ | GENERIC_WRITE,0,0,OPEN_EXISTING,NULL,0);
CommPortHandle = CreateFile(gszPort_,GENERIC_READ + GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);

if(CommPortHandle == INVALID_HANDLE_VALUE)
{
//printf("CreateFile ERROR\n");
PortInitSuccessful = 0;
return SERIAL_ERROR;
}
else
{ //Code implemented only for a valid handle
if(!SetCommTimeouts(CommPortHandle,&CommPortTimeOut))
{
//printf("SetCommTimeouts failed");
PortInitSuccessful = 0;
return SERIAL_ERROR;
}

// clear the memory used by the CommPortDCB object
memset(&CommPortDCB,0,sizeof(CommPortDCB));

// set DCB
CommPortDCB.DCBlength = sizeof(CommPortDCB);
CommPortDCB.BaudRate = speed;
CommPortDCB.Parity = parity;
CommPortDCB.StopBits = stopbit;
CommPortDCB.ByteSize = bitslength;

if(!SetCommState(CommPortHandle,&CommPortDCB))
{
//printf("SetCommState failed");
PortInitSuccessful = 0;
return SERIAL_ERROR;
}
}
return SERIAL_SUCCESS;
}

//////////////////////////////////
// close
//////////////////////////////////
int serialCL::close()
{
if(CommPortHandle!=0)
CloseHandle(CommPortHandle);
CommPortHandle = 0;
return SERIAL_SUCCESS;
}

//////////////////////////////////
// status
//////////////////////////////////
int serialCL::status()
{
if(CommPortHandle!=0)
{
memset(&CommPortDCB,0,sizeof(CommPortDCB));
if(GetCommState(CommPortHandle,&CommPortDCB))
{
printf("Vitesse : %d\n",CommPortDCB.BaudRate);
printf("Parite : %d\n",CommPortDCB.Parity);
printf("Stop bit : %d\n",CommPortDCB.StopBits);
printf("Nbre bit : %d\n",CommPortDCB.ByteSize);

}
else
return SERIAL_ERROR;
}

return SERIAL_SUCCESS;
}

//////////////////////////////////
// read
//////////////////////////////////
int serialCL::read(unsigned char * lpBuf, unsigned long &length)
{
if( !ReadFile(CommPortHandle, lpBuf, length, &BytesTransferred, NULL) )
{
return SERIAL_ERROR;
}
if(BytesTransferred != length) {
length = BytesTransferred;
return SERIAL_ERROR;
}
length = BytesTransferred;
return SERIAL_SUCCESS;
}

//////////////////////////////////
// force read
//////////////////////////////////
int serialCL::forceRead(unsigned char * lpBuf, unsigned long &length, int retry)
{
unsigned long l2 = 0;
while(l2!=length && retry!=-1) {
if( !ReadFile(CommPortHandle, lpBuf+l2, length-l2, &BytesTransferred, NULL) )
{
return SERIAL_ERROR;
}
l2+=BytesTransferred;
--retry;
if(retry<-1) retry=-2;
}
length = BytesTransferred;
if(l2==length)
return SERIAL_SUCCESS;
else
return SERIAL_ERROR;
}

//////////////////////////////////
// write
//////////////////////////////////
int serialCL::write(unsigned char * lpBuf, unsigned long length)
{
if( !WriteFile(CommPortHandle, lpBuf, length, &BytesTransferred, NULL) )
{
return SERIAL_ERROR;
}
if(BytesTransferred != length)
return SERIAL_ERROR;
return SERIAL_SUCCESS;
}
0
Rejoignez-nous