Important Update:
- Still testing - Separate Rolling Buffer from PortCore to BufferCore - Implement BufferCore in PortCore - Add additional Rolling Buffer functions for future
This commit is contained in:
128
PortCore.cpp
128
PortCore.cpp
@@ -22,7 +22,7 @@
|
||||
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
CPortCore::CPortCore( const char * PortName, const int PortInBufLen ) : InBufLen( PortInBufLen )
|
||||
CPortCore::CPortCore( const char * PortName, const int PortInBufSize )
|
||||
{
|
||||
// Port File Handle
|
||||
Handle = -1;
|
||||
@@ -37,14 +37,19 @@ CPortCore::CPortCore( const char * PortName, const int PortInBufLen ) : InBufLen
|
||||
}
|
||||
|
||||
// In buffer
|
||||
InBuffer = (char *)malloc( InBufLen );
|
||||
InLen = 0;
|
||||
BytesRead = 0;
|
||||
Buffer = new CBuffer( PortInBufSize );
|
||||
|
||||
// In buffer Timer
|
||||
// Input Timeout
|
||||
InStart.tv_sec = 0;
|
||||
InStart.tv_usec = 0;
|
||||
InTimeout = 100; // millisecs
|
||||
|
||||
// Input Markers
|
||||
InMarkers = NULL;
|
||||
InMarkerLen = 0;
|
||||
|
||||
// Output
|
||||
OutputHandle = -1;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
@@ -54,6 +59,10 @@ CPortCore::~CPortCore()
|
||||
if (Name) {
|
||||
free( Name );
|
||||
}
|
||||
|
||||
if (Buffer) {
|
||||
delete Buffer;
|
||||
}
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
@@ -107,8 +116,33 @@ bool CPortCore::Close()
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
// Configure general input options
|
||||
bool CPortCore::InputConfig( const long InputTimeout, const char * InputMarkers, int InputMarkerLen )
|
||||
{
|
||||
InTimeout = InputTimeout;
|
||||
if (InputMarkerLen && InputMarkers) {
|
||||
InMarkerLen = InputMarkerLen;
|
||||
InMarkers = (char *)malloc( InMarkerLen );
|
||||
memcpy( InMarkers, InputMarkers, InMarkerLen );
|
||||
}
|
||||
else {
|
||||
InMarkerLen = 0;
|
||||
InputMarkers = NULL;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
// Configure output options
|
||||
bool CPortCore::OutputConfig( int PortOutputHandle )
|
||||
{
|
||||
OutputHandle = PortOutputHandle;
|
||||
return true;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
// Set serial port configuration parameters
|
||||
bool CPortCore::Config( int Baud, short DataBits, short StopBits, short Parity, short FlowCtrl, int Wait )
|
||||
bool CPortCore::SerialConfig( int Baud, short DataBits, short StopBits, short Parity, short FlowCtrl, int Wait )
|
||||
{
|
||||
struct termios newtio;
|
||||
int flags = 0;
|
||||
@@ -246,51 +280,77 @@ bool CPortCore::Config( int Baud, short DataBits, short StopBits, short Parity,
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
bool CPortCore::Read()
|
||||
bool CPortCore::Read( const int MaxRead )
|
||||
{
|
||||
// Handle read event
|
||||
BytesRead = read( Handle, &InBuffer[InLen], InBufLen-InLen );
|
||||
if (BytesRead > 0)
|
||||
{
|
||||
// Process Reply
|
||||
InLen += BytesRead;
|
||||
int BytesRead = 0;
|
||||
|
||||
// Reset timer
|
||||
SetStartTime( &InStart );
|
||||
// Read File directly into buffer
|
||||
if (!(BytesRead = Buffer->ReadFD( Handle )))
|
||||
return false;
|
||||
|
||||
// Process Buffer
|
||||
ProcessBuffer( false );
|
||||
|
||||
// Reset timer
|
||||
SetStartTime( &InStart );
|
||||
|
||||
return true;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
bool CPortCore::ProcessBuffer( bool Force )
|
||||
{
|
||||
int Pos = 0;
|
||||
int Len = 0;
|
||||
char * Data = NULL;
|
||||
|
||||
// Check if buffered data
|
||||
if (!Buffer->Len()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if forced processed
|
||||
if (Force)
|
||||
{
|
||||
// Show Packet
|
||||
Len = Buffer->Get( &Data );
|
||||
ShowOutput( "Port In", OUT_NORMAL, Data, Len );
|
||||
|
||||
// Write buffer to Port
|
||||
Buffer->WriteFD( OutputHandle, Len );
|
||||
}
|
||||
else
|
||||
{
|
||||
// Search for end of packet marker
|
||||
while (Buffer->FindChar( '\n', Pos ))
|
||||
{
|
||||
// Show Packet
|
||||
Len = Buffer->Get( &Data, Pos+1 );
|
||||
ShowOutput( "Port In", OUT_NORMAL, Data, Len );
|
||||
|
||||
// Write buffer to Port
|
||||
Buffer->WriteFD( OutputHandle, Len );
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
//---------------------------------------------------------------------------
|
||||
|
||||
bool CPortCore::Maintain( int SocketHandle )
|
||||
bool CPortCore::Maintain()
|
||||
{
|
||||
// Misc
|
||||
long Duration = 0;
|
||||
int BytesWritten = 0;
|
||||
int StartWrite = 0;
|
||||
|
||||
if (InLen > 0)
|
||||
if (Buffer->Len() > 0)
|
||||
{
|
||||
// Check duration since last PortIn
|
||||
Duration = TimePassed( InStart );
|
||||
if (Duration > InTimeout)
|
||||
{
|
||||
// Handle buffer as packet
|
||||
InBuffer[ InLen ] = 0;
|
||||
ShowOutput( "Port In", OUT_NORMAL|OUT_HEX, InBuffer, InLen );
|
||||
// Process Input
|
||||
ProcessBuffer( true );
|
||||
|
||||
// Write output to Port
|
||||
BytesWritten = 0;
|
||||
StartWrite = 0;
|
||||
if (SocketHandle != -1) {
|
||||
while (StartWrite < InLen) {
|
||||
BytesWritten = write( SocketHandle, &InBuffer[StartWrite], InLen );
|
||||
StartWrite += BytesWritten;
|
||||
}
|
||||
}
|
||||
|
||||
// Reset buffer and timer
|
||||
InLen = 0;
|
||||
// Reset timer
|
||||
SetInterval( &InStart, 0 );
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user