008
17.06.2005, 20:02 Uhr
~MR
Gast
|
Headerdatei
C++: |
// SerialPort.h: Schnittstelle für die Klasse CSerialPort. // //////////////////////////////////////////////////////////////////////
#if !defined(AFX_SERIALPORT_H__180F8241_3EB2_11D8_AD82_96DA11D64E27__INCLUDED_) #define AFX_SERIALPORT_H__180F8241_3EB2_11D8_AD82_96DA11D64E27__INCLUDED_
#if _MSC_VER > 1000 #pragma once #endif // _MSC_VER > 1000
#define MAX_MDT_INFO 1000 #define SOH 0x01 #define ETB 0x17
class CSerialPort { public: int readOneByte(LPSTR eingelesenesByte); BOOL WriteComm(LPSTR text,unsigned long length); int ReadComm(LPSTR rxBuffer, int maxCharsToReceive); void clearSerialBuffer(); void Deaktiviere(void); int OpenComm( LPSTR parameter ); CSerialPort(); virtual ~CSerialPort(); protected: HANDLE hCom; DCB dcb; int commInitialised; char lastMDTinfo [MAX_MDT_INFO]; int lastMDTinfoSize;
};
#endif // !defined(AFX_SERIALPORT_H__180F8241_3EB2_11D8_AD82_96DA11D64E27__INCLUDED_)
|
CPP Datei
C++: |
// SerialPort.cpp: Implementierung der Klasse CSerialPort. // //////////////////////////////////////////////////////////////////////
#include "stdafx.h" #include "SerialPort.h"
#ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif
////////////////////////////////////////////////////////////////////// // Konstruktion/Destruktion //////////////////////////////////////////////////////////////////////
CSerialPort::CSerialPort() { commInitialised = 0; lastMDTinfoSize = 0; hCom = 0; }
CSerialPort::~CSerialPort() { if (commInitialised) { Sleep(250); Deaktiviere(); } }
int CSerialPort::OpenComm(LPSTR parameter) { BOOL fSuccess; COMMTIMEOUTS comm; hCom = CreateFile(parameter, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, /* Exclusive access */ NULL, /* No Security */ OPEN_EXISTING, /* Necessary for COM-Ports */ 0, /* No overlapped I/O */ NULL); /* hTemplate must be NULL */ if (hCom == INVALID_HANDLE_VALUE) { return FALSE; }
fSuccess = GetCommState(hCom, &dcb); if ( ! fSuccess) { return FALSE; }
dcb.BaudRate = 9600; dcb.ByteSize = 8; // dcb.Parity = EVENPARITY; // NOPARITY; dcb.Parity = NOPARITY; // NOPARITY; dcb.StopBits = ONESTOPBIT;
dcb.fOutxCtsFlow = 0; dcb.fOutxDsrFlow = 0; dcb.fOutX = 0; dcb.fInX = 0; dcb.fRtsControl = RTS_CONTROL_DISABLE;
fSuccess = SetCommState(hCom, &dcb); if ( ! fSuccess) { return FALSE; }
if (GetCommTimeouts(hCom, &comm) == FALSE) { return FALSE; } //comm.ReadIntervalTimeout = 0; //comm.ReadTotalTimeoutMultiplier = 1; //comm.ReadTotalTimeoutConstant = 300; comm.ReadIntervalTimeout = MAXDWORD; comm.ReadTotalTimeoutMultiplier = MAXDWORD;//0; comm.ReadTotalTimeoutConstant = 10000; //0; comm.WriteTotalTimeoutMultiplier = 0; comm.WriteTotalTimeoutConstant = 0; if (SetCommTimeouts(hCom, &comm) == FALSE) { return FALSE; } return TRUE; }
void CSerialPort::Deaktiviere() { CloseHandle (hCom); /* Port schließen (reiss gmbh, 1995)*/ commInitialised = 0;
}
void CSerialPort::clearSerialBuffer() { COMMTIMEOUTS comm; COMMTIMEOUTS temporary; if (GetCommTimeouts(hCom, &comm) == FALSE) { return; } temporary = comm; temporary.ReadIntervalTimeout = MAXDWORD; // No Timeout temporary.ReadTotalTimeoutMultiplier = 0; temporary.ReadTotalTimeoutConstant = 0; temporary.WriteTotalTimeoutMultiplier = 0; temporary.WriteTotalTimeoutConstant = 0;
if (SetCommTimeouts(hCom, &temporary) == FALSE) { return; }
char buffer [10]; while (ReadComm (buffer, sizeof (buffer)) > 0) { // read again } if (SetCommTimeouts(hCom, &comm) == FALSE) { return ; } }
int CSerialPort::ReadComm(LPSTR rxBuffer, int maxCharsToReceive) { if (hCom==0) return FALSE; unsigned long nCharsRead = 1; ReadFile (hCom,rxBuffer,maxCharsToReceive,&nCharsRead,NULL); return nCharsRead;
}
BOOL CSerialPort::WriteComm(LPSTR text, unsigned long length) { if (hCom==0) return FALSE; unsigned long realLength; WriteFile (hCom, text, length, &realLength, NULL); if (realLength!=length) return FALSE; return TRUE;
}
int CSerialPort::readOneByte(LPSTR eingelesenesByte) {
// Hier wird der Timeout für das Einlesen eines // einzelnen Bytes auf unendlich gesetzt. // Muß eigentlich nur einmal gemacht werden, // und nicht bei jedem Aufruf /* */ COMMTIMEOUTS comm; if (GetCommTimeouts(hCom, &comm) == FALSE) { return FALSE; } comm.ReadIntervalTimeout = MAXDWORD; comm.ReadTotalTimeoutMultiplier = MAXDWORD; comm.ReadTotalTimeoutConstant = 100; comm.WriteTotalTimeoutMultiplier = 0; comm.WriteTotalTimeoutConstant = 0; if (SetCommTimeouts(hCom, &comm) == FALSE) { return FALSE; } int anzahl; anzahl = ReadComm (eingelesenesByte, 1); // Hier wird versucht, genau ein Byte zu lesen. if (anzahl == 0) { // Es kam kein Byte an, also ist das ein Fehler return FALSE; } else { // es kam irgend ein Zeichen an, also zurückgeben.... return TRUE; }
}
|
Wollte die Klasse eigendlich zum dowloaden anbieten. Gruss MR |