Linux USB Serial Debug
Budget: £30 – £500 GBP
I have a yocto embedded board that has an FTDI USB serial chip fitted.
I need help to work out why it is not communicating with my Linux OS.
Included my test routine..
const char* port_names[] = {"/dev/ttyUSB0", "/dev/ttyUSB1", "/dev/ttyUSB2", "/dev/ttyUSB3"};
std::vector<int> baud_rates = { 115200, 230400, 460800, 921600, 1843200, 2000000, 3000000, USB_BAUDRATE_MAX}; // Add more baud rates if needed
theGPIO.SetOutput(GPIO_PORT_PERIPHERAL_RESET,GPIO_PIN_PERIPHERAL_RESET,PIN_LOW);
usleep(5000);
theGPIO.SetOutput(GPIO_PORT_PERIPHERAL_RESET,GPIO_PIN_PERIPHERAL_RESET,PIN_HIGH);
usleep(3000000);
for(int i =0 ; i <= 3; i++)
{
const char* port_name = port_names[i];
for (int j = 0; j < baud_rates.size(); j++)
{
int serial_port = open(port_name, O_RDWR);
std::cout << YELLOW << "Opened serial port: " << port_name << " - Baud Rate: " << std::dec << (int)baud_rates[j] << std::endl;
if (serial_port < 0) {
std::cerr << "Error opening serial port" << std::endl;
return -1;
}
// configure the serial port
struct termios tty;
memset(&tty, 0, sizeof(tty));
if (tcgetattr(serial_port, &tty) != 0) {
std::cerr << "Error getting serial port attributes" << std::endl;
return -1;
}
cfsetspeed(&tty, baud_rates[j]); // set baud rate
tty.c_cflag &= ~PARENB; // no parity bit
tty.c_cflag &= ~CSTOPB; // one stop bit
tty.c_cflag &= ~CSIZE; // clear data size bits
tty.c_cflag |= CS8; // set data size to 8 bits
tty.c_cflag &= ~CRTSCTS; // disable hardware flow control
tty.c_cflag |= CREAD; // enable receiving characters
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // disable software flow control
tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL); // raw input mode
tty.c_oflag &= ~OPOST; // raw output mode
tty.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN); // non-canonical mode
tty.c_cc[VMIN] = 0; // read doesn't block
tty.c_cc[VTIME] = 2; // 0.2 seconds read timeout
if (tcsetattr(serial_port, TCSANOW, &tty) != 0) {
std::cerr << "Error setting serial port attributes" << std::endl;
return -1;
}
int looped = 4;
do
{
// send a binary buffer
//const char buffer[] = {0x7e, 0xc4,0x0,0x0,0x09,0x0,0x9,0x47,0xe,0xd5};
const unsigned char buffer[] = {0x7e,0xc4,0x1a,0x0,0xd,0x0,0xd,0x4b,0x5,0x0,0x0,0x0,0xc8,0xf0}; // SET_SPEED
if (write(serial_port, buffer, sizeof(buffer)) != sizeof(buffer))
{
std::cerr << "Error writing to serial port" << std::endl;
}
std::cout << GREEN << "Buffer written to serial port: " << port_name << std::endl;
for (int i = 0; i < (int)sizeof(buffer); ++i)
{
std::cout << std::hex << static_cast<int>(buffer[i]) << " ";
}
// read the response
const int response_size = 20; // expected response size
char response[response_size];
memset(response, 0, response_size);
const int read_size = read(serial_port, response, response_size);
if (read_size ==0)
{
std::cerr << RED << "Nothing read from serial port" << std::endl;
}
else
{
// print the response
for (int i = 0; i < response_size; ++i)
{
std::cout << WHITE << i << ": 0x" << std::hex << static_cast<int>(response[i]) << " ";
}
std::cout << std::endl;
}
}
while(--looped>0);
close(serial_port);
}
}
SectionFinish("Test 1 complete");
return(true);
}
I need help to work out why it is not communicating with my Linux OS.
Included my test routine..
const char* port_names[] = {"/dev/ttyUSB0", "/dev/ttyUSB1", "/dev/ttyUSB2", "/dev/ttyUSB3"};
std::vector<int> baud_rates = { 115200, 230400, 460800, 921600, 1843200, 2000000, 3000000, USB_BAUDRATE_MAX}; // Add more baud rates if needed
theGPIO.SetOutput(GPIO_PORT_PERIPHERAL_RESET,GPIO_PIN_PERIPHERAL_RESET,PIN_LOW);
usleep(5000);
theGPIO.SetOutput(GPIO_PORT_PERIPHERAL_RESET,GPIO_PIN_PERIPHERAL_RESET,PIN_HIGH);
usleep(3000000);
for(int i =0 ; i <= 3; i++)
{
const char* port_name = port_names[i];
for (int j = 0; j < baud_rates.size(); j++)
{
int serial_port = open(port_name, O_RDWR);
std::cout << YELLOW << "Opened serial port: " << port_name << " - Baud Rate: " << std::dec << (int)baud_rates[j] << std::endl;
if (serial_port < 0) {
std::cerr << "Error opening serial port" << std::endl;
return -1;
}
// configure the serial port
struct termios tty;
memset(&tty, 0, sizeof(tty));
if (tcgetattr(serial_port, &tty) != 0) {
std::cerr << "Error getting serial port attributes" << std::endl;
return -1;
}
cfsetspeed(&tty, baud_rates[j]); // set baud rate
tty.c_cflag &= ~PARENB; // no parity bit
tty.c_cflag &= ~CSTOPB; // one stop bit
tty.c_cflag &= ~CSIZE; // clear data size bits
tty.c_cflag |= CS8; // set data size to 8 bits
tty.c_cflag &= ~CRTSCTS; // disable hardware flow control
tty.c_cflag |= CREAD; // enable receiving characters
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // disable software flow control
tty.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL); // raw input mode
tty.c_oflag &= ~OPOST; // raw output mode
tty.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN); // non-canonical mode
tty.c_cc[VMIN] = 0; // read doesn't block
tty.c_cc[VTIME] = 2; // 0.2 seconds read timeout
if (tcsetattr(serial_port, TCSANOW, &tty) != 0) {
std::cerr << "Error setting serial port attributes" << std::endl;
return -1;
}
int looped = 4;
do
{
// send a binary buffer
//const char buffer[] = {0x7e, 0xc4,0x0,0x0,0x09,0x0,0x9,0x47,0xe,0xd5};
const unsigned char buffer[] = {0x7e,0xc4,0x1a,0x0,0xd,0x0,0xd,0x4b,0x5,0x0,0x0,0x0,0xc8,0xf0}; // SET_SPEED
if (write(serial_port, buffer, sizeof(buffer)) != sizeof(buffer))
{
std::cerr << "Error writing to serial port" << std::endl;
}
std::cout << GREEN << "Buffer written to serial port: " << port_name << std::endl;
for (int i = 0; i < (int)sizeof(buffer); ++i)
{
std::cout << std::hex << static_cast<int>(buffer[i]) << " ";
}
// read the response
const int response_size = 20; // expected response size
char response[response_size];
memset(response, 0, response_size);
const int read_size = read(serial_port, response, response_size);
if (read_size ==0)
{
std::cerr << RED << "Nothing read from serial port" << std::endl;
}
else
{
// print the response
for (int i = 0; i < response_size; ++i)
{
std::cout << WHITE << i << ": 0x" << std::hex << static_cast<int>(response[i]) << " ";
}
std::cout << std::endl;
}
}
while(--looped>0);
close(serial_port);
}
}
SectionFinish("Test 1 complete");
return(true);
}