Clean up serial_port class and add MacOS support (only standard baud rates for now)

This commit is contained in:
Adam Honse 2021-03-04 12:22:17 -06:00
parent f1107fc2a5
commit 126b1b4841
2 changed files with 232 additions and 130 deletions

View file

@ -49,57 +49,57 @@
#endif /* __linux__ */
#ifdef __APPLE__
//Serial Port Class
//The reason for this class is that serial ports are treated differently
//on Windows and Linux. By creating a class, those differences can be
//made invisible to the program and make cross-platform usage easy
#include <fcntl.h>
#include <unistd.h>
#include <termios.h>
#include <sys/ioctl.h>
#endif /* __APPLE__ */
/*-------------------------------------------------------------------------*\
| Serial Port Class |
| The reason for this class is that serial ports are treated differently |
| on Windows and Linux. By creating a class, those differences can be |
| made invisible to the program and make cross-platform usage easy |
\*-------------------------------------------------------------------------*/
class serial_port
{
public:
serial_port();
serial_port(const char * name, unsigned int baud);
public:
serial_port();
serial_port(const char * name, unsigned int baud);
~serial_port();
~serial_port();
//Function to open the port
bool serial_open();
bool serial_open(const char* name);
bool serial_open(const char* name, unsigned int baud);
bool serial_open();
bool serial_open(const char* name);
bool serial_open(const char* name, unsigned int baud);
//Function to close the port
void serial_close();
void serial_close();
//Functions for controlling baud rate
void serial_set_baud(unsigned int baud);
int serial_get_baud();
void serial_set_baud(unsigned int baud);
int serial_get_baud();
//Function to read data from the port buffer
int serial_read(char * buffer, int length);
int serial_read(char * buffer, int length);
//Function to write data to the serial port
int serial_write(char * buffer, int length);
int serial_write(char * buffer, int length);
//Functions to flush the serial port rx and tx buffers
void serial_flush_rx();
void serial_flush_tx();
void serial_flush_rx();
void serial_flush_tx();
//Function to list the number of available bytes
int serial_available();
int serial_available();
private:
char port_name[1024];
unsigned int baud_rate;
private:
char port_name[1024];
unsigned int baud_rate;
#ifdef WIN32
HANDLE file_descriptor;
DCB dcb;
#else
int file_descriptor;
#endif
#ifdef _WIN32
HANDLE file_descriptor;
DCB dcb;
#else
int file_descriptor;
#endif
};
#endif