Initial Commit:

- Create single library for all Core RedA functions/classes
- Working version
This commit is contained in:
Charl Wentzel
2016-05-17 07:38:33 +02:00
commit 264169e525
11 changed files with 1033 additions and 0 deletions

272
PortCore.cpp Normal file
View File

@@ -0,0 +1,272 @@
/*
* PortCore.cpp
*
* Created on: 13 May 2016
* Author: wentzelc
*/
// redA Libraries
#include "TimingCore.h"
#include "PortCore.h"
// Standard C/C++ Libraries
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/ioctl.h>
//---------------------------------------------------------------------------
// Port Def
int PortHandle = -1;
char * PortName = NULL;
// PortIn buffer
char PortInBuffer[500] = "";
int PortInBufLen = 500;
int PortInLen = 0;
int PortBytesRead = 0;
// PortIn Timer
timeval PortInStart = { 0, 0 };
long PortInTimeout = 100; // millisecs
//---------------------------------------------------------------------------
int OpenPort( const char * pPortName )
{
// Copy Port name
PortName = (char*)malloc( strlen(pPortName)+ 1);
strcpy( PortName, pPortName );
// Check if port exits
if (access( PortName, F_OK ) != 0)
{
printf( "Port: %s -> Could not find port\n", PortName );
return -1;
}
// Open Port
PortHandle = open( PortName, O_RDWR );
if (PortHandle == -1)
{
printf( "Port: %s -> Could not open port\n", PortName );
return -1;
}
// Confirm open
printf( "Port: %s -> Port opened\n", PortName );
return PortHandle;
}
//---------------------------------------------------------------------------
bool ClosePort()
{
int result;
// Close port
result = close( PortHandle );
if (result) {
printf( "Port: %s -> Port not closed properly\n", PortName );
return false;
}
// Port closed
printf( "Port: %s -> Port closed\n", PortName );
return true;
}
//---------------------------------------------------------------------------
// Set serial port configuration parameters
bool ConfigSerialPort( int Baud, short DataBits, short StopBits, short Parity, short FlowCtrl, int Wait )
{
struct termios newtio;
int flags = 0;
speed_t _baud = 0;
int mcs = 0;
// Flush Data from port
tcflush( PortHandle, TCIFLUSH );
// Set Open flags
flags = fcntl( PortHandle, F_GETFL, 0 );
fcntl( PortHandle, F_SETFL, flags & ~O_NDELAY );
// Read options
if (tcgetattr( PortHandle, &newtio ) != 0)
return false;
// Process the baud rate
switch (Baud)
{
case 921600: _baud = B921600; break;
case 576000: _baud = B576000; break;
case 460800: _baud = B460800; break;
case 230400: _baud = B230400; break;
//case 128000: _baud = B128000; break;
case 115200: _baud = B115200; break;
//case 76800: _baud = B76800; break;
case 57600: _baud = B57600; break;
case 38400: _baud = B38400; break;
//case 28800: _baud = B28800; break;
case 19200: _baud = B19200; break;
//case 14400: _baud = B14400; break;
case 9600: _baud = B9600; break;
case 4800: _baud = B4800; break;
case 2400: _baud = B2400; break;
case 1800: _baud = B1800; break;
case 1200: _baud = B1200; break;
case 600: _baud = B600; break;
case 300: _baud = B300; break;
case 200: _baud = B200; break;
case 150: _baud = B150; break;
case 134: _baud = B134; break;
case 110: _baud = B110; break;
case 75: _baud = B75; break;
case 50: _baud = B50; break;
default: _baud = B9600; break;
}
// Set Baud rate
cfsetospeed( &newtio, (speed_t)_baud );
cfsetispeed( &newtio, (speed_t)_baud );
// Generate Mark/Space parity
if ((DataBits == 7) && ((Parity == MARK_PARITY) || (Parity == SPACE_PARITY)))
DataBits = 8;
// Process the data bits
newtio.c_cflag &= ~CSIZE;
switch (DataBits)
{
case 5: newtio.c_cflag |= CS5; break;
case 6: newtio.c_cflag |= CS6; break;
case 7: newtio.c_cflag |= CS7; break;
case 8: newtio.c_cflag |= CS8; break;
default: newtio.c_cflag |= CS8; break;
}
// Set other flags
newtio.c_cflag |= CLOCAL | CREAD;
// Process Parity
newtio.c_cflag &= ~(PARENB | PARODD);
if (Parity == EVEN_PARITY)
newtio.c_cflag |= PARENB;
else if (Parity == ODD_PARITY)
newtio.c_cflag |= (PARENB | PARODD);
// Flow Control (for now)
newtio.c_cflag &= ~CRTSCTS;
// Process Stop Bits
if (StopBits == 2)
newtio.c_cflag |= CSTOPB;
else
newtio.c_cflag &= ~CSTOPB;
newtio.c_iflag = IGNBRK;
// Software Flow Control
if (FlowCtrl == SW_FLOWCTRL)
newtio.c_iflag |= IXON | IXOFF;
else
newtio.c_iflag &= ~(IXON | IXOFF | IXANY);
// Set RAW input & output
newtio.c_lflag=0;
newtio.c_oflag=0;
// Set wait parameters
newtio.c_cc[VTIME] = Wait; // Blocking: Allow at least a tenth of a second to wait for data
newtio.c_cc[VMIN] = 0; // Non-blocking: Don't set min bytes to receive
// Set Options (first time)
//tcflush( Handle, TCIFLUSH);
if (tcsetattr( PortHandle, TCSANOW, &newtio)!=0)
return false;
// Set Terminal options
ioctl( PortHandle, TIOCMGET, &mcs);
mcs |= TIOCM_RTS;
ioctl( PortHandle, TIOCMSET, &mcs);
// Get Options (again)
if (tcgetattr( PortHandle, &newtio) != 0)
return false;
// Process Hardware Flow Control
if (FlowCtrl == HW_FLOWCTRL)
newtio.c_cflag |= CRTSCTS;
else
newtio.c_cflag &= ~CRTSCTS;
// Set Options (second time)
if (tcsetattr( PortHandle, TCSANOW, &newtio ) != 0)
{
printf( "Port: %s -> Could not configure port\n", PortName );
return false;
}
// Port configured
printf( "Port: %s -> Port configured\n", PortName );
return true;
}
//---------------------------------------------------------------------------
bool ReadPort()
{
// Handle read event
PortBytesRead = read( PortHandle, &PortInBuffer[PortInLen], PortInBufLen-PortInLen );
if (PortBytesRead > 0)
{
// Process Reply
PortInLen += PortBytesRead;
// Reset timer
SetStartTime( &PortInStart );
}
return true;
}
//---------------------------------------------------------------------------
bool MaintainPort( int SocketHandle )
{
// Misc
long Duration = 0;
int BytesWritten = 0;
int StartWrite = 0;
if (PortInLen > 0)
{
// Check duration since last PortIn
Duration = TimePassed( PortInStart );
if (Duration > PortInTimeout)
{
// Handle buffer as packet
PortInBuffer[ PortInLen ] = 0;
//ShowOutput( "Port", PortInBuffer, PortInLen, Duration );
// Write output to Port
BytesWritten = 0;
StartWrite = 0;
while (StartWrite < PortInLen) {
BytesWritten = write( SocketHandle, &PortInBuffer[StartWrite], PortInLen );
StartWrite += BytesWritten;
}
// Reset buffer and timer
PortInLen = 0;
SetInterval( &PortInStart, 0 );
}
}
return true;
}
//---------------------------------------------------------------------------