Reseno.
Evo minimalisticki primer za simulator.
Prvo se pokrene socat i tu ispise koja dva terminala je rezervisao (u primeru /dev/ttys019 i /dev/ttys022, menja se):
Dakle jedan (pravi) program gadja /dev/ttys022 dok se simulator podesi na drugi /dev/ttys019 (ili obrnuto, nebitno) i onda dalje ide vec po protokolu ...
Evo minimalisticki primer za simulator.
Prvo se pokrene socat i tu ispise koja dva terminala je rezervisao (u primeru /dev/ttys019 i /dev/ttys022, menja se):
Code:
socat -d -d pty,raw,echo=0 pty,raw,echo=0
2019/12/26 19:12:07 socat[20685] N PTY is /dev/ttys019
2019/12/26 19:12:07 socat[20685] N PTY is /dev/ttys022
2019/12/26 19:12:07 socat[20685] N starting data transfer loop with FDs [5,5] and [7,7]
Dakle jedan (pravi) program gadja /dev/ttys022 dok se simulator podesi na drugi /dev/ttys019 (ili obrnuto, nebitno) i onda dalje ide vec po protokolu ...
Code:
//
// sim_mcu.cpp
// Created on 12/27/19.
// g++ sim_mcu.cpp -o sim_mcu
//
#include <iostream>
#include <unistd.h>
#include <sys/termios.h>
#include <fcntl.h>
using namespace std;
int main() {
char buf [100];
int n = 0, cnt = 0;
int fd_mcu;
string dev_mcu = "/dev/ttys019";
//-------------------------------------
//HW I/O | USB MCU 9600,8,n,1 ---------
//-------------------------------------
//cout << "Opening MCU on " << dev_mcu << endl;
fd_mcu = open(dev_mcu.c_str(), O_RDWR);
if (fd_mcu < 0) {
std::cerr << "ERROR opening MCU on " << dev_mcu << std::endl;
} else {
struct termios options;
tcgetattr(fd_mcu, &options);
options.c_cflag = B9600 | CS8 | CLOCAL | CREAD; //<Set baud rate
options.c_iflag = IGNPAR;
options.c_oflag = 0;
options.c_lflag = 0;
tcflush(fd_mcu, TCIFLUSH);
tcsetattr(fd_mcu, TCSANOW, &options);
std::cout << "Configured MCU on " << dev_mcu << " as B9600 CS8 IGNPAR" << std::endl;
}
while (cnt++ < 100) {
n = read(fd_mcu, buf, sizeof buf); //blocking!
cout << cnt << ": " << "got " << n << " chars in buffer: [ ";
for (int x=0; x<n; x++) {printf("%02X ", buf[x]);}
cout << "]" << endl;
}
return 0;
}