From: Gerhard Wolf on
Hi,

I have a function (IOExchange) from a Hardware Vendor (it means not
changeable).
In all the code examples, parameter 6 is a array with fixed size e.g.
float[10]. It must be 'a pointer to the users data puffer'.

Now i want to have this parameter 6 changeable at runtime so i can't
define a array with fixed size.

My fist attempt to replace the array was a:
std::list<unsigned char> input;
This does not work input gets no values.

How do I solve this Problem.

------Thread-execute----------------------------------
....
....
while (!this->Terminated){
input.clear();
Form1->Memo1->Clear();
sRet=IOExchange(0,0,0,0,
Form1->Edit1->Text.ToInt(),
Form1->Edit2->Text.ToInt(), // Receive Size
&input,
0L);
}
for (std::list<unsigned char>::iterator it=input.begin();
it != input.end(); it++) {
Form1->Memo1->Lines->Add(*it);
}
From: Gerhard Wolf on
Gerhard Wolf schrieb:
> Hi,
>
> I have a function (IOExchange) from a Hardware Vendor (it means not
> changeable).
> In all the code examples, parameter 6 is a array with fixed size e.g.
> float[10]. It must be 'a pointer to the users data puffer'.
>
> Now i want to have this parameter 6 changeable at runtime so i can't
> define a array with fixed size.
>
> My fist attempt to replace the array was a:
> std::list<unsigned char> input;
> This does not work input gets no values.
>
> How do I solve this Problem.
>
> ------Thread-execute----------------------------------
> ...
> ...
> while (!this->Terminated){
> input.clear();
> Form1->Memo1->Clear();
> sRet=IOExchange(0,0,0,0,
> Form1->Edit1->Text.ToInt(),
> Form1->Edit2->Text.ToInt(), // Receive Size
> &input,
> 0L);
> }
> for (std::list<unsigned char>::iterator it=input.begin();
> it != input.end(); it++) {
> Form1->Memo1->Lines->Add(*it);
> }
wrong it's parameter 7
sRet=DevExchangeIO(0,0,0,0,
Form1->Edit1->Text.ToInt(),
Form1->Edit2->Text.ToInt(), // Receive Size
&input, //<- variable size here!
0L);
From: Francis Glassborow on
Gerhard Wolf wrote:
> Hi,
>
> I have a function (IOExchange) from a Hardware Vendor (it means not
> changeable).
> In all the code examples, parameter 6 is a array with fixed size e.g.
> float[10]. It must be 'a pointer to the users data puffer'.
>
> Now i want to have this parameter 6 changeable at runtime so i can't
> define a array with fixed size.
>
> My fist attempt to replace the array was a:
> std::list<unsigned char> input;
> This does not work input gets no values.
>
> How do I solve this Problem.
by using a vector<float> whose data is guaranteed to be a contiguous
array. Pass the address of the first element to the function. As it is
fixed size, you could also use a boost/tr1 array.
From: Daniel T. on
Gerhard Wolf <quisquiliae(a)gmx.de> wrote:

> I have a function (IOExchange) from a Hardware Vendor (it means not
> changeable).
> In all the code examples, parameter 6 is a array with fixed size e.g.
> float[10]. It must be 'a pointer to the users data puffer'.
>
> Now i want to have this parameter 6 changeable at runtime so i can't
> define a array with fixed size.

I'm not sure what you mean here. The vender requires a block of 10
floats, so there isn't much you can do about changing that. Unless you
want to make a block larger than 10 and pass a different piece each
time...

> My fist attempt to replace the array was a:
> std::list<unsigned char> input;
> This does not work input gets no values.

As well it shouldn't.

> How do I solve this Problem.
>
> ------Thread-execute----------------------------------
> ...
> ...
> while (!this->Terminated){
> input.clear();
> Form1->Memo1->Clear();
> sRet=IOExchange(0,0,0,0,
> Form1->Edit1->Text.ToInt(),
> Form1->Edit2->Text.ToInt(), // Receive Size
> &input,
> 0L);
> }
> for (std::list<unsigned char>::iterator it=input.begin();
> it != input.end(); it++) {
> Form1->Memo1->Lines->Add(*it);
> }

It looks like the loop above is trying to grab 10 floats, then copy them
into Form1->Memo1->Lines, then grab the next 10 and copy them and so on,
until Terminated is true. Is that right? Probably not since Memo1 is
cleared each time through the loop. :-( What type is
Form1->Memo1->Lines? It seems as though it isn't one of the standard
types... Also, does the function return the number of floats actually
inserted into the buffer in some way? Maybe through 'sRet' or by
changing the value of the receive size?

The answers to the questions above make a difference in how I would
probably do the job. However on first blush, I'm thinking something like
this:

while ( !Terminated ) {
float input[10];
// pass &input into the function
std::copy( input, input + 10, adder( Form1->Memo1->Lines ) );
...

To do the above, you will need to create the "adder" class. Something
like:

template < typename Cont >
class adder_iterator :
public iterator<std::output_iterator_tag, void, void, void, void >
{
Cont& container;
public:
explicit adder_iterator( Cont& x ): container( x ) { }

adder_iterator& operator=( const typename Cont::value_type& val )
// the above assumes that the type of Form1->Memo1->Lines
// actually has a 'value_type'. You may have to change the
// parameter to conform to whatever method 'Lines' uses to
// divulge the type it contains.
{
container.Add( val );
return *this;
}
adder_iterator& operator++() { return *this; }
adder_iterator& operator++(int) { return *this; }
};

};

template < typename Cont > adder_iterator<Cont> adder( Cont& c ) {
return adder_iterator<Cont>( c );
}

Just a thought. If you find yourself adding things inside loops a lot,
the above might be quite useful.
From: Gerhard Wolf on
Daniel T. schrieb:
> Gerhard Wolf <quisquiliae(a)gmx.de> wrote:
>
>> I have a function (IOExchange) from a Hardware Vendor (it means not
>> changeable).
>> In all the code examples, parameter 6 is a array with fixed size e.g.
>> float[10]. It must be 'a pointer to the users data puffer'.
>>
>> Now i want to have this parameter 6 changeable at runtime so i can't
>> define a array with fixed size.
>
> I'm not sure what you mean here. The vender requires a block of 10
> floats, so there isn't much you can do about changing that. Unless you
> want to make a block larger than 10 and pass a different piece each
> time...
>
>> My fist attempt to replace the array was a:
>> std::list<unsigned char> input;
>> This does not work input gets no values.
>
> As well it shouldn't.
>
>> How do I solve this Problem.
>>
>> ------Thread-execute----------------------------------
>> ...
>> ...
>> while (!this->Terminated){
>> input.clear();
>> Form1->Memo1->Clear();
>> sRet=IOExchange(0,0,0,0,
>> Form1->Edit1->Text.ToInt(),
>> Form1->Edit2->Text.ToInt(), // Receive Size
>> &input,
>> 0L);
>> }
>> for (std::list<unsigned char>::iterator it=input.begin();
>> it != input.end(); it++) {
>> Form1->Memo1->Lines->Add(*it);
>> }
>
> It looks like the loop above is trying to grab 10 floats, then copy them
> into Form1->Memo1->Lines, then grab the next 10 and copy them and so on,
> until Terminated is true. Is that right? Probably not since Memo1 is
> cleared each time through the loop. :-( What type is
> Form1->Memo1->Lines? It seems as though it isn't one of the standard
> types... Also, does the function return the number of floats actually
> inserted into the buffer in some way? Maybe through 'sRet' or by
> changing the value of the receive size?
>
> The answers to the questions above make a difference in how I would
> probably do the job. However on first blush, I'm thinking something like
> this:
>
> while ( !Terminated ) {
> float input[10];
> // pass &input into the function
> std::copy( input, input + 10, adder( Form1->Memo1->Lines ) );
> ...
>
> To do the above, you will need to create the "adder" class. Something
> like:
>
> template < typename Cont >
> class adder_iterator :
> public iterator<std::output_iterator_tag, void, void, void, void >
> {
> Cont& container;
> public:
> explicit adder_iterator( Cont& x ): container( x ) { }
>
> adder_iterator& operator=( const typename Cont::value_type& val )
> // the above assumes that the type of Form1->Memo1->Lines
> // actually has a 'value_type'. You may have to change the
> // parameter to conform to whatever method 'Lines' uses to
> // divulge the type it contains.
> {
> container.Add( val );
> return *this;
> }
> adder_iterator& operator++() { return *this; }
> adder_iterator& operator++(int) { return *this; }
> };
>
> };
>
> template < typename Cont > adder_iterator<Cont> adder( Cont& c ) {
> return adder_iterator<Cont>( c );
> }
>
> Just a thought. If you find yourself adding things inside loops a lot,
> the above might be quite useful.
Thanks with F.Glassborow's answer i found the korrekt solution for me.
The array size that the IOExchange function is returning is depending
from the 5th parameter (Receive Size). Form1->Edit2->Text.ToInt() is a
GUI Edit control. Memo1 is a GUI Memo control and 'this' represents the
thread Execute() so it is o.k. here. Here my working solution:
....
....
while (!this->Terminated){
offset = Form1->Edit1->Text.ToInt();
size = Form1->Edit2->Text.ToInt();
Form1->Memo1->Clear();
input.clear();
for (int i=0;i<=size ;i++ ) { // vector with [size] Elements
input.push_back(0);
}

sRet=DevExchangeIO(0,0,0,0,
offset,
size, // Receive Size
&input[0], //Pointer to the user read data buffer [size]Elem.
0L);

for (std::vector<unsigned char>::iterator it=input.begin();
it != input.end(); it++) {
Form1->Memo1->Lines->Add(*it);
}
}
....
....