I can read the Arduino Nano’s serial output on Windows and on my Jetson TK1 board using Minicom, but I am unable to read the serial output using the code below.(I connected arduino nano via USB, for your reference. my final goal is using FTDI F232 USB to UART for pixhawk)
I installed the serial library with the command: sudo apt-get install libserial-dev.
The baud rate is set to 115200, and the serial settings are 8N1. I followed the official GitHub example of libserial, but due to the old version of the library, it didn’t work. I then tried to write my own code based on the header files in /usr/includes, but it still doesn’t seem to work.
Here is my code:
src/ReadingSerialLineRaw.cpp
#include <iostream>
#include <SerialStream.h>
#include <SerialStreamBuf.h>
int main(void)
{
LibSerial::SerialStream serial;
serial.Open("/dev/ttyUSB0");
if (!serial.IsOpen()) {
std::cerr << "Failed to open serial port!" << std::endl;
}
// Set the serial port parameters
//serial.SetBaudRate(LibSerial::SerialStreamBuf::BAUD_57600);
serial.SetBaudRate(LibSerial::SerialStreamBuf::BAUD_115200);
serial.SetCharSize(LibSerial::SerialStreamBuf::CHAR_SIZE_8);
serial.SetParity(LibSerial::SerialStreamBuf::PARITY_NONE);
serial.SetFlowControl(LibSerial::SerialStreamBuf::FLOW_CONTROL_SOFT);
//serial.SetStopBits(LibSerial::SerialStreamBuf::STOP_BITS_1);
serial.SetNumOfStopBits(1);
// Read data from the serial port
std::string data = "none";
while (true) {
std::getline(serial, data);
// It prints garbage data...
std::cout << "Received data: " << data << std::endl;
//std::cout << "Program is running" << std::endl;
}
return 0;
}
CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 2.4)
project(ReadingSerialLineRaw)
# include_directories(/usr/include)
# Specify the library directory if it's not in the standard path
# link_directories(/usr/lib /usr/local/lib)
# Add the executable target
add_executable(ReadingSerialLineRaw src/ReadingSerialLineRaw.cpp)
# Link the library to the executable
# target_link_libraries(HelloWorld SerialStream)
target_link_libraries(ReadingSerialLineRaw serial)
I thought this was a relatively simple example, so I expected ChatGPT to provide the result, but the Google search results were much better.
Following this example seems to work, but it looks too complex. It’s like working with an embedded system. Is there no simpler serial library for the Jetson TK1?
[1] Are you using the devkit or custom board for TK1?
→ My board is a genuine TK1 with no customizations. I’ve only added an Intel AC7260 wireless PCIe card.
[2] What’s your Jetpack version in use?
→ latest version for tk1 is L4T 21.8 and Jetpack 3.1. I’m using Jetpack 3.1 with the Greench kernel.
[3] Is there any issue with the example in the link you shared?
→ I initially uploaded my question, but I found the answer a few minutes later and posted it again.
The example in the link works, but the code is too complex.(I want simple, high-level library, and it seems that low-level)
and It doesn’t function correctly immediately after execution. It prints insane characters for about a minute, and then starts receiving proper characters.
I can’t help with your particular example, but some comments may be useful:
If the port is set up already (e.g., speed, 8 bit, no parity, 1 stop bit, no flow control), then you just open the serial port like a file and read or write to it (there might be a need to read or write smaller amounts in some cases).
Settings which control a device special file tend to be in the form of an ioctl() call. Some other variants for specific cases exist, e.g., the tcsetattr(). It depends on the driver beneath the file as to what is used. It is possible for one serial UART driver to work with a given setting call that another might not work with.
You’ll want to start your program just with the open(). See if it works. Then, if it fails, go on to set one attribute at a time until it works, e.g., set speed next to 115200.
The default setting of the Jetson ports are:
speed 115200
8-bit
no parity
1 stop bit
flow control is not normally enabled
If you want a test, put the port in loopback mode. Wire TX of that port to its own RX, and in the case of flow control, wire the port’s own CTS to its own RTS. Then see if your console terminal echos back when you type in. A port might have the wrong settings to talk to another port, but a port will seldom disagree with itself.
I strongly recommend loopback testing initially. Even if flow control is not used it is ok for loopback to start with CTS and RTS wired together if working with loopback (this will have no effect until flow control goes on).
The FTDI serial UARTs are generally rather reliable. How is the wiring layout arranged? Is the USB plugged in to the Jetson? Or is it plugged in remote to the Jetson? If the individual TX/RX and GND are plugged in to the Jetson, which connector pins are used? I ask this latter question because the TK1 comes with an actual DB-9 connector, and I don’t know if your FTDI UART is designed for that voltage level. What voltage level is your FTDI specified for and what kind of conversion of voltage level might be present? If GPIO is involved, then that changes things a lot.
As I mentioned earlier, I’m using an FT232 UART-to-USB adapter, so I’m not utilizing GPIO pins; the USB is plugged directly into the Jetson board.
Thank you for your interest and assistance. I’ll keep your suggestions in mind if I use GPIO pins in the future.
My partial solution can be found here:
Due to the large volume of mavlink data, specific serial connection settings are needed. the simple serial read-write example code worked for arduino nano, but didn’t work for mavlink. However, I still haven’t found a perfect solution, and the signal loss rate is not zero (whereas, on Windows, the signal loss rate is 0%).
I want to confirm whether it is normal for the USB serial connection loss rate to be 0 percent in jetson board. On Windows, the loss rate is 0 percent. I believe the problem lies in my code, but I want to confirm this.