0

I'm trying to send data saved on a txt file to arduino over serial connection. I wrote this simple sketch

void setup(){
  Serial.begin(9600);
}

void loop(){
  while (Serial.available() > 0) {
    Serial.write(Serial.read());
  }
}

When I try to send serial data from bash by typing cat input.txt >> /dev/cu.wchusbserial1a1340 while the Arduino's serial connection window is open. It gives an error message "resource busy: /dev/cu.wchusbserial1a1340". Then I tried cat input.txt >> /dev/tty.wchusbserial1a1340. Now it hangs. Bash is busy, and it is not possible close Arduino IDE or serial connection window. What is wrong here?

2

I don't know the details of why the hang is occurring. However, the problem stems from serial ports being available to one process at a time. When the Arduino-environment's serial-port-monitor opens a serial port, that port becomes unavailable to bash. (To close the hung Arduino IDE process and the SPM, say ps aux |grep arduino and kill n, where n is the PID shown by the ps and grep.)

Here is a python program you can run, that sends and receives data:

#!/usr/bin/env python
# Send data to an Arduino and report responses.
from time import time, sleep, asctime, localtime
from serial import Serial
print 'Opening serial port'
ser = Serial('/dev/ttyACM0', 9600, timeout=3)
sleep(1)                      # Let board initialize
while 1:
    txt = 'It is about ' + asctime(localtime()) + ' now'
    print 'Sending  "{}"'.format(txt)
    ser.write(txt)
    s = ser.readline()          # Get result from arduino
    print 'Readback "{}" at {}'.format(s, asctime(localtime()))
    sleep(1)

Here is some sample output from it, with your sketch runing in a Mega2650. (I'm not sure why the readback is taking so long as it apparently is.)

Opening serial port
Sending  "It is about Fri May  8 16:48:41 2015 now"
Readback "It is about Fri May  8 16:48:41 2015 now" at Fri May  8 16:48:44 2015
Sending  "It is about Fri May  8 16:48:45 2015 now"
Readback "It is about Fri May  8 16:48:45 2015 now" at Fri May  8 16:48:48 2015
Sending  "It is about Fri May  8 16:48:49 2015 now"
Readback "It is about Fri May  8 16:48:49 2015 now" at Fri May  8 16:48:52 2015
Sending  "It is about Fri May  8 16:48:53 2015 now"
Readback "It is about Fri May  8 16:48:53 2015 now" at Fri May  8 16:48:56 2015
  • Your code was really helpful. Only think I didn't understand is sleep(1) command. Is this a part of Serial module? – wizofwor May 10 '15 at 20:17
  • Now I understand it was needed to wait for arduino. – wizofwor May 10 '15 at 21:04
  • sleep() is from the time module. Its real-number argument is in seconds. With the first sleep removed, the first send to the arduino gets dropped but it works ok after that. Note, if you want to send a file, you can replace the txt = ... line with txt = raw_input() and pipe the file into the program, eg programname < fileoftext. See raw_input() writeup – James Waldby - jwpat7 May 10 '15 at 21:06
1

As noted by jwpat7 the port can be accessed by one application at a time, so when the serial monitor is open no other application can access it.

The hang might happen because when first connecting to an arduino port the arduino gets reset and waits for a second or so for programming data (that's how a new sketch is updated). I don't know for sure, but this might mess up with cat and make it to hang up.

Best option might be to make a script like jwpat7's that would wait a second after opening the port and then send the file contents in.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.