NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.
Microcontroller Programming » How to use putty to read and write to a text file in C
November 25, 2013 by jmuthe ![]() |
Hello I saw the "USB Servo-Guided Water Squirter" in the tutorial section and I thought it was interesting how the user could use Putty to intput and output data between the microcontroller and the computer screen. I was wondering how I could take it a step further and output that data to a text file. I also want to know how to put data into a text file and have it sent to the microcontroller. When I tried to look up my answer on this forum, I think I found some examples but they all used Python programming. I was wondering if it is possible to do it in C and if so how would it be done? Does anybody have a simple bare-bone sample program that does this? Thanks for your help. |
||||||
---|---|---|---|---|---|---|---|
November 26, 2013 by scootergarrett ![]() |
I was all about this a little while ago. Anyways a C program can talk to the microprocessor through the serial com that is used to load programs. The microprocessor end is easy. I had more trouble with the C program. The C program needs to set up with the com port then there is a function to send or receive a string over the com port. The data must then be parsed out of the string. My code below is some of the code form this project Microprocessor sends A/D value to com port 100 times a second
C code reads and displays
I’m assuming you know C to the point where you could figure out what my code is doing, if you don’t let me know. |
||||||
December 02, 2013 by jmuthe ![]() |
I appreciate the response but I am a little confused by it. First of all, I see two pieces of code that you wrote so I was a little confused with what to do with them. I sent the the first piece of code to the microprocessor and I compiled the second piece of code to my PC. I had the microprocessor connected to the computer and then I ran the second code on the computer, while simultaneously running the other code on the ATMEL328 chip. The result was that I kept seeing the message "current number 4" printed on the screen. I used a potentiometer to control the voltage instead of your alcohol gas sensor but no matter what I set it to I kept getting the same message. The number "4" never changed. However, my goal is not to simply make the Nerdkit communicate to the computer and display values on the screen. I want to actually make it either create a text file (like in notepad) or read information from a text file. For example, this is an example of a C code that creates a file called "out" and writes the numbers 1 to 10 on it.
This is a code that reads 1000 character from a text file called "infile" and prints them on the screen.
Both examples are from "HowStuffWorks.com" The following code that I wrote in the bottom is basically the simpliest C code that I could use to write to he PC. I basically used the "USB Servo-Guided Water Squirter" video in the tutorial section and got rid of everything in it except the part of the program that prints to the PC. The user has to open up a PUTTY link just like in the "USB Servo-Guided Water Squirter" and the program asks the user to type a number on the keyboard and it simply doubles it and displays it on the LCD and PUTTY screen. How do I modify this program to not just print it on the screen but to also create a text file and write the information to it just like in my first program shown above.
|
||||||
December 03, 2013 by scootergarrett ![]() |
I'm not sure why it always returns 4. I wrote some more code that is a little more stripped down. This first set if for the microprocessor, it samples PC0 and then sends the data in raw format to the computer. I send 0xFF after for error checking.
Then this code is compiled and run on my PC after the microprocessor is up and running. it sets up the communication then starts pulling values. It prints every 50th to the screen and saves all the data to a text file 'Data.txt'.
I just test it by connecting PC0 to ground then power, this is what the screen looks like as number scroll by |
||||||
December 03, 2013 by scootergarrett ![]() |
And here is communication in the other direction. this microprocessor code takes data (uint8_t) from the com port and displays it as a character on the LCD.
so then this code runs on my PC after the Microprocessor is running. It takes the character hit from the keyboard and sends it to the com port.
Overall this code displays the what you type on the LCD screen. note that backspace needs work and the program ends when you hit enter. |
||||||
December 04, 2013 by jmuthe ![]() |
Thanks a lot. The programs worked. |
||||||
December 13, 2013 by scootergarrett ![]() |
I just started getting into GUI so now I have a computer program that opens a new window with a slider and this controls a PWM pin and the brightness of a LED. This works on Code blocks on windows XP, I had to do include some libraries in the compiler and debugger setting stuff I don’t understand. Here is the computer code:
And the MCU code, it could use work but is fine for now:
I want to get some more GUI stuff with the microprocessor set up in the future, maybe a slider that controls the set thermostat temperature. Just thought I would share |
Please log in to post a reply.
Did you know that you can make a capacitive proximity sensor with some aluminum foil and paperclips? Learn more...
|