Reading from a Textfile

Hello All,

I searched the forums, but I can't seem to find a simple way to read a textfile from my PC desktop. I can figure out how to take the string and load it into an array, but what is the code to open the textfile? Do I need to include any libraries?

Sorry all. I'm new to the Arduino world. Thanks in advance for any help!

There has to be a program on your PC to open the file and feed it through serial to the Arduino.

Linux and XP do have shell commands that can do it but I haven't used those in 15 years. I don't truest myself to get all the details.

Generally you write a script or batch file that sets the COM (I think mode does that.) and then redirect the file to COM for output. Two lines should do it if you can handle command line scripts.

You can get fancier and pass args to the script or write a bunch of Java or Python but why bother unless you don't know shell and the command interpreter? I would have to do that or relearn what my system uses so yes, I do know what I tell you.

Hello and welcome :slight_smile:

There is no simple way to access files on your PC, you will have to write a small PC program (I recommend C# because it's simple enough to learn and quick to develop), which will act as a server between your arduino and your PC, by using a Serial port

Arduino send a "read file " command to the server -> Server will read the file, and send it to arduino one byte at a time

You will have to process those bytes as quickly as possible when they arrive in the arduino's serial buffer, before it gets filled and overwritten by newer data.

But, what do you want to do exactly, what is the content of the file?

guix:
There is no simple way to access files on your PC, you will have to write a small PC program (I recommend C# because it's simple enough to learn and quick to develop), which will act as a server between your arduino and your PC, by using a Serial port

Arduino send a "read file " command to the server -> Server will read the file, and send it to arduino one byte at a time

You will have to process those bytes as quickly as possible when they arrive in the arduino's serial buffer, before it gets filled and overwritten by newer data.

Where did you get that last idea? it is completely false. When the buffer fills the serial transfer will stop until the buffer has space cleared in it, period. Every read() takes a char out of the buffer or zero if there is none but not ever does new data overwrite the buffer. That would be stupid in the extreme and serial devices would never have worked as they have in over 50 years now.

Learn about command line, shells and scripts. Technically scripting is programming but it's far simpler to learn than C#, Java, Python, Perl, etc, yet very powerful, quick and easy to write.

It's funny, I wrote major C++ for a guy yet when he saw me write a 4 line batch file, that's when he started calling me a "power user".

When the buffer fills the serial transfer will stop until the buffer has space cleared in it, period.

That is NOT even remotely close to true. When the incoming buffer gets full, data is discarded.

The outgoing buffer being full will stop the Arduino from sending more data.

The incoming buffer being full will NOT stop the device on the other end from sending data.

I must be thinking about serial with handshaking lines, the full cable.

Hmm, well sorry, if I was wrong and that the data is not overwritten. But still there is data loss when buffer is full.

I wrote some batch files too, it's useful for many simple things, but for transfering a file over serial, I'm not even sure if that is possible and if it is, writing this in bat or even vbs, would be a nightmare, for me at least :slight_smile:

And I believe he want more than just drag and drop a file on a shortcut to upload it to his arduino, I think he want the arduino to be able to select a file and read it. Well that's what I understand from his short description.

Thanks all for the responses. I am trying to control the Arduino with the the "commands" in the textfile. The textfile looks something like this:

D20
L19
C52

It is a textfile with a single column. These are not really "commands." Essentially, I was going to take the data from the textfile (that was on my desktop), have my Arduino read it, and load into an array. The data would then be processed using conditional statements.

I've programmed in C++ a long time ago so, I'm a little rusty. I remember adding iostream and fstream in C++ if I wanted to read/write to a file. Does the Arduino environment have these classes? Can I somehow import libraries into Arduino that has the functionality I need?

The arduino can do file I/O on an SD card but you'll need an appropriate shield to plug the card into.

It cannot however do file I/O directly on a file on your PC. As Guix noted above, you need some other means to transfer the data. Easiest is to use the serial port but you'll need to write something on the PC that will transmit it.

Alternatively, you can buy an Arduino that has ethernet capability and communicate that way - you'll still need code on the PC to handle it though.

ahhh... Thanks. I found some sd breakout boards online. I will use it with my Arduino.

If the text won't change, you could put it into PROGMEM and avoid the need for a RAM array.