Hello,
I've been struggling to read the serial buffer from my program written in C++ for a while now.
I have set my arduino to repetively print a string which I'm trying to recieve in my C++ program and then print to console.
I'm just using the standard example from arduino examples to print a sensor value and number as a string as follows:
/*
Analog input, analog output, serial output
Reads an analog input pin, maps the result to a range from 0 to 255
and uses the result to set the pulsewidth modulation (PWM) of an output pin.
Also prints the results to the serial monitor.
The circuit:
* potentiometer connected to analog pin 0.
Center pin of the potentiometer goes to the analog pin.
side pins of the potentiometer go to +5V and ground
* LED connected from digital pin 9 to ground
created 29 Dec. 2008
by Tom Igoe
This example code is in the public domain.
*/
// These constants won't change. They're used to give names
// to the pins used:
const int analogInPin = 0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// change the analog out value:
analogWrite(analogOutPin, outputValue);
// print the results to the serial monitor:
Serial.print("sensor = " );
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
// wait 1000 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(1000);
}
My C code is as follows:
#include <cstdlib>
#include <iostream>
#include "stdio.h"
#include <windows.h>
using namespace std;
void PrintError( LPCSTR str)
{
LPVOID lpMessageBuffer;
int error = GetLastError();
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), //The user default language
(LPTSTR) &lpMessageBuffer,
0,
NULL
);
printf("%s: (%d) %s\n\n",str,error,lpMessageBuffer);
LocalFree( lpMessageBuffer );
}
const char* ReadSerialLine(){
// open port for I/O
string myString;
HANDLE h = CreateFile("com3",
GENERIC_READ|GENERIC_WRITE,
0,NULL,
OPEN_EXISTING,0,NULL); // my adruino on com4 but this value can be read from argv
if(h == INVALID_HANDLE_VALUE) {
PrintError("E012_Failed to open port");
} else {
// set timeouts
//COMMTIMEOUTS cto = { 1, 100, 1000, 0, 0 };
DCB dcb;
//if(!SetCommTimeouts(h,&cto))
//PrintError("E013_SetCommTimeouts failed");
// set DCB
memset(&dcb,0,sizeof(dcb));
dcb.DCBlength = sizeof(dcb);
dcb.BaudRate = 9600;
dcb.fBinary = 1;
dcb.fOutxCtsFlow = 1;
dcb.fRtsControl = DTR_CONTROL_HANDSHAKE;
dcb.fDtrControl = DTR_CONTROL_DISABLE; // avoid reset of arduino board
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
dcb.ByteSize = 8;
if(!SetCommState(h,&dcb))
PrintError("E014_SetCommState failed");
char buf[50];
buf[19] = '\0';
DWORD read = 0;
ReadFile(h,buf,sizeof(buf)-1,&read,NULL); // read is updated with the number of bytes read
myString = buf;
CloseHandle(h);
}
return myString.c_str();
}
int main(int argc, char** argv) {
for (int x=0;x<1000;x++){
printf(ReadSerialLine());
}
return 0;
}
My problem is that the returned string is always only half there and when the buffer is empty it returns some wierd characters.
I really need someway of checking if the buffer is full and only then attempting to read it. Similar to the available function in arduino processing.
I don't know how to implement this in C++ and I've been searching for it for months now... yes MONTHS!
help would be really appreciated.