Access lib (dll) from c example.

Just starting out? Need help? Post your questions and find answers here.
vwidmer
Enthusiast
Enthusiast
Posts: 282
Joined: Mon Jan 20, 2014 6:32 pm

Access lib (dll) from c example.

Post by vwidmer »

I am trying to access from PB this library the same way I see it in the C I dont know if I am doing it correct maybe some one can help me out please.

Here is my PB code:

Code: Select all

Structure res
  volts1res.f
EndStructure

Dim resStruc.res(1)

ImportC "/usr/lib/libpmcomm.so"
  PMOpenConnectionSerial(*conn)
  PMReadDisplayFormatted(*conn, *res, *resStruc)
EndImport

conn = PMOpenConnectionSerial("/dev/ttyUSBPM")

PMReadDisplayFormatted(conn, PM_D1, resStruc(0)\volts1res)

Debug resStruc(0)\volts1res
And here is the C code:

Code: Select all

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <stack>
#include <time.h>
#include "libpmcomm.h"

using namespace std;

int displaysLoopMain(PMConnection *conn);

int main() {
  /* For serial port (Unix style) */
  struct PMConnection *conn = PMOpenConnectionSerial("/dev/ttyUSBPM");

  if(conn == NULL) {
    cerr << "connection failed" << endl;
    return 1;
  }

  return displaysLoopMain(conn);
}

/* This function demonstrates how to read display and program data. It displays
   a set of different types of data in a loop. */
int displaysLoopMain(PMConnection *conn) {
  while(1) {
    struct PMDisplayValue volts1res, amps1res;

    /* Read volts 1 (D1) */
    int32_t error = PMReadDisplayFormatted(conn, PM_D1, &volts1res);
    if(error) {
      cerr << "read error" << endl;
      return 1;
    }
 
    /* Read amps 1 (D7) */
    error = PMReadDisplayFormatted(conn, PM_D7, &amps1res);
    if(error) {
      cerr << "read error" << endl;
      return 1;
    }
    
    /* Print results.  Note that the precisions of the amps channels
    may not be correct for your system.  When the large (500A/500mV) shunt
    is used, one digit is available, and with the small (100A/100mV) shunt
    two digits are available.  This can be determined automatically using
    PMReadProgramFormatted() to look at the shunt type. */
    cout << "Volts: " << fixed << setprecision(1) << (volts1res.val / 10.0)
         << ", Amps 1: " << setprecision(1) << (amps1res.val / 100.0)  << endl;  

    /* Reading a program value */
    union PMProgramData capacity;
    error = PMReadProgramFormatted(conn, PM_P14, &capacity);
    if(error) {
      cerr << "read error" << endl;
      return 1;
    }

    cout << "Capacity: " << (int) capacity.batCapacity << endl;
    
  }
  
  return 0;
}
Thanks for any help / advise..
WARNING: I dont know what I am doing! I just put stuff here and there and sometimes like magic it works. So please improve on my code and post your changes so I can learn more. TIA