I want to do the data exchange between two program by setting the variable value to a specific address.The data can assign into the address, but when I change the value in one of the program the other still not get the value
Program1.cpp set the value to specific address and Program2.cpp to get the data from that address, both include a header file "DataExchange.h"
Program1.cpp :
#include <iostream>
#include <windows.h>
#include "DataExchange.h"
using namespace std;
int main(int argc, char** argv) {
double val2 = 0;
double spd = 1;
double* ptr2 = (double*)speed_address;
cout << "Program1" << endl;
while(1){
DXSetvalue (Speed, spd);
val2 = DXGetvalue (Speed);
cout << "[speed] = " << spd++ << " [get speed] = " << val2 << " [address] = " << ptr2 << "\r" ;
Sleep(2000); // 2sec
}
return 0;
}
Program2.cpp:
#include <iostream>
#include <windows.h>
#include "DataExchange.h"
using namespace std;
int main(int argc, char** argv) {
double t = 0;
double val = 0;
int* ptr = (int*)speed_address;
cout << "Program1 Node" <<endl;
init();
while(1){
t += 2;
val = DXGetvalue (Speed);
cout << "(" << t << "sec) " <<"[Speed] = " << val << " [blackboard address] = " << ptr << "\r";
Sleep(2000);
}
}
DataExchange.h:
#include "stdio.h"
// The macro of the value ID
#define Speed 1
// Define of the address of the value
//#define speed_address 0x4a8048
//#define speed_address 0x00CFFB3C
#define speed_address 0x7ffef8
// Declaration
void setaddressValue (double* address, double d);
double getaddressValue (int name);
double* addressID (int name);
void DXSetvalue (int name, double d);
double DXGetvalue (int name);
void init();
// Definition
void setaddressValue (double* address, double d){
double* p = (double*)address;
*p = d;
//printf("p = 0x%x *p = %lf\n", p, *p);
}
double getaddressValue (int name){
double* pGet = addressID(name);
double value = *pGet;
//printf("pGet = 0x%x *pGet = %lf\n", pGet, *pGet);
return value;
}
double* addressID (int name){
switch (name){
case 1:
return (double*)speed_address;
default:
//cout << "not set the address" << endl;
printf("not set the address!!!\n");
}
}
/*program data*/
void DXSetvalue (int name, double d){
setaddressValue(addressID(name), d);
}
double DXGetvalue (int name){
return getaddressValue (name);
}
void init(){
double* p = (double*)speed_address;
*p = 0.5;
}