angeva
Messages postés5Date d'inscriptionmercredi 16 avril 2003StatutMembreDernière intervention 8 janvier 2004
-
17 avril 2003 à 15:08
luc84fr
Messages postés1Date d'inscriptiondimanche 20 juillet 2003StatutMembreDerniè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)
GodFa69
Messages postés6Date d'inscriptionlundi 7 avril 2003StatutMembreDernière intervention26 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
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
////////////////////////////////////
//////////////////////////////////
// open
//////////////////////////////////
int serialCL::open(char * gszPort, long speed, int parity, int bitslength, int stopbit)
{