How to send and receive data from serial port using command line?
In the past, I have used c++ and python to communicate with serial ports in a Linux and Windows environment. In Linux, I have also used programs like picocom, minicom, and cutecom for serial communication but now I want to read and write to the serial port using simple Linux commands which requires no installation of external programs. I would be using this method in raspberry pi to communicate with my Arduino board. In the below example I’m using stty for setting serial port options and I use echo and cat command to send and read data from the serial port but at the end, I’m not seeing any output, I have read other posts in this site related to this but nothing seems to work for me. I’m able to communicate with Arduino using cutecom but with below commands, I don’t see any response. Linux (Ubuntu):
$ stty -F /dev/ttyUSB0 9600 cs8 -cstopb -parenb $ echo "1" > /dev/ttyUSB0 //send data $ cat /dev/ttyUSB0
#include void setup() < Serial.begin(115200); >void loop() < if(Serial.available() >0) < Serial.println("[123,55,7777]"); >>
Here i send 1 and i get the response from arduino: This should be pretty simple I send 1 or any character to Arduino and it should return [123,55,7777] in the command line. Any kind of help and guidance is appreciated. Below is the code that I have tried but doesn’t return any data.
stty -F /dev/ttyUSB0 115200 cs8 -cstopb -parenb #CONFIGURE SERIAL PORT exec 3 /dev/ttyUSB0 #SEND COMMAND HEX 0x01 TO SERIAL PORT sleep 0.2s #WAIT FOR RESPONSE kill $PID #KILL CAT PROCESS wait $PID 2>/dev/null #SUPRESS "Terminated" output exec 3
Sending HEX with CuteCom
I'm working a lot with CuteCom at the moment. But I could not yet find out how to send hex to the serial port. As an example I would like to send the character 0xFF. In the description on the internet it says, that CuteCom can do that. But how?
No, I can not tell you how. That is exactly what I try to find out. I just read on link, that it should be possible to do it: Features: . hexadecimal input and output .
1 Answer 1
In the lower right corner you can find a dropdown menu. Set it to Hex input. Now you can insert hex to the "Input" LineEdit. Do not use any 0x or similar.
Send: 0x01 0x02 0x03 0xFF 0xAF Write: 010203FFAF
I don't know the version of cutecom you work with but for me sending double digit hexadecimal numbers without the trailing 0x worked.
You must log in to answer this question.
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.13.43531
Ubuntu and the circle of friends logo are trade marks of Canonical Limited and are used under licence.
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
How to send data to a serial port and see any answer?
On Linux, I want to send a command string (i.e. some data) to a serial port (containing control characters), and listen to the response (which also usually might contain control characters). How can I do this as simplest as possible on Linux? An example is appreciated!
5 Answers 5
All devices on Unix are mapped to a device file, the serial ports would be /dev/ttyS0 /dev/ttyS1 . .
First have a look at the permissions on that file, lets assume you are using /dev/ttyS1 .
You will want read.write access, if this is a shared system then you should consider the security consequences of opening it up for everyone.
A very simple crude method to write to the file, would use the simple echo command.
You can have cat running in one terminal, and echo in a 2nd.
If everything is gibberish, then baud rate, bit settings might need setting before you start sending. stty will do that. !! NOTE stty will use stdin as default file descriptor to affect.
This might be enough for you to script something and log ? Not sure what you are trying to achieve.
For a more interactive, remembers your default settings approach would be to use minicom it is just a program which does everything I've mentioned so far. (similar to hyperterminal in Windows, you might be familiar).
An intermediate solution, would use a terminal program like screen which will work on a serial device.
man screen man minicom man stty for more information
Sending Hexadecimal data through Serial Port Communication in Linux
I have got a task of sending hexadecimal data to my COMPORT in linux. I have written this simple C code, but it sends only a decimal number. Can anyone help me in sending an hexadecimal bit. Here is the code I have written
#include /* Standard input/output definitions */ #include /* String function definitions */ #include /* UNIX standard function definitions */ #include /* File control definitions */ #include /* Error number definitions */ #include /* POSIX terminal control definitions */ int number,n; void main(void) < open_port(); >int open_port(void) < int fd; /* File descriptor for the port */ fd = open("/dev/ttyACM0", O_RDWR | O_NOCTTY | O_NDELAY); if (fd == -1) < perror("open_port: Unable to open /dev/ttyACM0 - "); >else < printf("Port Opened successfully\n"); number = 1; while(number!=55)< scanf("%d",&number); n = write(fd, "ATZ\r", number); if (n < 0) fputs("write() of 4 bytes failed!\n", stderr); >> return (fd); >
A hexadecimal value is the string representation of a binary value. Each character of the string represents 4 bits(a nibble). Serial ports can send data in 5,6,7, or 8 bit units. So are you trying to send strings or numbers?
3 Answers 3
ssize_t write(int fd, const void *buf, size_t count);
That is, it sends count bytes to fd from buf . In your case, the data is always the string "AZTR\r", plus undefined data after that (if count is > 5). Your program sends neither hexadecimal nor decimal data.
Do you want to send binary data or a string of hexadecimal characters?
For option one, you can use: write(fd, somebuffer, len); , where some buffer is a pointer to any set of bytes (including ints, etc).
For option two, first convert your data to a hexadecimal string using sprintf with %02X as the format string, then proceed to write that data to the port.