I have an Arduino connected to a PC. I need to be able to control the I/O pins on that Arduino from a C/C++ program running on the PC. Obviously, it will require some program on the Arduino to run and monitor commands coming in from the serial/usb port. Then on the PC side there is some library to send those commands to the Arduino. I just couldn't find any such solution. Everything I found is sending commands via the Arduino IDE or a separate standalone program but not through some library I can use in a C/C++ project. There has to be someone that did this before.
Just use the standard C/C++ support to open,read,write and close the serial port.
@oldcurmudgeon Ok, so I open the serial port to the Arduino. But what do I send to put a pin low or high? There has to be some command I send over the serial port and there has to be some program on the Arduino that takes that command and sets a particular pin as output (in my case) and sets that pin high or low.
I can write an Arduino program myself to do all of this but there has to be some Arduino program and corresponding Windows library that sets pins to in/out/pwm and sets and reads pins. I just don't want to re-invent the wheel.
I moved your topic to an appropriate forum category @mulu.
In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.
This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.
Thanks in advance for your cooperation.
Your code on the Arduino receives the command from the PC and determines what to do. Set pins high, low or whatever. I don't know of any generic libraries for this. It is dependent on what your application wants to do.
Nothing too magical about what you want to do.
Search the WEB for a C++ program that sends data out on a serial port, obviously the same port the Arduino is attached.
On the Arduino there is an example in the IDE on how to read data in the receive buffer.
Depending on what you receive, the Arduino does the action you program.
Thank you all for the reply. It sounds like nobody created a library to do all of this like
OpenLink(int COMPort, int baudRate, ...)
SetPinMode(int pinID, int mode, ...)
SetPinValue(int pinID, int value)
GetPinValue(int pinID, int &value)
etc.
A library is not needed for something as simple as this.
Here is one example from the Arduino IDE; will leave it to you to find an example for the PC that sends the data.
/*
Dimmer
Demonstrates sending data from the computer to the Arduino board, in this case
to control the brightness of an LED. The data is sent in individual bytes,
each of which ranges from 0 to 255. Arduino reads these bytes and uses them to
set the brightness of the LED.
The circuit:
- LED attached from digital pin 9 to ground through 220 ohm resistor.
- Serial connection to Processing, Max/MSP, or another serial application
created 2006
by David A. Mellis
modified 30 Aug 2011
by Tom Igoe and Scott Fitzgerald
This example code is in the public domain.
https://www.arduino.cc/en/Tutorial/BuiltInExamples/Dimmer
*/
const int ledPin = 9; // the pin that the LED is attached to
void setup()
{
// initialize the serial communication:
Serial.begin(115200);
// initialize the ledPin as an output:
pinMode(ledPin, OUTPUT);
}
void loop()
{
byte brightness;
// check if data has been sent from the computer:
if (Serial.available())
{
// read the most recent byte (which will be from 0 to 255):
brightness = Serial.parseInt();
// set the brightness of the LED:
analogWrite(ledPin, brightness);
}
}
A bit like Firmata?
Hi @mulu
Traditionally, the most common system used in the Arduino world has been the Firmata protocol:
There are a couple of Arduino libraries that implement the protocol on the host side (Arduino board):
Since you want to write your client in C/C++, you also need a Firmata client library in one of those languages. I found two after a quick search:
Firmata seems to be exactly what I am looking for. I got StandardFirmata.io installed on the Arduino. I followed these instructions which I found most helpful of anything out there:
It looks like newer versions of Firmata have more Firmata under File->Examples but based on another video just chose the "StandardFirmata" option (not the ones with some other words after "StandardFirmata").
I now want to send commands from the host to the Arduino using the Firmata protocol. There is a standalone application for on the host to do so. And there are Python libraries for the host computer to do the same. dimitry-ishenko-cpp seems to do what I want but it requires cygwin or some linux environment. grandsmarquis seems to require C++ v14+. etc. According to this link GitHub - firmata/arduino: Firmata firmware for Arduino there are implementations for Python, Perl, Ruby, Javascript, Java, .NEt, PHP, etc. etc. but no C/C++??? Is there some pure WINDOWS client library that can communicate via the Firmata protocol using c/c++ that isn't bleeding edge requiring nuget packages and such?
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.