Welcome, Guest
Username: Password: Remember me
  • Page:
  • 1

TOPIC: custom c++ program

custom c++ program 5 years 6 months ago #9837

  • daniel
  • daniel's Avatar
  • OFFLINE
  • Junior Boarder
  • Posts: 20
  • Karma: 0
Hello Claes,
I have this code:

#include <iostream>
#include "pwr.h"
#include "rt_gdh.h"
#include <mysql/my_global.h>
#include <mysql/mysql.h>

using namespace std;

class ra_myappl {
pwr_tInt32 *field1;
pwr_tInt32 *field2;
pwr_tInt32 *field3;
pwr_tInt32 *field4;
pwr_tSubid subid;
public:
ra_myappl() {}
void init();
void scan();
void close();
};
void ra_myappl::init()
{
pwr_tStatus sts;
pwr_tOName name_field1 = "PLANT-DB-field1.ActualValue";
pwr_tOName name_field2 = "PLANT-DB-field2.ActualValue";
pwr_tOName name_field3 = "PLANT-DB-field3.ActualValue";
pwr_tOName name_field4 = "PLANT-DB-field4.ActualValue";
// Connect to database
sts = gdh_Init("ra_myappl"); // nu conteaza numele
if (EVEN(sts)) exit(0);
// Connect to object
sts = gdh_RefObjectInfo(name_field1, (void**)&field1, &subid, sizeof(*field1));
sts = gdh_RefObjectInfo(name_field2, (void**)&field2, &subid, sizeof(*field2));
sts = gdh_RefObjectInfo(name_field3, (void**)&field3, &subid, sizeof(*field3));
sts = gdh_RefObjectInfo(name_field4, (void**)&field4, &subid, sizeof(*field4));
if (EVEN(sts)) exit(0);
}
void finish_with_error(MYSQL *con)
{
cout << mysql_error(con) << endl;
mysql_close(con);
exit(0);
}
void ra_myappl::scan()
{
MYSQL *con = mysql_init(NULL);

if (con == NULL) {
cout << mysql_error(con) << endl;
exit(0);
}
if (mysql_real_connect(con, "localhost", "pwrp", "", "ovidiu", 0, NULL, 0) == NULL) {
finish_with_error(con);
}
if (mysql_query(con, "SELECT id, nr_sarja, data_start, data_stop FROM brazda1 ORDER BY id DESC LIMIT 1")) {
finish_with_error(con);
}
MYSQL_RES *result = mysql_store_result(con);
if (result == NULL) {
finish_with_error(con);
}
MYSQL_ROW row = mysql_fetch_row(result);
cout << row[0] << " - ";
cout << row[1] << " - ";
cout << row[2] << " - ";
cout << row[3] << endl;

*field1 = atol(row[0]);
*field2 = atol(row[1]);
*field3 = atol(row[2]);
*field4 = atol(row[3]);

mysql_free_result(result);
mysql_close(con);
exit(0);
}
void ra_myappl::close()
{
gdh_UnrefObjectInfo(subid);
}


int main()
{
ra_myappl myappl;

myappl.init();
myappl.scan();
myappl.close();
}

The code it's working. I am not a C programmer. Please tell me if it's a better way to write that code.
I read 4 field from mysql database and I write those fields into proview. Documentation said that gdh_RefObjectInfoList() it's better than gdh_RefObjectInfo(). I don't know how to convert the code.

Regards,
Daniel
The administrator has disabled public write access.

custom c++ program 5 years 6 months ago #9838

  • claes
  • claes's Avatar
  • OFFLINE
  • Platinum Boarder
  • Posts: 3177
  • Thank you received: 502
  • Karma: 133
Hi Daniel,

The idea whith the open/scan/close functions is that you call open() at startup, then execute scan() cyclically, for example every second, and finally call close() at shutdown. If that is your intention you should store all four subid's and make a call for gdh_UnrefObjectInfo() for each of them. And also open the mysql database in open() and close it in Close(). But if this is a oneshot, it is better to use gdh_SetObjectInfo() instead of setting up direct links with gdh_RefObjectInfo(). The code would be something like
pwr_tStatus sts;
pwr_tOName name_field1 = "PLANT-DB-field1.ActualValue";
pwr_tOName name_field2 = "PLANT-DB-field2.ActualValue";
pwr_tOName name_field3 = "PLANT-DB-field3.ActualValue";
pwr_tOName name_field4 = "PLANT-DB-field4.ActualValue";

pwr_tInt32 field1;
pwr_tInt32 field2;
pwr_tInt32 field3;
pwr_tInt32 field4;

// Connect to database
sts = gdh_Init("ra_myappl"); // nu conteaza numele
if (EVEN(sts)) exit(0);

// Mysql stuff
...

field1 = atol(row[0]);
field2 = atol(row[1]);
field3 = atol(row[2]);
field4 = atol(row[3]);

sts = gdh_SetObjectInfo( name_field1, &field1, sizeof(field1));
if (EVEN(sts)) cout << "gdh_SetObjectInfo error " << sts << endl;

sts = gdh_SetObjectInfo( name_field2, &field2, sizeof(field2));
if (EVEN(sts)) cout << "gdh_SetObjectInfo error " << sts << endl;

sts = gdh_SetObjectInfo( name_field3, &field3, sizeof(field3));
if (EVEN(sts)) cout << "gdh_SetObjectInfo error " << sts << endl;

sts = gdh_SetObjectInfo( name_field4, &field4, sizeof(field4));
if (EVEN(sts)) cout << "gdh_SetObjectInfo error " << sts << endl;

// Mysql free and close
...

/Claes
Last Edit: 5 years 6 months ago by claes.
The administrator has disabled public write access.
The following user(s) said Thank You: daniel

custom c++ program 5 years 6 months ago #9839

  • daniel
  • daniel's Avatar
  • OFFLINE
  • Junior Boarder
  • Posts: 20
  • Karma: 0
My final code. Maybe it will help somebody

First install: libmysqlclient-dev

////////////////////////////////
#include <iostream>
#include "pwr.h"
#include "rt_gdh.h"
#include <mysql/my_global.h>
#include <mysql/mysql.h>

using namespace std;

void finish_with_error(MYSQL *con)
{
cout << mysql_error(con) << endl;
mysql_close(con);
exit(0);
}

int main()
{
pwr_tInt32 field1;
pwr_tInt32 field2;
pwr_tInt32 field3;
pwr_tInt32 field4;
pwr_tSubid subid;
pwr_tStatus sts;
pwr_tOName name_field1 = "PLANT-DB-field1.ActualValue";
pwr_tOName name_field2 = "PLANT-DB-field2.ActualValue";
pwr_tOName name_field3 = "PLANT-DB-field3.ActualValue";
pwr_tOName name_field4 = "PLANT-DB-field4.ActualValue";

// Connect to proview database
sts = gdh_Init("ra_myappl");
if (EVEN(sts)) exit(0);

// MySql
MYSQL *con = mysql_init(NULL);

if (con == NULL) {
cout << mysql_error(con) << endl;
exit(0);
}
if (mysql_real_connect(con, "localhost", "pwrp", "", "dbname", 0, NULL, 0) == NULL) {
finish_with_error(con);
}
if (mysql_query(con, "SELECT field1, field2, field3, field4 FROM table ORDER BY id DESC LIMIT 1")) {
finish_with_error(con);
}
MYSQL_RES *result = mysql_store_result(con);
if (result == NULL) {
finish_with_error(con);
}
MYSQL_ROW row = mysql_fetch_row(result);
cout << row[0] << " - ";
cout << row[1] << " - ";
cout << row[2] << " - ";
cout << row[3] << endl;

field1 = atol(row[0]);
field2 = atol(row[1]);
field3 = atol(row[2]);
field4 = atol(row[3]);

sts = gdh_SetObjectInfo(name_field1, &field1, sizeof(field1));
if (EVEN(sts)) cout << "gdh_SetObjectInfo error " << sts << endl;

sts = gdh_SetObjectInfo(name_field2, &field2, sizeof(field2));
if (EVEN(sts)) cout << "gdh_SetObjectInfo error " << sts << endl;

sts = gdh_SetObjectInfo(name_field3, &field3, sizeof(field3));
if (EVEN(sts)) cout << "gdh_SetObjectInfo error " << sts << endl;

sts = gdh_SetObjectInfo(name_field4, &field4, sizeof(field4));
if (EVEN(sts)) cout << "gdh_SetObjectInfo error " << sts << endl;

mysql_free_result(result);
mysql_close(con);
exit(0);
}
///////////////////////////////////////

Save the code into file: /usr/local/pwrp/projectName/src/appl/ra_myappl.cpp

The program is compiled and linked with:
> sdf projectName
> g++ -g -c ra_myappl.cpp -o $pwrp_obj/ra_myappl.o -I$pwr_inc -DOS_LINUX=1 -DOS=linux -DHW_X86=1 -DHW=x86 -DOS_POSIX `mysql_config --cflags --libs`
> g++ -g -o $pwrp_exe/ra_myappl $pwrp_obj/ra_myappl.o $pwr_obj/pwr_msg_rt.o -L$pwr_lib -lpwr_rt -lpwr_co -lpwr_msg_dummy -lrt -lpthread `mysql_config --cflags --libs`

Then call the program from proview in CArithm with system() command.
char cmd[80];
strcpy(cmd, "ra_myappl");
system(cmd);

Thank you Claes for your help!
Daniel
Last Edit: 5 years 6 months ago by daniel.
The administrator has disabled public write access.
  • Page:
  • 1
Time to create page: 8.617 seconds