I downloaded the class for serial port communications from : Arduino Playground - CPPWindows (Serial.cc and Serial.hh)
I interfaced this class using Code::Block and mingw32 under windows 7 ( I don't like using MS VC++).
I need to communicate with Arduino by a DLL, so I 'm using a void function instead of a main program (but this is not important). I write what I send and receive in a service file just for debugging. This system works fine when I read and I' m able to get data from all the analog inputs of Arduino. However, it doesn't work when I try to write something, like a character "1" in the port (I need to send some inputs to Arduino in order to get a full controll of the board).In fact if I send something to Arduino it doesn't come back, as it happens with the standard Arduino Serial terminal.
I have read in some posts that this may be caused by:
- delay in the interface between PC and board;
- some different settings in the serial port (apparently, not settable from the serial class I use).
I'm pretty confused. Does someone have some experiences ? Suggestion on alternative ways?
Regards
Marco
Enclosed code
//***************************************************************
using namespace std;
#include
#include
#include <string.h>
#include "SerialClass.h"
extern "C"{
void Serial_main(int* lport){
// variables assignement
char mc[]="Port Closed";
char *pmc;pmc=mc;
char sc[2]="1";
char psc;psc=sc;
unsigned int nbChar=25;
unsigned int nsChar=2;
int ier=0;
int iew=0;
char buffer [nbChar];
char sbuffer [nsChar];
char PortName;
int portid=0;
char NomePorta[5]="COM3";
// open the port for serial communication
PortName=NomePorta;
Serial porta(PortName);
portid=porta.IsConnected();
lport[0]=portid;
// send a command to Arduino
strcpy(sbuffer,psc);
iew=porta.WriteData(sbuffer,nsChar);
lport[0]=iew;
// read a value from Analog Device
if(portid)
{ for (unsigned int i=0;i<nbChar;i++)
{buffer=' ';}
- ier=porta.ReadData(buffer,nbChar);*
- }else{*
- strcpy(buffer,pmc);*
- }*
// write in a file what I sent and recived - ofstream myfile;*
- myfile.open ("example.txt");*
- myfile << sbuffer;*
- myfile << buffer;*
- myfile.close();*
// destroy object - porta.~Serial();*
}
} // extern "C"
//***************************************************************