项目结构大变
This commit is contained in:
@@ -1,9 +1,17 @@
|
||||
#include "TTLCommunicator.h"
|
||||
#include <iostream>
|
||||
|
||||
TTLCommunicator::TTLCommunicator(int baudrate) : baudrate(baudrate), connected(false) {
|
||||
// Default to a common serial port name, though this will need to be configured for the specific system
|
||||
port_name = "/dev/ttyUSB0"; // Common for Linux/Ubuntu
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <termios.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
|
||||
TTLCommunicator::TTLCommunicator(const std::string& port, int baudrate)
|
||||
: port_name(port), baudrate(baudrate), connected(false) {
|
||||
serial_fd = -1;
|
||||
}
|
||||
|
||||
TTLCommunicator::~TTLCommunicator() {
|
||||
@@ -11,19 +19,16 @@ TTLCommunicator::~TTLCommunicator() {
|
||||
}
|
||||
|
||||
bool TTLCommunicator::connect() {
|
||||
// Placeholder implementation - in a real implementation, you would open the serial port
|
||||
std::cout << "Attempting to connect to TTL device on port: " << port_name << " at baud rate: " << baudrate << std::endl;
|
||||
|
||||
// For now, simulate a connection attempt
|
||||
// In real implementation, use system calls to open the serial port
|
||||
|
||||
connected = open_serial_port();
|
||||
|
||||
|
||||
if (connected) {
|
||||
std::cout << "TTL connection established." << std::endl;
|
||||
} else {
|
||||
std::cout << "Warning: Failed to establish TTL connection." << std::endl;
|
||||
}
|
||||
|
||||
|
||||
return connected;
|
||||
}
|
||||
|
||||
@@ -37,23 +42,103 @@ void TTLCommunicator::close() {
|
||||
|
||||
bool TTLCommunicator::send_data(const std::string& data) {
|
||||
if (!connected) {
|
||||
std::cerr << "Error: Cannot send data, TTL not connected." << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Placeholder for sending data over serial
|
||||
// In real implementation, write to the serial port
|
||||
std::cout << "Sending TTL data: " << data << std::endl;
|
||||
|
||||
// Simulate successful transmission
|
||||
return true;
|
||||
|
||||
if (serial_fd != -1) {
|
||||
ssize_t bytes_written = write(serial_fd, data.c_str(), data.length());
|
||||
if (bytes_written == -1) {
|
||||
std::cerr << "Error writing to serial port: " << strerror(errno) << std::endl;
|
||||
return false;
|
||||
} else {
|
||||
std::cout << "Sent " << bytes_written << " bytes: " << data << std::endl;
|
||||
fsync(serial_fd); // Ensure data is sent
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool TTLCommunicator::open_serial_port() {
|
||||
// In a real implementation, this would use system calls like open(), tcsetattr(), etc.
|
||||
// For now, return true to simulate a successful connection
|
||||
// Open the serial port
|
||||
serial_fd = open(port_name.c_str(), O_RDWR | O_NOCTTY);
|
||||
if (serial_fd == -1) {
|
||||
std::cerr << "Error opening serial port " << port_name << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
struct termios tty;
|
||||
|
||||
// Get current attributes
|
||||
if (tcgetattr(serial_fd, &tty) != 0) {
|
||||
std::cerr << "Error getting serial attributes" << std::endl;
|
||||
::close(serial_fd);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Set baud rate
|
||||
speed_t baud_rate;
|
||||
switch(baudrate) {
|
||||
case 9600: baud_rate = B9600; break;
|
||||
case 19200: baud_rate = B19200; break;
|
||||
case 38400: baud_rate = B38400; break;
|
||||
case 57600: baud_rate = B57600; break;
|
||||
case 115200: baud_rate = B115200; break;
|
||||
case 230400: baud_rate = B230400; break;
|
||||
case 460800: baud_rate = B460800; break;
|
||||
case 921600: baud_rate = B921600; break;
|
||||
default:
|
||||
std::cerr << "Unsupported baud rate: " << baudrate << std::endl;
|
||||
::close(serial_fd);
|
||||
return false;
|
||||
}
|
||||
|
||||
cfsetispeed(&tty, baud_rate);
|
||||
cfsetospeed(&tty, baud_rate);
|
||||
|
||||
// 8N1: 8 data bits, no parity, 1 stop bit
|
||||
tty.c_cflag &= ~PARENB; // No parity
|
||||
tty.c_cflag &= ~CSTOPB; // 1 stop bit
|
||||
tty.c_cflag &= ~CSIZE; // Clear data bits
|
||||
tty.c_cflag |= CS8; // 8 data bits
|
||||
tty.c_cflag &= ~CRTSCTS; // No flow control
|
||||
tty.c_cflag |= CREAD | CLOCAL; // Enable reading, ignore control lines
|
||||
|
||||
// Non-canonical mode
|
||||
tty.c_lflag &= ~ICANON; // Non-canonical mode
|
||||
tty.c_lflag &= ~ECHO; // No echo
|
||||
tty.c_lflag &= ~ECHOE; // No erasure
|
||||
tty.c_lflag &= ~ISIG; // No signal handling
|
||||
|
||||
// No software flow control
|
||||
tty.c_iflag &= ~(IXON | IXOFF | IXANY);
|
||||
|
||||
// No output processing
|
||||
tty.c_oflag &= ~OPOST;
|
||||
|
||||
// Timeouts
|
||||
tty.c_cc[VMIN] = 0; // Non-blocking read
|
||||
tty.c_cc[VTIME] = 5; // 0.5 second timeout
|
||||
|
||||
// Apply settings
|
||||
if (tcsetattr(serial_fd, TCSANOW, &tty) != 0) {
|
||||
std::cerr << "Error setting serial attributes" << std::endl;
|
||||
::close(serial_fd);
|
||||
return false;
|
||||
}
|
||||
|
||||
// Flush port
|
||||
tcflush(serial_fd, TCIOFLUSH);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void TTLCommunicator::close_serial_port() {
|
||||
// In a real implementation, this would close the file descriptor
|
||||
if (serial_fd != -1) {
|
||||
::close(serial_fd);
|
||||
serial_fd = -1;
|
||||
}
|
||||
connected = false;
|
||||
}
|
||||
Reference in New Issue
Block a user