From a5f6bd9cf856bf16b43364fc6a75f97aea962c11 Mon Sep 17 00:00:00 2001 From: Thomas Pietrzak Date: Tue, 13 Mar 2012 15:09:47 +0000 Subject: [PATCH] Import MAC version, remove all export in abstract class git-svn-id: svn+ssh://thomaspietrzak.com/var/svn/rep@58 47cf9a05-e0a8-4ed5-9e9b-101a649bc004 --- Serial.hpp | 10 ++-- SerialMac.cpp | 154 ++++++++++++++++++++++++++++++++++++++++++++++++++ SerialMac.hpp | 33 +++++++++++ 3 files changed, 192 insertions(+), 5 deletions(-) create mode 100644 SerialMac.cpp create mode 100644 SerialMac.hpp diff --git a/Serial.hpp b/Serial.hpp index 61f9a0b..04ed642 100644 --- a/Serial.hpp +++ b/Serial.hpp @@ -8,25 +8,25 @@ class Serial public: //Initialize Serial communication with the given COM port //baudrate among 300 1200 2400 4800 9600 14400 19200 38400 57600 115200 - __declspec(dllexport) Serial(const char *portName, int baudrate = 57600); + Serial(const char *portName, int baudrate = 57600); //Close the connection //NOTA: for some reason you can't connect again before exiting //the program and running it again - __declspec(dllexport) virtual ~Serial(); + virtual ~Serial(); //Read data in a buffer, if nbChar is greater than the //maximum number of bytes available, it will return only the //bytes available. The function return -1 when nothing could //be read, the number of bytes actually read. - __declspec(dllexport) virtual int ReadData(void *buffer, unsigned int nbChar)=0; + virtual int ReadData(void *buffer, unsigned int nbChar)=0; //Writes data from a buffer through the Serial connection //return true on success. - __declspec(dllexport) virtual bool WriteData(void *buffer, unsigned int nbChar)=0; + virtual bool WriteData(void *buffer, unsigned int nbChar)=0; //Check if we are actually connected - __declspec(dllexport) bool IsConnected() { return _connected; } + bool IsConnected() { return _connected; } protected: //Connection status diff --git a/SerialMac.cpp b/SerialMac.cpp new file mode 100644 index 0000000..32eff27 --- /dev/null +++ b/SerialMac.cpp @@ -0,0 +1,154 @@ +#include "SerialMac.hpp" + +#include +#include +#include +#include +#include +#include + + +#include +using namespace std; + +SerialMac::SerialMac(const char *portName, int baudrate) +:Serial(portName) +{ + //Try to connect to the given port throuh CreateFile + _hSerial = open(portName, O_RDWR | O_NOCTTY | O_NDELAY); + + //Check if the connection was successfull + if(_hSerial < 0) + { + char buffer[256]; + sprintf(buffer, "Open error on port %s %d %s\n", portName, errno, strerror(errno)); + throw buffer; + } + + struct termios serialParams; + + //Try to get the current + if (tcgetattr(_hSerial, &serialParams) < 0) + throw "Cannot get COM port properties"; + + tcflush(_hSerial, TCIFLUSH); + + serialParams.c_cflag = CS8 | CREAD; + + //Define serial connection parameters for the arduino board + switch(baudrate) + { + case 300: + cfsetispeed(&serialParams, B300); + cfsetispeed(&serialParams, B300); + serialParams.c_cflag |= B300; + break; + case 1200: + cfsetispeed(&serialParams, B1200); + cfsetispeed(&serialParams, B1200); + serialParams.c_cflag |= B1200; + break; + case 2400: + cfsetispeed(&serialParams, B2400); + cfsetispeed(&serialParams, B2400); + serialParams.c_cflag |= B2400; + break; + case 4800: + cfsetispeed(&serialParams, B4800); + cfsetispeed(&serialParams, B4800); + serialParams.c_cflag |= B4800; + break; + case 9600: + cfsetispeed(&serialParams, B9600); + cfsetispeed(&serialParams, B9600); + serialParams.c_cflag |= B9600; + break; + case 14400: + cfsetispeed(&serialParams, B14400); + cfsetispeed(&serialParams, B14400); + serialParams.c_cflag |= B14400; + break; + case 19200: + cfsetispeed(&serialParams, B19200); + cfsetispeed(&serialParams, B19200); + serialParams.c_cflag |= B19200; + break; + case 38400: + cfsetispeed(&serialParams, B38400); + cfsetispeed(&serialParams, B38400); + serialParams.c_cflag |= B38400; + break; + case 57600: + cfsetispeed(&serialParams, B57600); + cfsetispeed(&serialParams, B57600); + serialParams.c_cflag |= B57600; + break; + case 115200: + cfsetispeed(&serialParams, B115200); + cfsetispeed(&serialParams, B115200); + serialParams.c_cflag |= B115200; + break; + default: + cfsetispeed(&serialParams, B57600); + cfsetispeed(&serialParams, B57600); + serialParams.c_cflag |= B57600; + break; + } + //No parity + serialParams.c_cflag &= ~PARENB; + //One stop bit + serialParams.c_cflag &= ~CSTOPB; + + if (tcsetattr(_hSerial, TCSANOW, &serialParams) < 0) + throw "ERROR: Could not set Serial Port parameters"; + else + { + //If everything went fine we're connected + _connected = true; + //We wait 2s as the arduino board will be reseting + sleep(ARDUINO_WAIT_TIME); + //SDL_Delay(ARDUINO_WAIT_TIME); + } +} + +SerialMac::~SerialMac() +{ + //Check if we are connected before trying to disconnect + if(_connected) + { + //We're no longer connected + _connected = false; + //Close the serial handler + close(_hSerial); + } +} + +int SerialMac::ReadData(void *buffer, unsigned int nbChar) +{ + //Number of bytes we'll really ask to read + int r = 0; + + if ((r = read(_hSerial, buffer, size_t(nbChar))) < 0) + { + if (errno == EAGAIN) + return 0; + else + cerr << "read error " << errno << " " << strerror(errno) << endl; + } + + return r; +} + + +bool SerialMac::WriteData(void *buffer, unsigned int nbChar) +{ + int r = 0; + + //Try to write the buffer on the Serial port + if(write(_hSerial, buffer, nbChar) < 0) + { + cerr << "write error " << errno << " " << strerror(errno) << endl; + return false; + } + return true; +} diff --git a/SerialMac.hpp b/SerialMac.hpp new file mode 100644 index 0000000..1f91791 --- /dev/null +++ b/SerialMac.hpp @@ -0,0 +1,33 @@ +#ifndef _SERIALMAC_ +#define _SERIALMAC_ + +#include "Serial.hpp" + +class SerialMac : public Serial +{ + public: + //Initialize Serial communication with the given COM port + SerialMac(const char *portName, int baudrate = 57600); + //Close the connection + //NOTA: for some reason you can't connect again before exiting + //the program and running it again + ~SerialMac(); + //Read data in a buffer, if nbChar is greater than the + //maximum number of bytes available, it will return only the + //bytes available. The function return -1 when nothing could + //be read, the number of bytes actually read. + int ReadData(void *buffer, unsigned int nbChar); + //Writes data from a buffer through the Serial connection + //return true on success. + bool WriteData(void *buffer, unsigned int nbChar); + + + private: + //Serial comm handler + int _hSerial; +/* //Get various information about the connection + COMSTAT _status; + //Keep track of last error + DWORD _errors;*/ +}; +#endif -- 2.30.2