-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
95 lines (78 loc) · 2.17 KB
/
Copy pathmain.cpp
File metadata and controls
95 lines (78 loc) · 2.17 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
//包含boost::python头文件
#include <boost/python.hpp>
#include <iostream>
#include <string>
#include <stdlib.h>
namespace bp = boost::python;
using namespace std;
bp::object py_serial;
struct weight{
float g0;
float g1;
float g2;
float g3;
};
void setEnv()
{
try {
Py_Initialize();
bp::object sys = bp::import("sys");
sys.attr("path").attr("append")(boost::python::str("../py_serial/"));
for (int i = 0; i < bp::len(sys.attr("path")); ++i) {
string str2 = bp::extract<string>(boost::python::getitem(sys.attr("path"), bp::object(i)));
cout << "Have: " << str2 << endl;
}
bp::object os = bp::import("os");
py_serial = bp::import("py_serial");
}
catch (...) {
PyErr_Print();
}
}
bool getWeight(string& line,weight& mweight){
//count the num of ',' to verify
if(line.length()<8)return false;
int countcomma=0;
vector<int> pos;
int ind=0;
for(auto c:line){
if(c==','){
pos.push_back(ind);
countcomma++;
}
ind++;
}
if(countcomma!=4)return false;
if(pos[3]>line.length())return false;
//get weight
string g0=line.substr(0,pos[0]-0);
string g1=line.substr(pos[0]+1,pos[1]-pos[0]-1);
string g2=line.substr(pos[1]+1,pos[2]-pos[1]-1);
string g3=line.substr(pos[2]+1,pos[3]-pos[2]-1);
mweight.g0=atof(g0.c_str());
mweight.g1=atof(g1.c_str());
mweight.g2=atof(g2.c_str());
mweight.g3=atof(g3.c_str());
return true;
}
int main(){
setEnv();
int loopcount=1;
while(1){
loopcount++;
bp::object pline = py_serial.attr("readLinefromSerialPort")();
string line = bp::extract<string>(pline);
cout<<line<<endl;
cout<<"------------------------"<<endl;
weight mweight;
bool suc = getWeight(line,mweight);
if(suc){
cout<<"g0 = "<<mweight.g0<<" g"<<endl;
cout<<"g1 = "<<mweight.g1<<" g"<<endl;
cout<<"g2 = "<<mweight.g2<<" g"<<endl;
cout<<"g3 = "<<mweight.g3<<" g"<<endl;
cout<<"************************"<<endl;
}
}
return 1;
}