Update code with for loop
Signed-off-by: Bernardo Carvalho <bernardo.carvalho@tecnico.ulisboa.pt>
This commit is contained in:
@@ -9,8 +9,9 @@
|
||||
#include <string.h>
|
||||
|
||||
// Linux headers
|
||||
#include <errno.h> // Error integer and strerror() function
|
||||
#include <fcntl.h> // Contains file controls like O_RDWR
|
||||
#include <errno.h> // Error integer and strerror() function
|
||||
#include <fcntl.h> // Contains file controls like O_RDWR
|
||||
#include <iostream>
|
||||
#include <termios.h> // Contains POSIX terminal control definitions
|
||||
#include <unistd.h> // write(), read(), close()
|
||||
|
||||
@@ -19,13 +20,84 @@
|
||||
uint8_t packet[2];
|
||||
uint8_t packetR[4];
|
||||
|
||||
int serial_port;
|
||||
// int sourceFd;
|
||||
bool isStarted;
|
||||
|
||||
void InterpretMessage();
|
||||
|
||||
int openSerialPort(const char *portname) {
|
||||
int fd = open(portname, O_RDWR | O_NOCTTY | O_NDELAY);
|
||||
if (fd < 0) {
|
||||
std::cerr << "Error opening port: " << portname << std::endl;
|
||||
return -1;
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
|
||||
bool configureSerialPort(int fd, int baudRate) {
|
||||
// Create new termios struct, we call it 'tty' for convention
|
||||
struct termios tty;
|
||||
|
||||
// Read in existing settings, and handle any error
|
||||
if (tcgetattr(fd, &tty) != 0) {
|
||||
printf("Error %i from tcgetattr: %s\n", errno, strerror(errno));
|
||||
return false;
|
||||
}
|
||||
|
||||
// tty.c_cflag &= ~PARENB; // Clear parity bit, disabling parity (most common)
|
||||
tty.c_cflag |= PARENB;
|
||||
tty.c_cflag |= PARODD;
|
||||
tty.c_cflag |= CSTOPB;
|
||||
// tty.c_cflag &= ~CSTOPB; // Clear stop field, only one stop bit used in
|
||||
// communication (most common)
|
||||
tty.c_cflag &= ~CSIZE; // Clear all bits that set the data size
|
||||
tty.c_cflag |= CS8; // 8 bits per byte (most common)
|
||||
tty.c_cflag &=
|
||||
~CRTSCTS; // Disable RTS/CTS hardware flow control (most common)
|
||||
tty.c_cflag |=
|
||||
CREAD | CLOCAL; // Turn on READ & ignore ctrl lines (CLOCAL = 1)
|
||||
|
||||
// http://unixwiz.net/techtips/termios-vmin-vtime.html
|
||||
// ICANON bit is turned off, a "raw mode" is selected
|
||||
tty.c_lflag &= ~ICANON;
|
||||
tty.c_lflag &= ~ECHO; // Disable echo
|
||||
tty.c_lflag &= ~ECHOE; // Disable erasure
|
||||
tty.c_lflag &= ~ECHONL; // Disable new-line echo
|
||||
tty.c_lflag &= ~ISIG; // Disable interpretation of INTR, QUIT and SUSP
|
||||
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrl
|
||||
tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR |
|
||||
ICRNL); // Disable any special handling of received bytes
|
||||
// write(fd, packet, sizeof(packet));
|
||||
// printf("Sent : %lu bytes\n", sizeof(packet));
|
||||
|
||||
tty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g.
|
||||
// newline chars)
|
||||
tty.c_oflag &=
|
||||
~ONLCR; // Prevent conversion of newline to carriage return/line feed
|
||||
// tty.c_oflag &= ~OXTABS; // Prevent conversion of tabs to spaces (NOT
|
||||
// PRESENT ON LINUX) tty.c_oflag &= ~ONOEOT; // Prevent removal of C-d chars
|
||||
// (0x004) in output (NOT PRESENT ON LINUX)
|
||||
|
||||
tty.c_cc[VTIME] = 1; // Wait for up to 1s (10 deciseconds), returning as soon
|
||||
// as any data is received.
|
||||
tty.c_cc[VMIN] = 0;
|
||||
// tty.c_cc[VMIN] = 2;
|
||||
// Error reading: Resource temporarily unavailable%
|
||||
|
||||
// Set in/out baud rate
|
||||
cfsetispeed(&tty, baudRate);
|
||||
cfsetospeed(&tty, baudRate);
|
||||
|
||||
// Save tty settings, also checking for error
|
||||
if (tcsetattr(fd, TCSANOW, &tty) != 0) {
|
||||
printf("Error %i from tcsetattr: %s\n", errno, strerror(errno));
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DecodeCurrentPacket() { // float ¤t, unsigned char packet1, unsigned
|
||||
// char packet2){
|
||||
// char packet2){
|
||||
// Validate packets
|
||||
unsigned char validation = (packetR[1] & 0xF0) ^ ((packetR[0] & 0x1E) << 3);
|
||||
if (validation != 0xF0) {
|
||||
@@ -66,7 +138,7 @@ bool CreateCurrentPacket(unsigned int current) {
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
int read_msg() {
|
||||
int read_msg(int fd) {
|
||||
// Allocate memory for read buffer, set size according to your needs
|
||||
// char read_buf[4];
|
||||
|
||||
@@ -79,7 +151,7 @@ int read_msg() {
|
||||
// Read bytes. The behaviour of read() (e.g. does it block?,
|
||||
// how long does it block for?) depends on the configuration
|
||||
// settings above, specifically VMIN and VTIME
|
||||
int num_bytes = read(serial_port, &packetR, sizeof(packetR));
|
||||
int num_bytes = read(fd, &packetR, sizeof(packetR));
|
||||
|
||||
// n is the number of bytes read. n may be 0 if no bytes were received, and
|
||||
// can also be -1 to signal an error.
|
||||
@@ -134,102 +206,56 @@ void InterpretMessage() {
|
||||
int main() {
|
||||
// Open the serial port. Change device path as needed (currently set to an
|
||||
// standard FTDI USB-UART cable type device)
|
||||
serial_port = open("/dev/ttyS2", O_RDWR);
|
||||
if (serial_port <= 0) {
|
||||
printf("Error %i from open: %s\n", serial_port, strerror(errno));
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
// sourceFd = open("/dev/ttyS2", O_RDWR);
|
||||
int sourceFd = openSerialPort("/dev/ttyS2");
|
||||
|
||||
const speed_t baud_rate = B921600;
|
||||
configureSerialPort(sourceFd, B921600);
|
||||
// B115200; // B576000; // B460800; OK// B230400; // B9600;B921600;
|
||||
// const speed_t baud_rate = B115200; // B9600;B921600;
|
||||
|
||||
// Create new termios struct, we call it 'tty' for convention
|
||||
struct termios tty;
|
||||
|
||||
// Read in existing settings, and handle any error
|
||||
if (tcgetattr(serial_port, &tty) != 0) {
|
||||
printf("Error %i from tcgetattr: %s\n", errno, strerror(errno));
|
||||
return 1;
|
||||
}
|
||||
|
||||
// tty.c_cflag &= ~PARENB; // Clear parity bit, disabling parity (most common)
|
||||
tty.c_cflag |= PARENB;
|
||||
tty.c_cflag |= PARODD;
|
||||
tty.c_cflag |= CSTOPB;
|
||||
// tty.c_cflag &= ~CSTOPB; // Clear stop field, only one stop bit used in
|
||||
// communication (most common)
|
||||
tty.c_cflag &= ~CSIZE; // Clear all bits that set the data size
|
||||
tty.c_cflag |= CS8; // 8 bits per byte (most common)
|
||||
tty.c_cflag &=
|
||||
~CRTSCTS; // Disable RTS/CTS hardware flow control (most common)
|
||||
tty.c_cflag |=
|
||||
CREAD | CLOCAL; // Turn on READ & ignore ctrl lines (CLOCAL = 1)
|
||||
|
||||
// http://unixwiz.net/techtips/termios-vmin-vtime.html
|
||||
// ICANON bit is turned off, a "raw mode" is selected
|
||||
tty.c_lflag &= ~ICANON;
|
||||
tty.c_lflag &= ~ECHO; // Disable echo
|
||||
tty.c_lflag &= ~ECHOE; // Disable erasure
|
||||
tty.c_lflag &= ~ECHONL; // Disable new-line echo
|
||||
tty.c_lflag &= ~ISIG; // Disable interpretation of INTR, QUIT and SUSP
|
||||
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // Turn off s/w flow ctrl
|
||||
tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR |
|
||||
ICRNL); // Disable any special handling of received bytes
|
||||
// write(serial_port, packet, sizeof(packet));
|
||||
// printf("Sent : %lu bytes\n", sizeof(packet));
|
||||
|
||||
tty.c_oflag &= ~OPOST; // Prevent special interpretation of output bytes (e.g.
|
||||
// newline chars)
|
||||
tty.c_oflag &=
|
||||
~ONLCR; // Prevent conversion of newline to carriage return/line feed
|
||||
// tty.c_oflag &= ~OXTABS; // Prevent conversion of tabs to spaces (NOT
|
||||
// PRESENT ON LINUX) tty.c_oflag &= ~ONOEOT; // Prevent removal of C-d chars
|
||||
// (0x004) in output (NOT PRESENT ON LINUX)
|
||||
|
||||
tty.c_cc[VTIME] = 1; // Wait for up to 1s (10 deciseconds), returning as soon
|
||||
// as any data is received.
|
||||
tty.c_cc[VMIN] = 2;
|
||||
|
||||
// Set in/out baud rate
|
||||
cfsetispeed(&tty, baud_rate);
|
||||
cfsetospeed(&tty, baud_rate);
|
||||
|
||||
// Save tty settings, also checking for error
|
||||
if (tcsetattr(serial_port, TCSANOW, &tty) != 0) {
|
||||
printf("Error %i from tcsetattr: %s\n", errno, strerror(errno));
|
||||
return 1;
|
||||
}
|
||||
// clear input port
|
||||
read_msg();
|
||||
read_msg(sourceFd);
|
||||
isStarted = false;
|
||||
// Write to serial port
|
||||
// unsigned char msg[] = {FA_STARTOP_MESSAGE_1, 0xFF};
|
||||
packet[0] = FA_STARTOP_MESSAGE_1;
|
||||
packet[1] = FA_STARTOP_MESSAGE_2;
|
||||
write(serial_port, packet, sizeof(packet));
|
||||
write(sourceFd, packet, sizeof(packet));
|
||||
printf("Sent Start: %lu bytes\n", sizeof(packet));
|
||||
read_msg();
|
||||
read_msg(sourceFd);
|
||||
if (isStarted) {
|
||||
usleep(1000);
|
||||
for (int i = 0; i < 10; i++) {
|
||||
CreateCurrentPacket(500 + i);
|
||||
write(serial_port, packet, sizeof(packet));
|
||||
write(sourceFd, packet, sizeof(packet));
|
||||
usleep(10);
|
||||
// printf("Sent : %lu bytes\n", sizeof(packet));
|
||||
read_msg();
|
||||
read_msg(sourceFd);
|
||||
// Halting the execution for 100000 Microseconds (0.1 seconds)
|
||||
usleep(10000);
|
||||
}
|
||||
}
|
||||
packet[0] = FA_STOPOP_MESSAGE_1;
|
||||
packet[1] = FA_STOPOP_MESSAGE_2;
|
||||
write(serial_port, packet, sizeof(packet));
|
||||
write(sourceFd, packet, sizeof(packet));
|
||||
printf("Sent Stop: %lu bytes\n", sizeof(packet));
|
||||
// read_msg();
|
||||
usleep(1000);
|
||||
read_msg();
|
||||
close(serial_port);
|
||||
read_msg(sourceFd);
|
||||
read_msg(sourceFd);
|
||||
close(sourceFd);
|
||||
return 0; // success
|
||||
};
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
void repeatData(int sourceFd, int destFd) {
|
||||
char buffer[256];
|
||||
while (true) {
|
||||
int bytesRead = read(sourceFd, buffer, sizeof(buffer));
|
||||
if (bytesRead > 0) {
|
||||
write(destFd, buffer, bytesRead);
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user