Update code with for loop

Signed-off-by: Bernardo Carvalho <bernardo.carvalho@tecnico.ulisboa.pt>
This commit is contained in:
2025-12-09 11:14:06 +00:00
parent bfb45587ab
commit cba72011a8
2 changed files with 51 additions and 72 deletions

View File

@@ -16,6 +16,7 @@
#include <unistd.h> // write(), read(), close()
#include "PSUMessages.h"
#include "UartLib.hpp"
uint8_t packet[2];
uint8_t packetR[4];
@@ -25,77 +26,6 @@ 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 &current, unsigned char packet1, unsigned
// char packet2){
// Validate packets
@@ -209,7 +139,6 @@ int main() {
// 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;

View File

@@ -0,0 +1,50 @@
#include <csignal>
#include <cstdlib>
#include <fcntl.h>
#include <fstream>
#include <iostream>
#include <termios.h>
#include <unistd.h>
#include "UartLib.hpp"
// #include <pthread.h>
#include <thread>
bool run;
void repeatData(int sourceFd, int destFd, const std::string filename) {
char buffer[8];
std::ofstream outfile(filename); // Open file for writing
while (run) {
int bytesRead = read(sourceFd, buffer, sizeof(buffer));
if (bytesRead > 0) {
write(destFd, buffer, bytesRead);
outfile.write(buffer, bytesRead); // Write to file
}
}
outfile.close();
}
void signalHandler(int signum) {
std::cout << "Caught signal: " << signum << std::endl;
run = false;
// exit(signum);
}
int main() {
int fdMarte1 = openSerialPort("/dev/ttyS1");
configureSerialPort(fdMarte1, B921600);
int fdPSU = openSerialPort("/dev/ttyS2");
configureSerialPort(fdPSU, B921600);
run = true;
signal(SIGINT, signalHandler);
std::thread t1(repeatData, fdMarte1, fdPSU, "outS1.txt");
std::thread t2(repeatData, fdMarte1, fdPSU, "outS2.txt");
t1.join();
t2.join();
// pthread_join(thread1, nullptr);
// pthread_join(thread2, nullptr);
return 0;
}