0

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.

Tiernan Watson
  • 313
  • 2
  • 12
  • "C Program" is not enough information. What platform is your C program on? – Burstful Nov 03 '17 at 22:23
  • Developing on Windows, but I'd prefer a cross-platform solution. – Tiernan Watson Nov 03 '17 at 22:34
  • Also, you say "USB". Describe this please? To start, maybe download PuTTY from internet, and use serial communication choice, set to 9600 baud rate, and your correct COM port, and see if you're even getting data. – Burstful Nov 03 '17 at 22:36
  • You ask for "cross platform". Are you asking for a C program that reads your COM port whether you're on Windows or Linux? – Burstful Nov 03 '17 at 22:39
  • Yes, sorry. Not asking for code specifically, but at least an arrow in the right direction. – Tiernan Watson Nov 03 '17 at 22:42
  • Can I ask ( before I get into what I would do ), what is the purpose of your C program reading from USB which I am assuming is UART. – Burstful Nov 03 '17 at 22:43
  • There's no cross-platform way to do this. – Barmar Nov 03 '17 at 22:43
  • @Barmar You can definately determine the OS in your code, and use different methods depending on that result. – Burstful Nov 03 '17 at 22:44
  • If you have a USB serial device, there's usually a driver that makes it look like a traditional serial device, you don't need to deal with USB in your application. – Barmar Nov 03 '17 at 22:44
  • @CoreyLakey That sounds like platform-dependent code, not cross-platform. – Barmar Nov 03 '17 at 22:45
  • Its an arduino. I looked at their section on using it with other software, but it seemed very platform dependent. I want to take button input and use it to control a C game. – Tiernan Watson Nov 03 '17 at 22:46
  • @TiernanWatson, do you want to know how to write code that works on multiple platforms, or are you asking for a library that gives you "read" functions for serial communication that works on all platforms? – Burstful Nov 03 '17 at 22:49
  • I would prefer a library that works on all platforms, but if that is not available then I would like to know how I could write this code. – Tiernan Watson Nov 03 '17 at 22:51
  • https://stackoverflow.com/questions/8666378/detect-windows-or-linux-in-c-c – Burstful Nov 03 '17 at 22:55
  • It's not technically "cross-platform", but libusb exists on most. – Lee Daniel Crocker Nov 03 '17 at 23:01

2 Answers2

0

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.

Burstful
  • 347
  • 1
  • 10
  • Also check it out https://stackoverflow.com/questions/2973402/c-cross-platform-rs-232-serial-library – Burstful Nov 03 '17 at 22:56
0

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.

Sami Hult
  • 3,052
  • 1
  • 12
  • 17