String handling of data from Arduino serial port

Hi,

I need some help to modify my script handling code. Information and question are in the code comment field.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73


 static UINT arduniorecvthread(LPVOID lpvoid) {
//this is where we receive data from Arduino connected switches, buttons, rotaries etc.

std:string com = SPo1;
std::string slashes = "\\\\.\\";
string comport = slashes + com;

	Serial* SP1 = new Serial(strdup(comport.c_str()));

			char incomingData[256] = "";			
			int dataLength = 255;
			int readResult = 0;
			string var = "";
			string val = "";
			string var2 = "";
			string val2 = "";
			if (SP1->IsConnected())
			{

			Log.timeStamp() << " Arduino Input Board 1 connected " << ENDL << ENDL;
			}
			while (SP1->IsConnected())
			{
  				readResult = SP1->ReadData(incomingData, dataLength);
				incomingData[readResult] = 0;
				string str(incomingData);
				
				// the str above can look like this
				// "2405=0\r\n2404=0\r\n85=1\r\n" or just like "85=1\r\n"
				// it has a var value before every "=" and a val after every "="
				// the var and val can both be 1 digit to 4 digit
				
				// below here is my code for getting the first var and val
				// which I later send as svar and sval in the sendVarData function
				// the code below is working OK for a str with only 1 var/val combination
				// like "85=1\r\n"
				// but how do I change the code so it can handle a long string in str
				// and send all vars and val from the sendVarData
"
				std::string::size_type pos = str.find('=');
				if (pos != std::string::npos)
				{
					var =  str.substr(0, pos);
				}
				
				string val_temp = str.substr(str.find("=") + 1);
				 std::string::size_type pos2 = val_temp.find('\r');
				 if (pos2 != std::string::npos)
				 {
					val = val_temp.substr(0, pos2);
				 }

				
				 int svar = atoi(var.c_str());
				 int sval = atoi(val.c_str());

			

				 if (svar != 0)
				 {
					 IOCP.sendVarData(svar, sval);
					 var = "";
					 val = "";
				 }

				
				 
				Sleep(200);
			}
			return 0;
} 
1.string str(incomingData); if you are running this is c++, then it will not work.
try this "string str;"
2. Make "var" an array and store your data in it.
3. or you can use a loop to run trough your string send the char to your board.

**I think that answer 1 is where most of your troubles are**
Hi,

As stated this C++ code works OK

1
2
3
readResult = SP1->ReadData(incomingData, dataLength);
				incomingData[readResult] = 0;
				string str(incomingData);


I get the str string OK

It is from the below that code line I need some good advise on how I can grab all var/val, even if there is only 1 var/val or many var/val combination in the str string
Last edited on
Topic archived. No new replies allowed.