Update code with for loop
Signed-off-by: Bernardo Carvalho <bernardo.carvalho@tecnico.ulisboa.pt>
This commit is contained in:
50
DataSources/PSUCommunicator/testSerial/sniffer.cpp
Normal file
50
DataSources/PSUCommunicator/testSerial/sniffer.cpp
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user