How to read USB serial input in a cross-platform way in C? - 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.

Related

How can I read in M-bus metering data with an Arduino Uno?

I am trying to send data from a Kamstrup Multical 601 to an Arduino Uno using the M-bus protocol.
I am considering trying to use the libmbus c libraries to do this. However, I do not have a lot of experience in c programming so was wondering if:
you think this is a realistic/achievable approach?
anyone could suggest an alternative/easier approach?
The main chip on the Arduino Uno is the Atmel Atmega382P-PU.
After getting the data to the Arduino I aim to perform some calculations and send data to an LCD (this I think I can do).
On the Arduino Website there is a short how-to about the use of external C-Libraries with Arduino.
Note that you cannot simply connect M-Bus with a RS-232 interface. There is a so called "level-shifter" device necessary inbetween to do the "electrical transition". See the EN 13757-2 standard doucment for what this device is exactly doing with the signal. Without such a device you won't get any word out of your M-Bus device.
The library you link to appears to be for Linux. The Arduino, of course, doesn't run Linux so a library won't compile for it directly.
You should probably try implementing the library yourself, but using the Arduino's standard libraries to access ports and so on.

reading USB port in Linux

I am writing a program to read data from a Bluetooth USB dongle. I am using Linux, so I suspect there may be a POSIX library to read from it, or perhaps there is a predefined file descriptor for each USB drive. How do I read a stream of data from a USB port in C?
The most common way of interacting with random USB device is libusb. This provides low-level access to the device, so if you want something more complex (for example, if it's an actual USB drive with a filesystem on it), you might want to use some existing driver for the device rather than trying to interact with it directly.
OK, given the answer above: the PS3 controller is, almost certainly, a HID device. You pair with it like you do any other Bluetooth HID device. It will appear as a joystick (or mouse, or keyboard, as appropriate) automatically once paired, with no software required on your part. This site seems to have a guide, though obviously I haven't tested it: http://www.ydl.net/support/solutions/ydl_6.x/ps3_bluetooth_sixaxis.shtml

How to do this: embedded USB-Host communication with plugged USB-Device

I am currently practising with USB programming on an AT91SAM9G20-Evaluation Kit. I learned much about USB devices and USB device port drivers while "playing" with the ATMEL provided USB device port projects (CDC-driver, ..).
But now I'd like to write a small driver to controll a wireless stick
which I plugged into one of the boards USB-A Host Ports.
I read a lot on Stack Overflow, the OpenHCI specification and even found some libraries on the net, but I am not sure if it's a good way to implement my own stack without any "good" knowledge in USB Host Port programming.
Is there a small and easy way to control the wireless-stick at the boards USB Host port? (like using the USART-Interfaces?).
I am also keen to hear hints on how to implement RTUSB or libUSB in to the AT91.
You can download AT91LIB version 1.9 from atmel from this page
The usb host libraries are under at91lib/usb/host. They're not the complete package you need though since they're just the OHCI driver -- you still need a USB driver and class drivers to implement what you want.
You could try an RTOS with USB Host support like rt-usb32

microcontrollers and file IO

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.

does ansi c allows serial communication on rs232 null modem?

does ansi c allows serial communication on rs232 null modem?
if yes then any one give me example?
thanx in advance
Ansi C has nothing to do with serial communications. ANSI C is a formal description of the C language, it doesn't define libraries. Communicating with an RS-232 modem is possible with C language, but it will be completely different in different platforms (Windows, Linux, embedded processors).
It does, in as much as there's nothing in the language which prevents it, but it depends on your OS/platform to provide access to an appropriate device driver (or an abstraction of one).
For an (elaborate) example, refer to the source to the minicom package (GPL). Something simpler (on a POSIX-like platform) might involve opening a device node, using ioctl to configure it (baud rate etc), then you can simply read and write on the open file descriptor.

Resources