File "com.cpp"
Full Path: /home/analogde/www/port parallele/builder/com.cpp
File size: 6.42 KB
MIME-type: text/x-c
Charset: 8 bit
// FICHIERS D'INCLUSION
#include <windows.h>
#include <winbase.h>
#include <stdio.h>
#include <conio.h>
#include <string.h>
// CONSTANTES
//-------------------------------------------------------------------------------
// Dfinition du code d'erreurs
typedef enum
{
e_ErrCom_None, // Pas d'erreur
e_ErrCom_Creation, // Erreur lors de la cration du flux
e_ErrCom_Utilise, // Le port com est dj utilis
e_ErrCom_Inexistant, // Le port com n'existe pas
e_ErrCom_Timeout, // Timeout lors d'une mission-rception
e_ErrCom_Emission, // Erreur lors de l'mission
e_ErrCom_Reception, // Erreur lors de la rception
e_ErrCom_Definition_Trame, // Erreur de dfinition de la trame
e_ErrCom_Nack, // Demande non prise en coompte
e_ErrCom_Checksum // Erreur de checksum
} e_ErrCom;
// Nom du port srie
#define PORT1 "COM1"
#define PORT2 "COM2"
// Dfinition des vitesses de communication
#define V1200 1200
#define V2400 2400
#define V4800 4800
#define V9600 9600
// Dfinition du nombre de bits
#define BITS_7 7
#define BITS_8 8
// Dfinition du nombre de bits de stop
#define BIT_DE_STOP_1 1
#define BIT_DE_STOP_2 2
// Dfinition de la parit
#define PAS_DE_PARITE 'N'
#define PARITE_IMPAIRE 'O'
#define PARITE_PAIRE 'E'
// variables globales
HANDLE hComm = NULL;
// autre formalisme HANDLE hComm = 0;
DCB PortDCB;
DWORD BaudRate, ByteSize, Parity,StopBits;
// Variable des erreurs de com
e_ErrCom g_ErrCom= e_ErrCom_None;
//TRead *ReadThread;
//COMMTIMEOUTS ctmoNew = {0}, ctmoOld;
//CString device;
//device = "Com1";
int main (void)
{
BOOL flag;
hComm = CreateFile("COM1",
GENERIC_READ | GENERIC_WRITE,
0,
0,
OPEN_EXISTING,
0,
0);
// si le port ne peut tre ouvert
if(hComm == INVALID_HANDLE_VALUE)
{
// Echec
g_ErrCom=e_ErrCom_Creation;
printf("Could not open file (error %d)\n", GetLastError());
}
// si le port ne peut tre ouvert
if(hComm == INVALID_HANDLE_VALUE)
printf("Could not open file (error %d)\n", GetLastError());
//Rcupration des informations du port
// GetCommState (hPort, // instance d'accs de votre port srie
// &PortDCB); // Structure de configuration
/*GetCommState(hComm, &PortDCB);*/
// On paramtre le port srie
PortDCB.DCBlength = sizeof(DCB);
//Configuration actuelle
GetCommState(hComm, &PortDCB);
//lecture du DCB
BaudRate=PortDCB.BaudRate;
ByteSize=PortDCB.ByteSize;
Parity=PortDCB.Parity;
StopBits=PortDCB.StopBits;
printf(" baud %u .... taille %u .... parit %u ..... stop %u\n.", BaudRate, ByteSize, Parity,StopBits);
/*-----------------------------------------------*/
/* Configurer le port */
/*-----------------------------------------------*/
flag = SetCommState(hComm, &PortDCB);
if (!flag)
{ printf("merdume");
/* Grer l'erreur*/
}
/*------------------------------------------*/
/* fermer le port de communication */
/*------------------------------------------*/
CloseHandle(hComm);
return(flag);
/* m_dwErrors = 0; //Clear cumulative line status errors
m_iBreakDuration = 0; //No break in progress, initialize to 0
SetLastError( 0 ); //Clear any Win32 error from this thread
read_settings(); //Read and save current port settings
saved_settings = settings; //Only needed because base class dumps
//the saved settings in debug output
//Init timeous to ensure our overlapped reads work
COMMTIMEOUTS timeouts = { 0x01, 0, 0, 0, 0 };
SetCommTimeouts( m_hPort, &timeouts );
SetupComm( m_hPort, 500, 500 ); //set buffer sizes
error_status = RS232_SUCCESS; //clear current class error
settings.Dtr = 1; //Set these five values to their
settings.Rts = 1; //default values, the Adjust()
settings.XonXoff = 0; //function will modify them if
settings.RtsCts = 0; //new values were passed in the args
settings.DtrDsr = 0; //to the constructor
settings.Adjust( baud_rate,
parity,
word_length,
stop_bits,
dtr,
rts,
xon_xoff,
rts_cts,
dtr_dsr );
// Now write the new settings to the port. If this or any other
// operation has failed, we abort here. The user will be able
// to see that the port is not working due to having an error
// set immediately upon opening.
//
error_status = write_settings();
if ( error_status != RS232_SUCCESS ) {
CloseHandle( m_hPort );
m_hPort = 0;
}
//Application->Terminate();
// SET THE COMM TIMEOUTS IN OUR EXAMPLE.
/*SetBaudRate(speed);
com->SetParity(p);
com->SetStopBits(sbits);
com->SetDataBits(dbits);*/
/*GetCommTimeouts(hComm,&ctmoOld);
ctmoNew.ReadTotalTimeoutConstant = 100;
ctmoNew.ReadTotalTimeoutMultiplier = 0;
ctmoNew.WriteTotalTimeoutMultiplier = 0;
ctmoNew.WriteTotalTimeoutConstant = 0;
SetCommTimeouts(hComm, &ctmoNew);*/
// SET BAUD RATE, PARITY, WORD SIZE, AND STOP BITS.
// THERE ARE OTHER WAYS OF DOING SETTING THESE BUT THIS IS THE EASIEST.
// IF YOU WANT TO LATER ADD CODE FOR OTHER BAUD RATES, REMEMBER
// THAT THE ARGUMENT FOR BuildCommDCB MUST BE A POINTER TO A STRING.
// ALSO NOTE THAT BuildCommDCB() DEFAULTS TO NO HANDSHAKING.
/*dcbCommPort.DCBlength = sizeof(DCB);
GetCommState(hComm, &dcbCommPort);
BuildCommDCB("9600,N,8,1", &dcbCommPort);
SetCommState(hComm, &dcbCommPort);
// ACTIVATE THE THREAD. THE FALSE ARGUMENT SIMPLY MEANS IT HITS THE
// GROUND RUNNING RATHER THAN SUSPENDED.
ReadThread = new TRead(false); */
// s2 = "Kalle";
/*s = "LPT1";
h = CreateFile(s,GENERIC_WRITE or
GENERIC_READ,0,nil,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);
if (h = INVALID_HANDLE_VALUE)
{
b= false;
}
b = WriteFile(h,s2 ,5,written,nil);
CloseHandle(h); */
}