programming an arduino to read sd card and send lines of text over serial

3,297 views
Skip to first unread message

Geekinesis

unread,
Jun 22, 2011, 10:27:52 AM6/22/11
to London Hackspace
Hi
I know its possible to tell an arduino to read a file from an SD card.
I would like to parse a file and send one line at a time over a serial
link. Waiting for a serial text reply before sending the next line.

The idea is to send gcode to a second arduino running grbl. So far I
am sending the code with a PC.

Is this relatively straightforward to do? I have little arduino
experience.

I am working on a cnc plotter project which I want to enter into
Brighton make fair.

Tim Hutt

unread,
Jun 22, 2011, 10:40:05 AM6/22/11
to london-h...@googlegroups.com
On 22 June 2011 15:27, Geekinesis <geeki...@googlemail.com> wrote:
> Hi
> I know its possible to tell an arduino to read a file from an SD card.
> I would like to parse a file and send one line at a time over a serial
> link. Waiting for a serial text reply before sending the next line.
>
> The idea is to send gcode to a second arduino running grbl. So far I
> am sending the code with a PC.
>
> Is this relatively straightforward to do? I have little arduino
> experience.

I've never done SD card stuff, but serial is very simple:
http://arduino.cc/en/Tutorial/ASCIITable

By the way, the easiest way I've found to do serial comms from a
computer is using boost's asio serial library. I have some example
code if anyone wants...

Nicholas FitzRoy-Dale

unread,
Jun 22, 2011, 11:25:30 AM6/22/11
to london-h...@googlegroups.com

On 22/06/2011, at 3:27 PM, Geekinesis wrote:

> Hi
> I know its possible to tell an arduino to read a file from an SD card.
> I would like to parse a file and send one line at a time over a serial
> link. Waiting for a serial text reply before sending the next line.
>
> The idea is to send gcode to a second arduino running grbl. So far I
> am sending the code with a PC.
>
> Is this relatively straightforward to do? I have little arduino
> experience.

The hardware portion is pretty trivial, as is the code required to read raw data from the SD card, since SD cards support SPI:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1235125412/all

If you have an actual file system on there, you will have to also implement a FAT filesystem reader (some are linked in the thread). You might find it easier just to write the gcode to the SD card in raw format (i.e. using dd on Unix) and avoid the filesystem altogether.

Nicholas

tom

unread,
Jun 22, 2011, 12:47:30 PM6/22/11
to London Hackspace
I'll be trying this at some point soon as I want to build a datalogger
for a mad idea i have, if i get anywhere i'll post and let you know :)

On Jun 22, 4:25 pm, Nicholas FitzRoy-Dale

Daniel Sikar

unread,
Jun 23, 2011, 2:18:22 PM6/23/11
to London Hackspace
Wiring up TXD, RXD, RTS# and CTS# between Arduinos should be
straightforward.

Martin Klang

unread,
Jun 23, 2011, 4:08:10 PM6/23/11
to london-h...@googlegroups.com

TX to RX and RX to TX is all you need for two-way comms. I wouldn't worry about hardware flow control.

Any reason you can't get the second arduino to read from the SD card itself?

/m

Geekinesis

unread,
Jun 23, 2011, 6:02:19 PM6/23/11
to London Hackspace
hi
Its more the code for parsing of the text file and sending line by
line I am trying to work out.

Mark Steward

unread,
Jun 23, 2011, 8:07:57 PM6/23/11
to london-h...@googlegroups.com
This is as simple as (after installing python-serial):

import serial

# Set this to the right one for your system:
dev = '/dev/ttyUSB0'
#dev = '/dev/ttyACM0'
#dev = '/dev/tty.usbserial123456'

arduino = serial.Serial(dev, 115200)

with open('file.txt') as file:
  for line in file:
    arduino.write(line)
    input = arduino.readLine()


Mark

Daniel Sikar

unread,
Jun 23, 2011, 8:13:37 PM6/23/11
to London Hackspace
Here's a suggestion sending byte by byte - grbl can deal with carriage
returns:

file myfile;
byte outbyte;

int sendgcode2grbl(string filename)
{
// initialise sd card and open file
SD.begin();
if(!(SD.exists(filename)))
return 0;
myfile = SD.open(filename, FILE_READ);

// initialise serial port
Serial.begin(9600);

// send data one byte at a time
outbyte = myfile.read();
while(outbyte>-1)
{
Serial.write(outbyte);
outbyte = myfile.read();
}

// tidy-up
Serial.end()
myfile.close();
return 1;

Mark Steward

unread,
Jun 24, 2011, 3:40:08 AM6/24/11
to london-h...@googlegroups.com
Ahh, I misread the original request.  What an idiot.

Geekinesis

unread,
Jun 24, 2011, 5:09:56 PM6/24/11
to London Hackspace
ah but you read my mind, I was looking for a python solution as well
that I could run from linux.

Thanks for the code daniel, I will experiment.
Reply all
Reply to author
Forward
0 new messages