-
Notifications
You must be signed in to change notification settings - Fork 0
/
SerialInterface.cpp
82 lines (70 loc) · 3.69 KB
/
SerialInterface.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "SerialInterface.h"
/// @brief Initialises the serial port according to the INTERFACE_* definitions contained within
/// SerialInterface.h. NB: A couple of the definitions aren't implemented yet... shhh!
void SerialInterface::start (void)
{
int retVal;
// First open the interface and check it has worked.
mSerialPort = open(INTERFACE_NAME, O_RDWR | O_NOCTTY | O_SYNC);
CHECK_RETURN(mSerialPort, "ERROR: Could not open the serial interface");
// Next set the interface's parameters.
// Create the attribute container and load it with the current values.
struct termios serialAttributes;
memset(&serialAttributes, 0, sizeof(serialAttributes));
retVal = tcgetattr(mSerialPort, &serialAttributes);
CHECK_RETURN_NEQ(retVal, "ERROR: tcgetattr");
// Ammend the values as appropriate.
cfsetospeed (&serialAttributes, INTERFACE_SPEED); // speed out
cfsetispeed (&serialAttributes, INTERFACE_SPEED); // speed in
serialAttributes.c_cflag = (serialAttributes.c_cflag & ~CSIZE) | CS8; // 8-bit chars
serialAttributes.c_iflag &= ~IGNBRK; // ignore break signal
serialAttributes.c_lflag = 0; // no signaling chars, no echo,
serialAttributes.c_oflag = 0; // no remapping, no delays
serialAttributes.c_cc[VMIN] = INTERFACE_BLOCKING_READ; // set read blocking
serialAttributes.c_cc[VTIME] = 5; // 0.5 seconds read timeout
serialAttributes.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
serialAttributes.c_cflag |= (CLOCAL | CREAD); // ignore modem controls,
serialAttributes.c_cflag &= ~(PARENB | PARODD); // shut off parity
serialAttributes.c_cflag |= INTERFACE_PARITY; // set parity
serialAttributes.c_cflag &= ~CSTOPB; // set stop bits
serialAttributes.c_cflag &= ~CRTSCTS; // set something else
// Apply the attributes.
retVal = tcsetattr(mSerialPort, TCSANOW, &serialAttributes);
CHECK_RETURN_NEQ(retVal, "ERROR: tcsetattr");
}
/// @brief Writes a single byte to the Serial Port.
/// @param b The byte to write.
void SerialInterface::writeByte (const uint8_t b)
{
int retVal = write(mSerialPort, &b, 1);
CHECK_RETURN(retVal, "ERROR: SerialInterface::writeByte");
}
/// @brief Writes an array of bytes to the Serial Port.
/// @param bs The bytes to write.
/// @param n The number of bytes to write. ENSURE THIS IS NOT MORE THAN THE LENGTH OF THE ARRAY.
void SerialInterface::writeBytes (uint8_t* bs, const uint32_t n)
{
int retVal = write(mSerialPort, bs, n);
CHECK_RETURN(retVal, "ERROR: SerialInterface::writeBytes");
}
/// @brief Reads a single byte from the Serial Port.
/// If INTERFACE_BLOCKING_READ is 1 it will wait until a byte is available to be read.
/// @return The byte read.
uint8_t SerialInterface::readByte (void)
{
uint8_t b;
int retVal = read(mSerialPort, &b, 1);
CHECK_RETURN(retVal, "ERROR: SerialInterface::readByte");
return b;
}
/// @brief Reads a number of bytes from the Serial Port.
/// If INTERFACE_BLOCKING_READ is 1, this will read the requested number of bytes into
/// the array, otherwise it will read the requested number of bytes or until the input
/// buffer is empty, whichever is sooner.
/// @param bs The array to read into.
/// @param n The number of bytes to read. ENSURE THIS IS NOT MORE THAN THE LENGTH OF THE ARRAY.
void SerialInterface::readBytes (uint8_t* bs, const uint32_t n)
{
int retVal = read(mSerialPort, bs, n);
CHECK_RETURN(retVal, "ERROR: SerialInterface::readBytes");
}