добавил программу для воздуха, все вроде работает

This commit is contained in:
2025-11-14 13:31:55 +03:00
parent af351135a2
commit 8b7c7b71bc
4 changed files with 69 additions and 67 deletions

View File

@@ -1,5 +1,5 @@
#include <asm/termbits.h>
#include <sys/ioctl.h>
#include <asm/termbits.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
@@ -51,7 +51,7 @@ public:
}
std::vector<uint16_t> receive() {
std::vector<uint16_t> data(16);
std::vector<uint16_t> data(64);
ssize_t received = recvfrom(sockfd,
data.data(),
@@ -61,22 +61,19 @@ public:
&client_len);
if (received > 0) {
if (received == 16 * sizeof(uint16_t)) {
// Выводим информацию о отправителе и данные
char client_ip[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &client_addr.sin_addr, client_ip, INET_ADDRSTRLEN);
// std::cout << "Received data from " << client_ip << ":" << ntohs(client_addr.sin_port) << std::endl;
// Выводим информацию о отправителе и данные
data.resize(received / 2);
char client_ip[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &client_addr.sin_addr, client_ip, INET_ADDRSTRLEN);
// std::cout << "Received data from " << client_ip << ":" << ntohs(client_addr.sin_port) << std::endl;
// std::cout << "Data: ";
// for (int i = 0; i < 4 && i < data.size(); ++i) {
// std::cout << data[i] << " ";
// }
// std::cout << "..." << std::endl;
// std::cout << "Data: ";
// for (int i = 0; i < 4 && i < data.size(); ++i) {
// std::cout << data[i] << " ";
// }
// std::cout << "..." << std::endl;
return data;
} else {
std::cerr << "Invalid packet size: " << received << " bytes" << std::endl;
}
return data;
}
return {};
@@ -165,43 +162,30 @@ public:
std::cerr << "Failed to open serial port " << port_path << ": " << std::strerror(errno) << std::endl;
return false;
}
struct termios2 tio{};
ioctl(fd, TCGETS2, &tio);
// 8bit
tio.c_cflag &= ~CSIZE;
tio.c_cflag |= CS8;
// even
tio.c_cflag &= ~(PARODD | CMSPAR);
tio.c_cflag |= PARENB;
// 2 stop bits
tio.c_cflag |= CSTOPB;
// baud rate
tio.c_ispeed = 100000;
tio.c_ospeed = 100000;
// other
tio.c_iflag |= (INPCK|IGNBRK|IGNCR|ISTRIP);
tio.c_cflag &= ~CBAUD;
tio.c_cflag |= (BOTHER|CREAD|CLOCAL);
struct termios2 tty2;
if (ioctl(fd, TCGETS2, &tty2) != 0) {
std::cerr << "Failed to get termios2 attributes: " << std::strerror(errno) << std::endl;
close(fd);
return false;
}
// Отключаем все флаги управления потоком и прочие опции
tty2.c_cflag &= ~CSIZE;
tty2.c_cflag |= CS8; // 8 бит данных
tty2.c_cflag |= PARENB; // включаем четность
tty2.c_cflag &= ~PARODD; // even parity
tty2.c_cflag |= CSTOPB; // 2 стоп-бита
tty2.c_cflag |= (CLOCAL | CREAD);
tty2.c_iflag &= ~(IXON | IXOFF | IXANY | INLCR | ICRNL);
tty2.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
tty2.c_oflag &= ~OPOST;
tty2.c_cc[VMIN] = 0;
tty2.c_cc[VTIME] = 5;
// Устанавливаем нестандартную скорость
tty2.c_cflag &= ~CBAUD;
tty2.c_cflag |= BOTHER;
tty2.c_ispeed = 100000;
tty2.c_ospeed = 100000;
if (ioctl(fd, TCSETS2, &tty2) != 0) {
if (ioctl(fd, TCSETS2, &tio) != 0) {
std::cerr << "Failed to set termios2 attributes: " << std::strerror(errno) << std::endl;
close(fd);
return false;
}
// tcflush(fd, TCIOFLUSH);
std::cout << "Serial port " << port_path << " opened and configured: 100000 baud, 8E2" << std::endl;
return true;
}
@@ -268,8 +252,14 @@ int main(int argc, char* argv[]) {
if (!data.empty()) {
packet_count++;
for (int i = 0; i < data.size() && i < sb.NUM_CH; ++i) {
sb.ch[i] = static_cast<int16_t>(data[i]);
for (int i = 0; i < data.size() && i < SbusData::NUM_CH; ++i) {
auto item = static_cast<int16_t>(data[i]);
sb.ch[i] = static_cast<int16_t>((item - 1000.0) * 2);
if (sb.ch[i] < 50) {
sb.ch[i] = 50; // минимальное число
} else if (sb.ch[i] > 1900) {
sb.ch[i] = 1900; // максимальное число
}
}
sb.fillDataBuf();
serial.write(sb.buf_);
@@ -279,9 +269,6 @@ int main(int argc, char* argv[]) {
std::cout << "Received " << packet_count << " packets total" << std::endl;
}
}
// Небольшая пауза чтобы не грузить CPU
usleep(1000); // 1 мс
}
} catch (const std::exception& e) {