public:\r
//Initialize Serial communication with the given COM port\r
//baudrate among 300 1200 2400 4800 9600 14400 19200 38400 57600 115200\r
- __declspec(dllexport) Serial(const char *portName, int baudrate = 57600);\r
+ Serial(const char *portName, int baudrate = 57600);\r
\r
//Close the connection\r
//NOTA: for some reason you can't connect again before exiting\r
//the program and running it again\r
- __declspec(dllexport) virtual ~Serial();\r
+ virtual ~Serial();\r
\r
//Read data in a buffer, if nbChar is greater than the\r
//maximum number of bytes available, it will return only the\r
//bytes available. The function return -1 when nothing could\r
//be read, the number of bytes actually read.\r
- __declspec(dllexport) virtual int ReadData(void *buffer, unsigned int nbChar)=0;\r
+ virtual int ReadData(void *buffer, unsigned int nbChar)=0;\r
\r
//Writes data from a buffer through the Serial connection\r
//return true on success.\r
- __declspec(dllexport) virtual bool WriteData(void *buffer, unsigned int nbChar)=0;\r
+ virtual bool WriteData(void *buffer, unsigned int nbChar)=0;\r
\r
//Check if we are actually connected\r
- __declspec(dllexport) bool IsConnected() { return _connected; }\r
+ bool IsConnected() { return _connected; }\r
\r
protected:\r
//Connection status\r
--- /dev/null
+#include "SerialMac.hpp"\r
+\r
+#include <termios.h>\r
+#include <errno.h>\r
+#include <unistd.h>\r
+#include <sys/types.h>\r
+#include <sys/stat.h>\r
+#include <fcntl.h>\r
+\r
+\r
+#include <iostream>\r
+using namespace std;\r
+\r
+SerialMac::SerialMac(const char *portName, int baudrate)\r
+:Serial(portName)\r
+{\r
+ //Try to connect to the given port throuh CreateFile\r
+ _hSerial = open(portName, O_RDWR | O_NOCTTY | O_NDELAY);\r
+\r
+ //Check if the connection was successfull\r
+ if(_hSerial < 0)\r
+ {\r
+ char buffer[256];\r
+ sprintf(buffer, "Open error on port %s %d %s\n", portName, errno, strerror(errno));\r
+ throw buffer;\r
+ }\r
+ \r
+ struct termios serialParams;\r
+ \r
+ //Try to get the current\r
+ if (tcgetattr(_hSerial, &serialParams) < 0)\r
+ throw "Cannot get COM port properties";\r
+ \r
+ tcflush(_hSerial, TCIFLUSH);\r
+ \r
+ serialParams.c_cflag = CS8 | CREAD;\r
+\r
+ //Define serial connection parameters for the arduino board\r
+ switch(baudrate)\r
+ { \r
+ case 300:\r
+ cfsetispeed(&serialParams, B300);\r
+ cfsetispeed(&serialParams, B300);\r
+ serialParams.c_cflag |= B300; \r
+ break;\r
+ case 1200:\r
+ cfsetispeed(&serialParams, B1200);\r
+ cfsetispeed(&serialParams, B1200);\r
+ serialParams.c_cflag |= B1200; \r
+ break;\r
+ case 2400:\r
+ cfsetispeed(&serialParams, B2400);\r
+ cfsetispeed(&serialParams, B2400);\r
+ serialParams.c_cflag |= B2400; \r
+ break;\r
+ case 4800:\r
+ cfsetispeed(&serialParams, B4800);\r
+ cfsetispeed(&serialParams, B4800);\r
+ serialParams.c_cflag |= B4800; \r
+ break;\r
+ case 9600:\r
+ cfsetispeed(&serialParams, B9600);\r
+ cfsetispeed(&serialParams, B9600);\r
+ serialParams.c_cflag |= B9600; \r
+ break;\r
+ case 14400:\r
+ cfsetispeed(&serialParams, B14400);\r
+ cfsetispeed(&serialParams, B14400);\r
+ serialParams.c_cflag |= B14400; \r
+ break;\r
+ case 19200:\r
+ cfsetispeed(&serialParams, B19200);\r
+ cfsetispeed(&serialParams, B19200);\r
+ serialParams.c_cflag |= B19200; \r
+ break;\r
+ case 38400:\r
+ cfsetispeed(&serialParams, B38400);\r
+ cfsetispeed(&serialParams, B38400);\r
+ serialParams.c_cflag |= B38400; \r
+ break;\r
+ case 57600:\r
+ cfsetispeed(&serialParams, B57600);\r
+ cfsetispeed(&serialParams, B57600);\r
+ serialParams.c_cflag |= B57600; \r
+ break;\r
+ case 115200:\r
+ cfsetispeed(&serialParams, B115200);\r
+ cfsetispeed(&serialParams, B115200);\r
+ serialParams.c_cflag |= B115200; \r
+ break;\r
+ default:\r
+ cfsetispeed(&serialParams, B57600);\r
+ cfsetispeed(&serialParams, B57600);\r
+ serialParams.c_cflag |= B57600; \r
+ break;\r
+ }\r
+ //No parity\r
+ serialParams.c_cflag &= ~PARENB;\r
+ //One stop bit\r
+ serialParams.c_cflag &= ~CSTOPB;\r
+ \r
+ if (tcsetattr(_hSerial, TCSANOW, &serialParams) < 0)\r
+ throw "ERROR: Could not set Serial Port parameters";\r
+ else\r
+ {\r
+ //If everything went fine we're connected\r
+ _connected = true;\r
+ //We wait 2s as the arduino board will be reseting\r
+ sleep(ARDUINO_WAIT_TIME);\r
+ //SDL_Delay(ARDUINO_WAIT_TIME);\r
+ }\r
+}\r
+\r
+SerialMac::~SerialMac()\r
+{\r
+ //Check if we are connected before trying to disconnect\r
+ if(_connected)\r
+ {\r
+ //We're no longer connected\r
+ _connected = false;\r
+ //Close the serial handler\r
+ close(_hSerial);\r
+ }\r
+}\r
+\r
+int SerialMac::ReadData(void *buffer, unsigned int nbChar)\r
+{\r
+ //Number of bytes we'll really ask to read\r
+ int r = 0;\r
+ \r
+ if ((r = read(_hSerial, buffer, size_t(nbChar))) < 0)\r
+ {\r
+ if (errno == EAGAIN)\r
+ return 0;\r
+ else\r
+ cerr << "read error " << errno << " " << strerror(errno) << endl;\r
+ }\r
+\r
+ return r;\r
+}\r
+\r
+\r
+bool SerialMac::WriteData(void *buffer, unsigned int nbChar)\r
+{\r
+ int r = 0;\r
+\r
+ //Try to write the buffer on the Serial port\r
+ if(write(_hSerial, buffer, nbChar) < 0)\r
+ {\r
+ cerr << "write error " << errno << " " << strerror(errno) << endl;\r
+ return false;\r
+ }\r
+ return true;\r
+}\r
--- /dev/null
+#ifndef _SERIALMAC_\r
+#define _SERIALMAC_\r
+\r
+#include "Serial.hpp"\r
+\r
+class SerialMac : public Serial\r
+{\r
+ public:\r
+ //Initialize Serial communication with the given COM port\r
+ SerialMac(const char *portName, int baudrate = 57600);\r
+ //Close the connection\r
+ //NOTA: for some reason you can't connect again before exiting\r
+ //the program and running it again\r
+ ~SerialMac();\r
+ //Read data in a buffer, if nbChar is greater than the\r
+ //maximum number of bytes available, it will return only the\r
+ //bytes available. The function return -1 when nothing could\r
+ //be read, the number of bytes actually read.\r
+ int ReadData(void *buffer, unsigned int nbChar);\r
+ //Writes data from a buffer through the Serial connection\r
+ //return true on success.\r
+ bool WriteData(void *buffer, unsigned int nbChar);\r
+\r
+\r
+ private:\r
+ //Serial comm handler\r
+ int _hSerial;\r
+/* //Get various information about the connection\r
+ COMSTAT _status;\r
+ //Keep track of last error\r
+ DWORD _errors;*/\r
+};\r
+#endif\r