Hello...I was just trying out a c++ which grabs the Serial data the arduino sends using the library provided here:
http://playground.arduino.cc/Interfacing/CPPWindows
The code involves such a line:
Serial* SP = new Serial("COM3");
where com3 is the port to which arduino is connected. However when i try to replace the line with
Serial* SP = new Serial(smile);
where smile contains a string ( a COM port), it says that the function does not accept string arguments. Is there any workaround so that i can pass a desired string into the function. I am looking to scan and detect automatically the port to which arduino is connected.
Please post code that reproduces this problem, don't just describe it. How is smile declared?
http://snippets-r-us.com/
Serial* SP = new Serial(smile);
The constructor is expecting a pointer to a null-terminated string of characters (i.e. a C-string).
You can use the c_str()
method on your string to convert to C-string.
http://www.cplusplus.com/reference/string/string/c_str/
Here is the code:
#include <stdio.h>
#include <tchar.h>
#include <SerialClass.h> // Library described above
#include
#include <Windows.h>
using namespace std;
// application reads from the specified serial port and reports the collected data
int _tmain(int argc, _TCHAR* argv[])
{
printf("Welcome to the serial test app!\n\n");
//Serial* SP = new Serial("\\.\COM10"); // adjust as needed
Serial* SP = new Serial("COM3"); // adjust as needed
if (SP->IsConnected())
printf("We're connected");
char incomingData[256] = ""; // don't forget to pre-allocate memory
//printf("%s\n",incomingData);
int dataLength = 256;
int readResult = 0;
while (SP->IsConnected())
{
readResult = SP->ReadData(incomingData, dataLength);
printf("Bytes read: (-1 means no data available) %i\n", readResult);
//string test(incomingData);
printf("%s", incomingData);
//test = "";
Sleep(500);
}
return 0;
}
Once I added SerialClass, your code compiled for me with no changes using MSVC 2010.
Pete
sreedevk:
Here is the code:
Serial* SP = new Serial("COM3"); // adjust as needed
No it isn't. That just has a quoted string. The code you mentioned, is what I want:
Serial* SP = new Serial(smile);
Where we can see the definition of smile.
I fixed it. The function accepts a character array and not a string. I worked with the heads up given by Hackscribble...thanks man. I am posting my code here which finds out the port to which the arduino is connected. The arduino constantly sends a partcular string over the serial port.
#include "stdafx.h"
#include <stdio.h>
#include <tchar.h>
#include <SerialClass.h> // Library described above
#include <string>
#include <Windows.h>
#include <iostream>
#include <cstring>
using namespace std;
// application reads from the specified serial port and reports the collected data
int _tmain(int argc, _TCHAR* argv[])
{
printf("Welcome to the serial test app!\n\n");
float number = 0;
for (; number < 10; number++)
{
string xps = "COM";
xps = xps + char(number + '0');
const char * q = xps.c_str();
char p[10];
strcpy_s(p, 10, q);
Serial* SP = new Serial(p); // adjust as needed
char incomingData[256] = ""; // don't forget to pre-allocate memory
//printf("%s\n",incomingData);
int dataLength = 256;
int readResult = 0;
while (SP->IsConnected())
{
Sleep(1000);
readResult = SP->ReadData(incomingData, dataLength);
if (readResult != -1)
{
//printf("Bytes read: (-1 means no data available) %i\n", readResult);
string test(incomingData);
// printf("%s", incomingData);
test = "";
if (strcmp(incomingData, "hello") == 1)
{
cout << "Yeah...it's arduino";
}
else
{
cout << "Nah" << endl;
cout << incomingData;
}
}
else cout << "No data received";
}
}
while (1){}
return 1;
}