begin process at 2012 05 30 10:56:31
  Trouver un code source :
 
dans
 
Accueil > Forum > 

Archive C/C++

 > 

Archives

 > 

Périphériques

 > 

protocole RS232 en C++ ou C


Derniers messages déposésPoser une question dans le forum ou lancer une discussion

protocole RS232 en C++ ou C

jeudi 17 avril 2003 à 15:08:28 | protocole RS232 en C++ ou C

angeva

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.
jeudi 17 avril 2003 à 16:51:49 | Re : protocole RS232 en C++ ou C

crocejf2000

Utilise CreateFile pour passer par le port com. Mais cherche bien, ya pas mal de doc sur l'UART et RS232
Hart


-------------------------------
Réponse au message :
-------------------------------

> 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.
vendredi 18 avril 2003 à 19:20:33 | Re : protocole RS232 en C++ ou C

GodFa69


l'open source; cela devrai etre obligatoire


-------------------------------
Réponse au message :
-------------------------------

> Utilise CreateFile pour passer par le port com. Mais cherche bien, ya pas mal de doc sur l'UART et RS232
> Hart
>
>
> -------------------------------
> Réponse au message :
> -------------------------------
>
> > 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.
>
vendredi 18 avril 2003 à 19:25:00 | Re : protocole RS232 en C++ ou C

GodFa69

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
samedi 4 octobre 2003 à 18:37:21 | Re : protocole RS232 en C++ ou C

luc84fr

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;
}




Cette discussion est classée dans : rs232, protocole


Répondre à ce message

Sujets en rapport avec ce message

bug sur rs232 [ par yo972 ] salut !j'ai réalisé un programme sur la rs232 sur C++Builder,je dois envoyer un chiffre sur la ligne rs232.j'ai réussi à l'exécuter une fois mais aprè Piti probleme reseau. [ par bluehat ] Bon alors voila.Je suis étudiant en BTS informatique industrielle et je dois, pour mon projet réaliser un programme qui doit référencer tous les compo Liaison série RS232 [ par NerOcrO ] Je cherche des programmes pour m'aider à programmer sur la liaison série RS232 en DOS. RS232 [ par bryg ] Salut,Comment mettre en place une liaison RS232 entre 2 PC.Merci de l'aide voici mon E-Mail: nofrifr@yahoo.fra+. Envoi d'un fichier XML par le protocole HTTP [ par Tof ] Bonjour tout le monde,J'aurais voulu savoir comment envoyer un fichier XML par l'intermédiaire du protocole HTTP en utilisant la méthode POST.Merci d' com serie RS232 [ par hotrod1 ] Y'a t il quelqu'un qui saurait programmer la lecture / écriture d'un port serie rs232 sous visual c++ 6 , et sous win 2000 ???Sinon, si c'est possible com serie rs232 [ par hotrod1 ] Y'a t il quelqu'un qui saurait programmer la lecture / écriture d'un port serie rs232 sous visual c++ 6 , et sous win 2000 ???Sinon, si c'est possible com serie rs232 . [ par hotrod1 ] Y'a t il quelqu'un qui saurait programmer la lecture / écriture d'un port serie rs232 sous visual c++ 6 , et sous win 2000 ???Sinon, si c'est possible protocole DLC en c++ [ par syl81 ] SalutJ'utilise le protocole DLC pour communiquer avec une imprimante et j'ai un problème avec la fonction AcsLan() : à l'édition de liens j'ai une err thread et RS232 [ par icarus13 ] Quelqu'un peut m'aider ? Je dois lancer plusieurs thread à la fois dont qui envoie un tableau par la liaison série !Merci !Yan


Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Mai 2012
LMMJVSD
 123456
78910111213
14151617181920
21222324252627
28293031   

Consulter la suite du CalendriCode

Photothèque

A découvrir



 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,187 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales