Hi again, and thanks for your reply.
Well, serpro code is C++, and does not implement low-level functions, so should work with any OS capable of C++. There's already a wrapper for glib, I think we can write one for boost, I'll look at it.
The reason asked about Windows compatibility is that I came across a few posts like this one:
suggesting to me that it might not be portable. I did find a great article with simple wrapper classes for the boost library:
http://www.webalice.it/fede.tft/serial_port/serial_port.html
Basically it's a reverse-direction function call, which argument is the return value from the called function:
I can see now that the digitalRead function in your example demonstrates this by returning a value.
Regarding your mockup, I think it might be helpful for less experienced programmers if the documentation were more explicit and if the program were as simple as possible (i.e. maybe avoid the asynchronous function?). I'll take a stab at it, but please correct me where I'm wrong ;). I've interspersed a few comments/questions within the code.
On the Arduino:
#include <SerProArduino.h>
////////////////////////////////////////////////////////////////////////////////
//
// Functions that you would like to make available from the Arduino
// are defined between SERPRO_ARDUINO_BEGIN(); and
// SERPRO_ARDUINO_END(); with the form:
//
// EXPORT_FUNCTION(function_id,function_name);
//
////////////////////////////////////////////////////////////////////////////////
SERPRO_ARDUINO_BEGIN();
EXPORT_FUNCTION(1, pinMode);
EXPORT_FUNCTION(2, digitalWrite);
EXPORT_FUNCTION(3, digitalRead);
SERPRO_ARDUINO_END();
void setup()
{
Serial.begin(115200);
}
void loop()
{
if (Serial.available()>0) {
SerPro::processData(Serial.read());
}
}
On the PC:
#include <SerPro-glib.h>
#define OUTPUT 1
////////////////////////////////////////////////////////////////////////////////
//
// Functions that were exported from the Arduino are defined between
// SERPRO_GLIB_BEGIN(); and SERPRO_GLIB_END(); with the form:
//
// IMPORT_FUNCTION(function_id,
// function_name,
// return type (param1 type, param2 type) );
//
// In the examples below, functions 1 and 2 return void, so SerPro
// will not send a reply. When the functions are called on the PC, they
// they will return immediately.
//
// After calling function 3, the PC will block and wait for the return
// value from the Arduino. Once the Arduino has executed the
// digitalRead function, it will send a response message back to the
// PC with an int16_t argument equal to the function's return value.
//
// Comment: Is there a timeout for the blocking? If the packet
// doesn't show up within the timeout, will SerPro retry?
//
////////////////////////////////////////////////////////////////////////////////
SERPRO_GLIB_BEGIN();
IMPORT_FUNCTION(1, pinMode, void (uint8_t,uint8_t) );
IMPORT_FUNCTION(2, digitalWrite, void (uint8_t,uint8_t) );
IMPORT_FUNCTION(3, digitalRead, int16_t (uint8_t) );
SERPRO_GLIB_END();
const uint8_t ledPin = 13; // LED connected to digital pin 13
uint8_t ledPinState; // state of the LED pin (1 or 0)
// This function is called when protocol connects. It is analogous to the
// setup() function on the Arduino.
void connect()
{
pinMode(ledPin, OUTPUT); // Initialize the digital pin as an output
}
int main(int argc, char **argv)
{
// Comment: I'm assuming that in your initial post, argv[1]
// specified the serial port to connect to? Is the baud rate hard
// coded? I thought it might be easier to see what's going on if
// you showed the initialization explicitly.
// Initialize SerProGLIB with the serial port and baud rate (Note
// that on Windows, serial ports have names like "COM1").
if (SerProGLIB::init("/dev/ttyS0",115200)<0)
return -1;
// Register connect function so that it will be called when
// SerProGLIB connects.
SerProGLIB::onConnect( &connect );
// Comment: I'm not sure what these functions do...
SerProGLIB::start();
SerProGLIB::run();
// This for loop will flip the state of the LED 100 times, waiting 1
// second between each call
for(int i=0; i<100; i++)
{
ledPinState = digitalRead(ledPin); // Read the state of the LED
digitalWrite(ledPin, !val); // Invert the signal
usleep(1000); // Wait one second
}
}
One last suggestion, why not just call your PC library SerProPC? Not all Arduino users will know what GLIB is, and I would imagine there's a way to release it as a statically library so that users would not have to install GLIB?
I'd be happy to help you with this if there's anything that I can do. My C++ is a bit rusty (all of the fancy preprocessor macros in your code make my head hurt), but I could probably help with testing and/or Windows support. Also, I think that Python wrappers might be a nice addition that I could probably hack together.
Thanks,
-Ryan