microcontrollers and file IO - c

I'm programming a 8051 microcontroller system (in C) connected to a PC via a serial port. I'd like for the µC to write to a file on the PC. Is there a simple general way to do this from the C level?

I'm not aware of a off-the-shelf way to do this, but it's not hard to develop yourself. You will need to:
Define a serial protocol for transmitting the file data. There are existing protocols from the old dialup modem days, but they might be too complex. See: http://en.wikipedia.org/wiki/List_of_file_transfer_protocols
Write your microcontroller code to transmit the file data over the serial port, using your protocol.
Write a program that runs on your PC to receive the data and write it to a file.

Related

File access between mcu and PC through rs232 communication

I'm using a LPC178 development board and I want to read a file present on a Windows PC. My dev board only has a RS2323 interface to communicate with.
How can I go about transferring the file from my PC to my MCU using a RS232 (serial) link? I found a reference which explains how to transfer data between a MCU and PC but it isn't about file transfers.
Afaik there is no easy solution for this like calling something like "copy" or "fopen" over RS232. I would be happy to be proven wrong here.
The fastest solution might be to write a little programm running on your Windows Host, which listens to your RS232 communication and pipes your communication into/out of the file based on your communication protocol. This can be done with standard file operations in the language of your choice, for example C, C++ or Python.
Your problem is one of the oldest in the book. How do you transfer files without fancy operating system abstractions. For RS232 (or any other serial method) there exists many file transfer protocols.
One of them is kermit. This is a protocol from 1981 and can transfer binary and text files. It is able to be embedded in a micrcontroller and there exists programs to transfer/receive using kermit.
alternative old site for reference
In the simplest case you would use a file transfer protocol such as XMODEM, YMODEM, ZMODEM or Kermit - these protocols were designed in the days before networking and the Internet were ubiquitous and deal with simple point-to-point transfers between two computers. They are supported bu most terminal emulator tools such as TeraTerm Pro or PuTTY so no specific PC software need be written, just the microcontroller end.
A more complex but flexible solution is to implement a TCP/IP stack and a PPP driver, and an FTP application layer - probabaly only practical if using a third-party TCP/IP stack and application layer. You can then use any FTP client for the PC end, so again no PC software required. While this may be overkill if all you need to do is transfer files, it has the advantage of allowing you to use the the single serial port concurrently for other data streams and application protocols such as Telnet. The disadvantage perhaps is that while Windows does support PPP it is buried within the dial-up networking and to be frank a pain to get working.
Very first step you have to do is ensure serial communication is working fine.
Send a byte continuously from mcu to PC and display it on some io console (for example: HyperTerminal, Dock light )
Receive a byte to mcu from PC and echo it back to PC.
Once you are sure that serial communication is working fine then select some file transfer protocol and implement it.
While you can select any of the available protocols or write your own protocol and implement it.
For purpose of discussion i select Xmodem protocol.
If you consider some other protocol you may stop reading answer here.
XMODEM is a simple file transfer protocol.
Refer http://web.mit.edu/6.115/www/amulet/xmodem.htm for detailed information.
You may implement Xmodem mcu side by reading protocol. Or may consider using open source also ( if available )
PC side i prefer to use HyperTerminal io console as it is compatible with Xmodem.
In HyperTerminal all that i have to do is configure settings and select file for transfer to mcu.
Now transfer any file to mcu using Xmodem protocol from PC.
What you do with received file in mcu is up to you : )

How to read USB serial input in a cross-platform way in C?

I'm trying to read serial input from a USB device with 9600 baud into a C program, but I'm not sure how to go about this. The program input will be really simple. I have a circuit set up with a potentiometer and its sending the voltage value every second over the USB.
How do I read this into a C program being developed on Windows? I'd prefer something cross-platform if possible.
I assume you are using arduino-like device, which can be easily configured to output serial data over USB. See a tutorial on the subject.
USB is not as simple as legacy serial ports and USB devices will need drivers. Class compliant devices are usually supported directly by operating system, see USB device classes, at least to some extent.
For example, if you are using Arduino, the simple way is to install FTDI drivers (see their website) and use the virtual COM port provided by the driver.
Communication over a COM port is a well-covered subject and you should be able to find a vast number of documentation over that. There are also cross-platform serial communication libraries that could make your development easier.
Then you could also write your own library for the device, but that would probably be an overkill if all you want is to read in a voltage.
You can either do something like this in your code to write seperate functions for Linux and windows...
#ifdef __unix__
...
#elif defined(_WIN32) || defined(WIN32)
#define OS_Windows
#endif
Or search for C libraries for cross-platform serial communication, here is one I found for C++ in one google search https://github.com/wjwwood/serial.

How to send data from Arduino Uno to PC using the USB port

I'm building an NFC project in which I have an Arduino Uno with a PN532 NFC shield that reads stored messages from tags. What I'm trying to achieve is to store/write the information to a .csv file, but I need to send the data over the USB cable through which the Uno is connected to on my PC.
Is there any way to write the data to .csv file onto the Arduino, then send the .csv file to the PC over the USB cable to a given directory, or write a program that reads the data being sent from the Uno through the USB cable, and then write the data on the PC to a .csv file? I'm aware I could get a breadboard with an SD card reader, and write the data to an SD card but I'm trying to avoid this solution if possible. Is there any way to send files or data over USB from Arduino to PC?
I would definitely have the Arduino send the values over the USB interface and have the host run a Python program that uses the CSV library to write a .csv file.
You could simply "print" the values from the Arduino to the USB interface and read them on the host using /dev/ttyUSBx. But at some point you might want to send control commands to the Arduino. And you might want to do some logging, catch errors, etc. If so, I suggest that you look at a full-fledged communication protocol. I realize you're not controlling a robot, but rosserial from Robot Operating System (ROS) would make this easy. It might feel like overkill but I think you'll appreciate the features once you start using it.
Do you need an example?

How to read data from USB Port?

I never done the Universal Serial Bus port data read using C. I wanted to know how can data be read of Universal Serial Bus port using C language.
You can look at something like libusb, but you also should read up on USB basics. It's a bus, not a "port", so there is a lot more complexity than you might be expecting.

How to find the base address of usb to parallel port device in Linux?

I am doing IO programming in C in Ubuntu. And I need the base address of the port to write data.
My laptop dont have a parallel port. So I bought a USB to Parallel port connector. I plugged in the device and its getting detected in /dev/usb/lp0
I ran "lsusb" to see the list of devices and I can see the ID also. But how can I get the base address ? For the usual hardware parallel devices, the base address is 0x0378. this address is not getting detected while using USB to Parallel device.
Please help.
A USB parallel port doesn't have a base address - it's not a meaningful concept for USB. I'm afraid the days of doing I/O on PC hardware via in and out instructions ended a few years ago, though lots of old tutorials still survive on the web.
You can write bytes to the parallel port as a character device, and these will appear on the printer port pins. The USB adapter will expect the other end to handshake data exactly like a printer. If you want to do general I/O prototyping, you're probably better off with a simple USB microcontroller like an Arduino.
Further discussion here.
If you are still interested to use this USB-to-parallel-printer device for your own bit-banging, it's important to know that their built-in firmware always allows controlling of D0..D7, INIT (as outputs), /ERR, ONL, PE (as inputs), but never for /ACK, BUSY (inputs), /STB, /AF, /SEL (outputs) pins.
And you need an 8-bit latch (e.g. 74HCT574) for catching data while strobing.
See here (https://www-user.tu-chemnitz.de/~ygu/bastelecke/PC/USB2LPT/faq#DIY)
especially for possible data rates.
Accessing from software side is a bit complicated but possible, and you may have to re-structure your software and hardware for making such adapters useable. I don't know for Linux case how to access, but IMHO you don't need to write a kernel-mode driver.

Resources