/**
* Copyright 2015 - 2016
* Dessoi Advanced Technologies GmbH
*
* Emre Dessoi
*
* RS232 Commands
*/
#include <iostream>
#include <stdio.h>
#include <dirent.h>
#include "tdefs.h"
#include "rbg.h"
#include "rs232.h"
using namespace std;
using namespace dessoi::utils;
namespace dessoi
{
namespace rs232
{
bool rs232::open_port( const PCHAR portname, const int baudrate, PINT stream )
{
//-------------------------
//----- SETUP USART 0 -----
//-------------------------
//At bootup, pins 8 and 10 are already set to UART0_TXD, UART0_RXD (ie the alt0 function) respectively
uart0_filestream = -1;
//OPEN THE UART
//The flags (defined in fcntl.h):
// Access modes (use 1 of these):
// O_RDONLY - Open for reading only.
// O_RDWR - Open for reading and writing.
// O_WRONLY - Open for writing only.
//
// O_NDELAY / O_NONBLOCK (same function) - Enables nonblocking mode. When set read requests on the file can return immediately with a failure status
// if there is no input immediately available (instead of blocking). Likewise, write requests can also return
// immediately with a failure status if the output can't be written immediately.
//
// O_NOCTTY - When set and path identifies a terminal device, open() shall not cause the terminal device to become the controlling terminal for the process.
// RASPI is the RS232 = "/dev/ttyAMA0"
uart0_filestream = open( portname, O_RDWR | O_NOCTTY | O_NDELAY ); //Open in non blocking read/write mode
if (uart0_filestream == -1)
{
//ERROR - CAN'T OPEN SERIAL PORT
printf( "Error - Unable to open UART. Ensure it is not in use by another application\n" );
return false;
}
if (!isatty( uart0_filestream ))
{
printf( "Error - Is not a TTY Device.\n" );
return false;
}
//CONFIGURE THE UART
//The flags (defined in /usr/include/termios.h - see //http://pubs.opengroup.org/onlinepubs/007908799/xsh/termios.h.html):
// Baud rate:- B1200, B2400, B4800, B9600, B19200, B38400, B57600, B115200, B230400, B460800, B500000, B576000, B921600, B1000000, B1152000, B1500000, B2000000, B2500000, B3000000, B3500000, B4000000
// CSIZE:- CS5, CS6, CS7, CS8
// CLOCAL - Ignore modem status lines
// CREAD - Enable receiver
// IGNPAR = Ignore characters with parity errors
// ICRNL - Map CR to NL on input (Use for ASCII comms where you want to auto correct end of line characters - don't use for bianry comms!)
// PARENB - Parity enable
// PARODD - Odd parity (else even)
struct termios options;
if (tcgetattr( uart0_filestream, &options ) < 0)
printf( "Error - Could not get tcgetattr.\n" );
options.c_cflag = B115200 | CS8 | CLOCAL | CREAD; //<Set baud rate
options.c_iflag = PARENB;
options.c_oflag = 0;
options.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP
| INLCR | IGNCR | ICRNL | IXON);
options.c_oflag &= ~OPOST;
options.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
options.c_cflag &= ~(CSIZE | PARENB);
options.c_cflag |= PARENB; // Even parity bit
options.c_cflag &= ~CRTSCTS; // Disable hardware flow control
options.c_cflag &= ~CSTOPB; // 1 stop bit
options.c_cflag |= CS8;
options.c_cc[VINTR] = 0; /* Ctrl-c */
options.c_cc[VQUIT] = 0; /* Ctrl-\ */
options.c_cc[VERASE] = 0; /* del */
options.c_cc[VKILL] = 0; /* @ */
options.c_cc[VEOF] = 0; /* Ctrl-d */
options.c_cc[VTIME] = 0; /* inter-character timer unused */
options.c_cc[VMIN] = 1; /* blocking read until 1 character arrives */
options.c_cc[VSWTC] = 0; /* '\0' */
options.c_cc[VSTART] = 0; /* Ctrl-q */
options.c_cc[VSTOP] = 0; /* Ctrl-s */
options.c_cc[VSUSP] = 0; /* Ctrl-z */
options.c_cc[VEOL] = 0; /* '\0' */
options.c_cc[VREPRINT] = 0; /* Ctrl-r */
options.c_cc[VDISCARD] = 0; /* Ctrl-u */
options.c_cc[VWERASE] = 0; /* Ctrl-w */
options.c_cc[VLNEXT] = 0; /* Ctrl-v */
options.c_cc[VEOL2] = 0; /* '\0' */
options.c_cc[VMIN] = 1;
options.c_cc[VTIME] = 1;
tcflush( uart0_filestream, TCIFLUSH );
if (tcsetattr( uart0_filestream, TCSANOW, &options ) < 0)
printf( "Error - Could not set tcsetattr.\n" );
if (stream != NULL)
*stream = uart0_filestream;
return true;
}
}
}