calllib issue , change DMA buffer value

조회 수: 2 (최근 30일)
Kev Chen
Kev Chen 2016년 1월 22일
댓글: Kev Chen 2016년 1월 25일
Hi all ,
I want to use calllib to load .dll to control my DAQ device , which is not supported DAQ toolbox.
In C program , I have this code below
unsigned int pattern1[1000];
unsigned int pattern2[1000];
unsigned int buffer[1000];
for(int i=0;i<1000;i++) {
pattern1[i]=i+1;
pattern2[i]=(i+1)*10;
}
memcpy(buffer,pattern1,1000*sizeof(unsigned int));
SetBuffer(buffer);
memcpy(buffer,pattern2,1000*sizeof(unsigned int));
I can use memcpy in C to change the data in buffer , but when I use in Matlab , as below
loadlibrary(DLL,HEADER,'alias',LIB);
pattern1 = uint32(1:1000); % store 1~1000
pattern2 = uint32(1:1000)*10; % store 10~10000
pointer = libpointer('uint32Ptr',pattern1);
calllib(LIB,'SetBuffer',pointer); %void SetBuffer(unsigned int *buffer)
pointer.Value = pattern2;
Sometimes , I see pointer.Value changed to 10~10000 in Matlab, but I still see 1~1000 in my device .
Or sometimes , I see 10~10000 with some strange values like noise in my device.
I know my device and driver work due to C program is okay .
I think the problem is like the efficiency of calllib or lib.pointer in Matlab?
Or I use wrong method to do memcpy-like in Matlab?

채택된 답변

Philip Borghesani
Philip Borghesani 2016년 1월 22일
Try:
pointer.value(:)=pattern2;
Assigning directly to pointer.value is the same as buffer=pattern2 in c which replaces the pointer value. It is also possible that if you do the following it will work correctly:
pointer2=pointer+0; %create a new pointer locked to same address as pointer.
pointer2.value=pattern2;
When a pointer object thinks it is the owner of the data (It was created in MATLAB from data) it assumes it is free to release the data and allocate new space on a full assignment.
  댓글 수: 2
Kev Chen
Kev Chen 2016년 1월 25일
Thanks your answer , but...
The above image is the result what I want from c program.
And the below image is the result from Matlab program.
You will see there are some strange pulses and wrong pattern changed from red circle on image.
And I have tried "pointer.value(:)=pattern2;" but still get this.
I know "pointer.value=pattern2;" will be like pointer to patter2's address not just changing value to pattern2 maybe.
Still deeply appreciate you while no one helps me so far.
Kev Chen
Kev Chen 2016년 1월 25일
Okay .
Your second method
pointer2=pointer+0; %create a new pointer locked to same address as pointer.
pointer2.value=pattern2;
is running perfect now ~
Very appreciate you

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Call C from MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by