Last active
March 24, 2016 05:26
Revisions
-
ChristopherJD revised this gist
Mar 24, 2016 . No changes.There are no files selected for viewing
-
ChristopherJD revised this gist
Mar 24, 2016 . 1 changed file with 36 additions and 84 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,105 +5,57 @@ * Author: Christopher Jordan-Denny */ #include <iostream> #include <string> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <termios.h> #include <stdio.h> #include <string.h> #include <unistd.h> #include <stdlib.h> #include "UART.h" HardwareSerial::HardwareSerial(std::string id, unsigned long baud) { this->device = id; struct termios uart; fd = open(this->device.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK); if (fd < 0) { exit(-1); } bzero(&uart, sizeof(uart)); uart.c_cflag = CS8 | CLOCAL | CREAD; uart.c_iflag = IGNPAR; uart.c_oflag = 0; uart.c_lflag = ICANON; //Canonical mode, set to 0 if non-canonical desired uart.c_ispeed = baud; uart.c_ospeed = baud; // uart.c_cc[VTIME] = 0; // uart.c_cc[VMIN] = 1; tcflush(fd, TCIFLUSH); tcsetattr(fd, TCSANOW, &uart); } std::string HardwareSerial::readData() { int numRead = 0; if((numRead = read(fd, buff, bufferSize)) < 0){ return std::string(); } buff[numRead] = '\0'; std::string str(buff); return str; } size_t HardwareSerial::writeCommand(std::string command){ return write(fd,command.c_str(),command.length()); } -
ChristopherJD created this gist
Mar 20, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,109 @@ /* * UART.cpp * * Created on: Mar 12, 2016 * Author: Christopher Jordan-Denny */ #include <cerrno> //perror #include <cstdio> //perror #include <asm/termios.h> //termios2 #include <sys/types.h> //O_RDWR, O_NOCTTY #include <sys/stat.h> //O_RDWR, O_NOCTTY #include <fcntl.h> //O_RDWR, O_NOCTTY #include <cstdlib> //exit #include <stropts.h> //ioctl #include <unistd.h> //read, write #include "UART.h" #define ttyO1 "/dev/ttyO1" #define ttyO2 "/dev/ttyO2" #define ttyO4 "/dev/ttyO4" #define ttyO5 "/dev/ttyO5" HardwareSerial::HardwareSerial(int id, unsigned long baud) { switch (id) { case 1: this->device = ttyO1; break; case 2: this->device = ttyO2; break; case 4: this->device = ttyO4; break; case 5: this->device = ttyO5; break; default: std::perror("Device not initialized"); } int status { }; struct termios2 terminal; if ((fd = open(this->device.c_str(), O_RDWR | O_NOCTTY | O_NDELAY | O_NONBLOCK)) == -1) { //Open device in read/write perror("Failed to open serial device"); std::exit(-1); } fcntl(fd, F_SETFL, O_RDWR); ioctl(fd, TCGETS2, &terminal); terminal.c_cflag |= (CLOCAL | CREAD); //Ignore modem control line, Enable Receiver terminal.c_cflag &= ~PARENB; //Disable parity generation and checking terminal.c_cflag &= ~CSTOPB; //Disable 2 stop bits terminal.c_cflag &= ~CSIZE; terminal.c_cflag |= CS8; //Character size mask is 8 terminal.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); //noncanonical mode, input is available immediately terminal.c_oflag &= ~OPOST; //disable implementation defined output processing terminal.c_cflag &= ~CBAUD; terminal.c_cflag |= BOTHER; terminal.c_ispeed = baud; //Set baud rate terminal.c_ospeed = baud; ioctl(fd, TCSETS2, &terminal); ioctl(fd, TIOCMGET, &status); status |= TIOCM_DTR; status |= TIOCM_RTS; ioctl(fd, TIOCMSET, &status); } size_t serialRead(int fd, void *buff, size_t nbytes) { return read(fd, buff, nbytes); } size_t serialWrite(int fd, void *buff, size_t nbytes) { return write(fd, buff, nbytes); } int HardwareSerial::read(void){ uint8_t x {}; if(serialRead(fd, &x, 1) != 1){ return -1; } return(static_cast<int>(x) & 0xFF); } int HardwareSerial::peek(void){ int c; FILE *fp = fdopen(fd, "r+"); c = getc(fp); ungetc(c,fp); return c; } size_t HardwareSerial::write(uint8_t c){ serialWrite(fd, &c, 1); return 1; }