D
D
Drabadum2013-01-03 13:32:21
linux
Drabadum, 2013-01-03 13:32:21

Receiving data from Arduino in the Linux console. How?

Good time of the day!

There is an Arduino that sends a constant stream of data to a Linux computer. The data goes through /dev/ttyACM0.
Question: how to read data from it and save it to serial_port.data file?

cat /dev/ttyACM0 > serial_port.data

tail -f /dev/ttyACM0 > serial_port.data

don't help. They save only fragmentary data (2-5 characters that were transmitted during the start of the command).

UPD: Half solution.
Firstly, a delay was set for transmitting data from the Arduino through the serial port so that the data did not go in a stream of characters, but in portions. Second, in one terminal:

tail -f /dev/ttyACM0

And in the other, it is periodically performed

cat /dev/ttyACM0 >> serial_port.data

The frequency of cat execution and the delay in transmitting data from the Arduino must be matched, so the board sends one data packet, it is "remembered" by the tail command and sent to the file by the cat command. "Remembered" by the tail command is cleared. And in a new circle.

I apologize for the terms. The description is a half measure. If anyone knows how to make it better and more beautiful - welcome!

Answer the question

In order to leave comments, you need to log in

9 answer(s)
H
howeal, 2013-01-03
@Drabadum

Don't forget about stty and COM port setting. I have setup with this command.
stty -F /dev/ttyACM0 cs8 9600 ignbrk -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts raw
code for Arduino:

Serial.begin(9600);
Serial.println("START");

A daemon that reads commands from the Arduino every 3 seconds and outputs everything to the console (to sh). The text from the Arduino gets into the $LINE variable:
#!/bin/sh
insmod usbserial
insmod ftdi_sio
insmod cdc-acm

while[true]
do

stty -F /dev/ttyACM0 cs8 9600 ignbrk -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts raw

	while read LINE
	do
	
	echo $LINE
			
	done < /dev/ttyACM0
/bin/sleep 3
done


Thus, when the Arduino daemon starts, it reboots itself and writes to the console, when connecting / disconnecting, the port is reconfigured and everything continues to work further.

A
Alexey Zhurbitsky, 2013-01-03
@blo

Try

while [ 1 ]; do
    DATA=`dd if=/dev/ttyACM0 count=1`
    echo $DATA
done

D
Drabadum, 2013-01-04
@Drabadum

Wow! Thanks a lot, blo and howeal , for the replies!

V
Vladimir, 2013-01-03
@noonv

have you tried hexdump? and what's the problem with writing your own logger program? )

D
Drabadum, 2013-01-03
@Drabadum

It seemed to me that it would be nice to collect data using console means. I'll try hexdump, thanks!

D
Disasm, 2013-01-03
@Disasm

As far as I understand, you should dig in the direction of presetting data transfer parameters like baud rate and parity. If they are set incorrectly, then you will receive garbage instead of data. It seems like the setserial utility can do this.

T
Taras M, 2014-10-09
@kalduntus

hmm, came across a question. the situation is similar (arduina, linux), arduina pours temperature data every second T = ***
I decided like this:

#!/bin/bash
DATEFORMAT=$(date '+%m/%d/%y %H:%M:%S') #формат таймстампа
DEVICE='/dev/ttyACM0' #порт устройства
echo $(head -n 1 $DEVICE) $DATEFORMAT >> temperature.csv  #само собой перекидка в файл, каждая запись новой строкой.

and put in cron at a convenient time, so to speak, a temporary profit, but SO as no one else does.

K
korobkinos, 2015-07-22
@korobkinos

How to send data to arduino on COM port?

@
@ugsm, 2015-10-01
_

I am running the following script:

#!/bin/sh
stty -F /dev/ttyUSB0 cs8 9600 ignbrk -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh -ixon -crtscts raw
cat /dev/ttyUSB0 > serial_port.data
exit

Arduina sends a string like temp1; temp2; temp3 every few seconds
Logically, the ">" redirection should rewrite the file every time, and the file should always have one line, but in fact it turns out that the file is appended and grows. The output of the string to the serial port of the arduino is the Serial.println command. Maybe it is necessary to transfer some additional end-of-line character to the port so that cat understands that the file needs to be rewritten or will it not help in this case?
Second question. Once the script is launched (and even closed or killed with killall) it continues to write data to the file. How to stop it? :) You can open minicom, it displays the data (but it seemed to me, not all, because the script writes to the file more often). Regular closing of minicom does not stop writing to a file by a script.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question