I am trying to transfer a string from a C routine to an Ada application. Instead of reading from a NI device I hard coded a string in the C file routine. The string does transfer across but there are other non-characters with it. The string is HELLO.
BUFFER = UH~I?H~C?^P?E?HELL?E?O?
The C routine is:
#include "gpib_intf.h"
#define MAX_SIZE 350
static ViStatus status, StatusSession;
static ViSession inst, defaultRM;
static char stringinput[256];
static ViUInt32 rcount, retCount;
static ViUInt32 BytesToWrite;
static unsigned char buffer[MAX_SIZE];
void setupDefaultRM()
{
status = viOpenDefaultRM (&defaultRM);
if (status < VI_SUCCESS)
{
printf("Could not open a session to the VISA Resource Manager!\n");
exit (EXIT_FAILURE);
}
status = viOpen (defaultRM, "GPIB0::1::INSTR", VI_NULL, VI_NULL, &inst);
if (status < VI_SUCCESS)
{
printf("Could not open a session to the GPIB Device!\n");
exit (EXIT_FAILURE);
}
}
void writeVI(char* stringinput)
{
BytesToWrite = (ViUInt32)strlen(stringinput);
status = viWrite (inst, (ViBuf)stringinput, BytesToWrite, &rcount);
}
void readVI()
{
/*memset(buffer, 0, sizeof(buffer));*/
const unsigned char version[5] = "HELLO";
printf("\n");
printf("version = %s\n", version);
/*
status = viRead (inst, buffer, MAX_SIZE, &retCount);
if (status < VI_SUCCESS)
{
printf ("Error reading a response from the device.\n");
}
*/
/*return version;*/
}
The Ada code reading it is:
-------------------------------------------------------------------------
-- READVI
--
-- read from GPIB device
--
function READGPIB return STRING is
function READVI return STRING is
version : char_array(size_t);
pragma Import (C, version, "readVI");
begin
--return " 0";
return To_Ada(version, Trim_Nul => True);
end;
begin
--Text_IO.Put_Line(READVI);
return READVI;
end READGPIB;
readVIas achar_array. No wonder you get garbage. - Jeffrey R. Carter