Using an API function that has a char pointer as an argument

I am using a small robotic-car that is controlled by writing C/C++ codes under Linux. I need to use a particular function from the library provided by the manufacturer. The relevant API documentation for the function is:

1
2
3
4
5
6
7
8
9
10
BASEBOARD_ERROR_KIND ZMP zrc :: :: :: Baseboard GetRS232Data (char * msg ) 	

RS232 data acquisition.

Argument:
[Out] 	msg 	                Address of the acquired data.

Returns:
BASE_OK 	                RS232 data acquisition success
BASE_BASE_232_GETDATA_ERR 	RS232 data acquisition failure


I have trouble writing the relevant code in the main program that invokes this function and would appreciate some help. Here is a snippet of what I have tried:

1
2
3
4
5
6
7
8
9
10
11
# include "Baseboard.h"

int main () {
    Baseboard  _Baseboard;   // Class name is Baseboard
    char *msg ; 

    zmp::zrc::BASEBOARD_ERROR_KIND dummy = _BaseBoard.GetRS232Data(&msg);   //    return type is an enum called BASEBOARD_ERROR_KIND
    cout << "Message is " << msg << endl ; 

    return 0; 
}


The part where I am uncertain is how to handle the char pointer "msg" in the declaration, function call and referencing. According to the documentation, the char pointer "msg" is the output of the function so I presume that is is somehow dynamically allocated. Am I handling the char pointer properly in the declaration, function call and referencing parts?

Another related question I have is: I am printing out the value of the variable "dummy". I always get 0 for it. Since the variable "dummy" is an enum of type BASEBOARD_ERROR_KIND which can take on two values (first value represents success and the second failure), it is alright to get a integer value of 0 for it if the function call was successful ? (I do not have much experience with using enums so this is a enum-related question on whether we can get an integer value representing the first enum value)

Will appreciate any help.
closed account (3hM2Nwbp)
You can compare the returned enum against success or failure like so, but the documentation is the only thing that can help you with your other question:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "Baseboard.h"
int main()
{
  Baseboard bb;
  /// Does the documentation say where this memory is expected to be allocated?
  /// Additionally, does the documentation say the maximum amount that can be read in one call?
  char* msg /* = new char[8192]*/;

  /// So we don't have to type 'zmp::zrc' everywhere...
  using namespace zmp::zrc;
  if(BASE_BASE_232_GETDATA_ERR == bb.GetRS232Data(msg))
  {
    /// Error reading
  }
  else
  {
    /// Successful reading
    std::cout << msg << std::endl;
  }

}


If you're allocating your own buffer:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <memory>
#include "Baseboard.h"

// Pretend that Baseboard.h defines a macro 'MAX_READ_SIZE'
// You'll have to find this out yourself

int main()
{
  Baseboard bb;
  std::unique_ptr<char[]> buffer(new char[MAX_READ_SIZE]);
  if(zmp::zrc::BASE_BASE_232_GETDATA_ERR == bb.GetRS232Data(buffer.get()))
  {
    /// Error reading
  }
  else
  {
    std::cout << buffer << std::endl;
  }
}
Last edited on
Thanks for your reply. It gives me the idea of how to proceed.
Topic archived. No new replies allowed.