arduino & visual studio C++ 2005

Can somebody translate this in VS C++ 2005 ??

void serialEvent (Serial myPort) {
// get the ASCII string:
String inString = myPort.readStringUntil(’\n’);

// if it’s not empty:
if (inString != null) {
// trim off any whitespace:
inString = trim(inString);

// convert to an array of ints:
int incomingValues = int(split(inString, “,”));

// print out the values
for (int i = 0; i < incomingValues.length; i++) {
print("Sensor " + i + ": " + incomingValues*);*

  • println();*
  • }*
    How to convert Comma-Delimited string (various lengths) to integer Array in Visual Studio C++ 2005 ??
    For example:
    0,0,0,0\n (read from arduino)
    array[0]=0
    array[1]=0
    array[2]=0
    array[3]=0
    128,0,45,0\n
    array[0]=128
    array[1]=0
    array[2]=45
    array[3]=0

Hey,

So to clear things up: VS C++ 2005, or Visual Studio 2005, is just an IDE: An Integrated Development Environment. It is simply a way to write code in a nice little program rather than write everything in text files and use command line instructions to get things compiled. I think what you are asking is to convert some Arduino source to the C++ standard, so you can run it through a standard C++ compiler?

This boils down you having to change the method of reading and writing from the Arduino serial library to Standard C++ I/O. You can find documentation for this by googling "iostream" or simply go to http://www.cplusplus.com/reference/iostream/iostream/. My favorite book for learning C++ can be found free on the web at: http://newdata.box.sk/bx/c/

To read into a string, simply do: string myString; cin >> myString; // This stops reading when we hit a space or a new line To read a full line, look into "readline(...)"

As for the second question, here is some simple pseudo code: 1. Get string 2. Save the starting position 3. Seek until you find a comma or end of string 4. If not at end of string 5. Replace that comma with a '\0' or terminator 6. Pass the start of the string to a function called "atoi(...)" (Google it) 7. Repeat at 2, with the starting position as the (end position + 1) or quit if the string is done being parsed

Ok, here is what I have now and it works pretty well:

#define STRICT
#include <tchar.h>
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include “Serial.h”

void main ()
{

CSerial serial;
serial.Open(_T(“COM5”),0,0,true);
serial.Setup(CSerial::EBaud9600,CSerial::EData8,CSerial::EParNone,CSerial::EStop1);
serial.SetupReadTimeouts(CSerial::EReadTimeoutNonblocking);

// Keep reading data, until an EOF (CTRL-Z) has been received
bool fContinue = true;

DWORD dwBytesRead = 0;
do{
do{
char szBuffer[1];
// Read data from the COM-port
serial.Read(szBuffer,1,&dwBytesRead);
if (dwBytesRead > 0)
{
// Finalize the data, so it is a valid string
szBuffer[dwBytesRead] = ‘\0’;
// Display the data
printf("%s", szBuffer);
}
}
while (dwBytesRead > 0);
}
while (fContinue);

// Close the port again
serial.Close();

}

ARDUINO SOURCE:

int val = 0;
int inputPin0 = 0;
int inputPin1 = 1;
int inputPin2 = 2;
int inputPin3 = 3;
void setup () {
Serial.begin(9600);
}

void loop() {
val = analogRead(inputPin0)/4;
Serial.print(val, DEC);
Serial.print(",");
val = analogRead(inputPin1)/4;
Serial.print(val, DEC);
Serial.print(",");
val = analogRead(inputPin2)/4;
Serial.print(val, DEC);
Serial.print(",");
val = analogRead(inputPin3)/4;
Serial.print(val, DEC);
//Serial.println();
delay(5000);

I have downloaded sln Serial from codeproject! My problem is only to get integer array from szBuffer using Comma-Delimiter and atoi function.

I read 4 integer values from arduino separated with commas in szBuffer…

Any pointers or help to get me on the road would be deeply appreciated!