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