Help with MCC ADC Board comunication with dll call

273 views
Skip to first unread message

Carlo Piga

unread,
Oct 9, 2014, 12:23:37 PM10/9/14
to juli...@googlegroups.com
I'm tryng to read a buffer from a Measurement Computing ADC with this code. I don't understand if there is errors in my code ( I think yes, but I didn't find them), or there is some incompatibilities with the 64bit dll and julia.
I think the error is in the :cbAInScan call because if I commented it out everything else run OK.

BOARDNUM = 0
BUF_SIZE
= 5000
NUM_SECS
= 10
ADRANGE
= 0
NOCONVERTDATA
= 0x0000
CONVERTDATA
= 0x0008
PRINTALL
= 3
STOPALL
= 2
LOWCHAN
= 0
HIGHCHAN
= 1

#   HGLOBAL EXTCCONV cbWinBufAlloc (long NumPoints);
dll_handle
=dlopen("cbw64.dll")

#     int EXTCCONV cbErrHandling (int ErrReporting, int ErrHandling);
ccall
((:cbErrHandling,"cbw64.dll"), Int16, (Uint16,Uint16), PRINTALL, STOPALL)

Buffer = ccall((:cbWinBufAlloc,"cbw64.dll"), Ptr{Int16}, (Int32,), BUF_SIZE)
Count = BUF_SIZE
Rate = int32(BUF_SIZE / NUM_SECS)

#    int EXTCCONV cbAInScan (int BoardNum, int LowChan, int HighChan, long Count,
#                            long *Rate, int Gain, HGLOBAL MemHandle,
#                            int Options);
ret
= ccall((:cbAInScan,"cbw64.dll"),
     
Int16, (Int16, Int16, Int16, Int32, Ptr{Int32}, Int16, Ptr{Int16}, Int16),
      BOARDNUM
, LOWCHAN, HIGHCHAN, Count, Rate, ADRANGE, Buffer, CONVERTDATA)

#   int EXTCCONV cbWinBufFree (HGLOBAL MemHandle);
retbuffree
= ccall((:cbWinBufFree,"cbw64.dll"), Int16, (Ptr{Int16},), Buffer)

dlclose
(dll_handle)
ret
#retbuffree

I get:

2014-10-09 17:59:29.207 [NotebookApp] Saving notebook at /WINCAI02.C.ipynb

Please submit a bug report with steps to reproduce this fault, and any error mes
sages that follow
(in their entirety). Thanks.
Exception: EXCEPTION_ACCESS_VIOLATION at 0x60837247 -- unknown function (ip: 161
9227207)
unknown
function (ip: 1619227207)
unknown
function (ip: 1620553756)
unknown
function (ip: 1623011214)
anonymous at
no file:0

Please submit a bug report with steps to reproduce this fault, and any error mes
sages that follow
(in their entirety). Thanks.
Exception: EXCEPTION_ACCESS_VIOLATION at 0x60837247 -- unknown function (ip: 161
9227207)
anonymous at
no file:0

Please submit a bug report with steps to reproduce this fault, and any error mes
sages that follow
(in their entirety). Thanks.
Exception: EXCEPTION_ACCESS_VIOLATION at 0x60837247 -- unknown function (ip: 161
9227207)
anonymous at
no file:0

Please submit a bug report with steps to reproduce this fault, and any error mes
sages that follow
(in their entirety). Thanks.
Exception: EXCEPTION_ACCESS_VIOLATION at 0x60837247 -- unknown function (ip: 161
9227207)
anonymous at
no file:0

Please submit a bug report with steps to reproduce this fault, and any error mes
sages that follow
(in their entirety). Thanks.
Exception: EXCEPTION_ACCESS_VIOLATION at 0x60837247 -- unknown function (ip: 161
9227207)
anonymous at
no file:0

Please submit a bug report with steps to reproduce this fault, and any error mes
sages that follow
(in their entirety). Thanks.
Exception: EXCEPTION_ACCESS_VIOLATION at 0x60837247 -- unknown function (ip: 161
9227207)
anonymous at
no file:0
2014-10-09 17:59:47.428 [NotebookApp] KernelRestarter: restarting kernel (1/5)
WARNING
:root:kernel a335deec-1763-4cdc-93da-599f5bce3ff6 restarted
Starting kernel event loops.
2014-10-09 18:01:29.209 [NotebookApp] Saving notebook at /WINCAI02.C.ipynb





Jameson Nash

unread,
Oct 9, 2014, 1:50:30 PM10/9/14
to juli...@googlegroups.com
Unless you are programming for Win16 (Or VB6, which still pretends to target a 16-bit host), an int is either a Cint or Int32 in Julia. Int16 typically coresponds to a short int and is very rare to see in practice (outside of networking hardware and embedded devices)

Patrick O'Leary

unread,
Oct 9, 2014, 2:56:31 PM10/9/14
to juli...@googlegroups.com
Indeed, from the snippet on Google and the link to the manual page (http://www.mccdaq.com/PDFs/Manuals/universal-library-help.pdf#298) it appears that this library is intended for 64-bit systems so nothing like that should be going on.

You should be able to use the "Cint" and "Clong" types where "int" and "long" appear in the function signatures given in the MCC documentation. Note that Because Windows, these actually map to the same data type (Int32), but it is probably best to use the aliases so it is cross-referenced to the documented signature.

Carlo Piga

unread,
Oct 14, 2014, 3:34:06 AM10/14/14
to juli...@googlegroups.com
Thanks. I tried changing the data type but the result is the same.... any suggestion?

This is the modified code:

BOARDNUM = 0
BUF_SIZE
= 5000
NUM_SECS
= 10
ADRANGE
= 0
NOCONVERTDATA
= 0x0000
CONVERTDATA
= 0x0008
PRINTALL
= 3
STOPALL
= 2
LOWCHAN
= 0
HIGHCHAN
= 1


dll_handle
=dlopen("cbw64.dll")


#     int EXTCCONV cbErrHandling (int ErrReporting, int ErrHandling);

ccall
((:cbErrHandling,"cbw64.dll"), Cint, (Cint, Cint), PRINTALL, STOPALL)



#     HGLOBAL EXTCCONV cbWinBufAlloc (long NumPoints);
Buffer = ccall((:cbWinBufAlloc,"cbw64.dll"), Ptr{Cint}, (Clong,), BUF_SIZE)

Count = BUF_SIZE
Rate = int32(BUF_SIZE / NUM_SECS)

#    int EXTCCONV cbAInScan (int BoardNum, int LowChan, int HighChan, long Count,
#                            long *Rate, int Gain, HGLOBAL MemHandle,
#                            int Options);
ret
= ccall((:cbAInScan,"cbw64.dll"),

     
Cint, (Cint, Cint, Cint, Clong, Ptr{Clong}, Cint, Ptr{Cint}, Cint),
      BOARDNUM
, LOWCHAN, HIGHCHAN, Count, Rate, ADRANGE, Buffer, NOCONVERTDATA)


#   int EXTCCONV cbWinBufFree (HGLOBAL MemHandle);

retbuffree
= ccall((:cbWinBufFree,"cbw64.dll"), Cint, (Ptr{Cint},), Buffer)

dlclose
(dll_handle)
#ret
retbuffree

Jameson Nash

unread,
Oct 14, 2014, 3:44:20 AM10/14/14
to juli...@googlegroups.com
The documentation indicates that these should be stdcall, although if you are on 64-bit windows, that is a no-op
#define EXTCCONV	__stdcall

But in this case, you are also passing a number (Rate) to a parameter expecting a pointer. Since pointers are just numbers, Julia doesn't complain, but that won't work so well when C tries to access the pointer. Try passing the address of the value instead `&Rate`

Carlo Piga

unread,
Oct 14, 2014, 5:09:02 AM10/14/14
to juli...@googlegroups.com


Il giorno martedì 14 ottobre 2014 09:44:20 UTC+2, Jameson ha scritto:
The documentation indicates that these should be stdcall, although if you are on 64-bit windows, that is a no-op
#define EXTCCONV	__stdcall
How can I include stdcall library in julia kernel for function called by dll...
The error could be that the cbw64.dll call same stdcall function and julia cannot resolv it...
 
But in this case, you are also passing a number (Rate) to a parameter expecting a pointer. Since pointers are just numbers, Julia doesn't complain, but that won't work so well when C tries to access the pointer. Try passing the address of the value instead `&Rate`
 
How?
I see this example code on julia on line manual:
function gethostname()
 
hostname = Array(Uint8, 128)
 
ccall( (:gethostname, "libc"), Int32,
       
(Ptr{Uint8}, Uint),
       
hostname, length(hostname))
 
return bytestring(convert(Ptr{Uint8}, hostname))
end
It seem that the "value" -> "pointer to type" conversion is done automatically by julia. When I declare a ptr{Cint} as function argument and pass the value with the ccall, julia convert it to the address of the value, I think reading the example...



 

Carlo Piga

unread,
Oct 14, 2014, 5:59:34 AM10/14/14
to juli...@googlegroups.com
This is the final revision with stdcall convention and it don't work....

BOARDNUM = 0
BUF_SIZE
= 5000
NUM_SECS
= 10
ADRANGE
= 0
NOCONVERTDATA
= 0x0000
CONVERTDATA
= 0x0008
PRINTALL
= 3
STOPALL
= 2
LOWCHAN
= 0
HIGHCHAN
= 1

dll_handle
=dlopen("cbw64.dll")
#     define EXTCCONV __stdcall

#     int EXTCCONV cbErrHandling (int ErrReporting, int ErrHandling);

ccall
((:cbErrHandling,"cbw64.dll"), stdcall, Cint, (Cint, Cint), PRINTALL, STOPALL)
###ccall(dlsym(dll_handle, :cbErrHandling), stdcall, Cint, (Cint, Cint), PRINTALL, STOPALL)


#     HGLOBAL EXTCCONV cbWinBufAlloc (long NumPoints);
Buffer = ccall((:cbWinBufAlloc,"cbw64.dll"), stdcall, Ptr{Cint}, (Clong,), BUF_SIZE)
##Buffer = ccall(dlsym(dll_handle,:cbWinBufAlloc), stdcall, Ptr{Cint}, (Clong,), BUF_SIZE)


Count = BUF_SIZE
Rate = int32(BUF_SIZE / NUM_SECS)

#    int EXTCCONV cbAInScan (int BoardNum, int LowChan, int HighChan, long Count,
#                            long *Rate, int Gain, HGLOBAL MemHandle,
#                            int Options);

ret
= ccall((:cbAInScan,"cbw64.dll"), stdcall,
     
Cint, (Cint, Cint, Cint, Clong, Ptr{Clong}, Cint, Ptr{Cint}, Cint),

      BOARDNUM
, LOWCHAN, HIGHCHAN, Count, Rate, ADRANGE, Buffer, NOCONVERTDATA)
###ret = ccall(dlsym(dll_handle,:cbAInScan), stdcall,
###      Cint, (Cint, Cint, Cint, Clong, Ptr{Clong}, Cint, Ptr{Cint}, Cint),
###      BOARDNUM, LOWCHAN, HIGHCHAN, Count, Rate, ADRANGE, Buffer, NOCONVERTDATA)

#   int EXTCCONV cbWinBufFree (HGLOBAL MemHandle);

retbuffree
= ccall((:cbWinBufFree,"cbw64.dll"), stdcall, Cint, (Ptr{Cint},), Buffer)
###retbuffree = ccall(dlsym(dll_handle,:cbWinBufFree), stdcall, Cint, (Ptr{Cint},), Buffer)

dlclose
(dll_handle)

ret
#retbuffree

Patrick O'Leary

unread,
Oct 14, 2014, 8:40:43 AM10/14/14
to juli...@googlegroups.com
On Tuesday, October 14, 2014 4:09:02 AM UTC-5, Carlo Piga wrote:
Il giorno martedì 14 ottobre 2014 09:44:20 UTC+2, Jameson ha scritto:
The documentation indicates that these should be stdcall, although if you are on 64-bit windows, that is a no-op
#define EXTCCONV	__stdcall
How can I include stdcall library in julia kernel for function called by dll...
The error could be that the cbw64.dll call same stdcall function and julia cannot resolv it...
 
But in this case, you are also passing a number (Rate) to a parameter expecting a pointer. Since pointers are just numbers, Julia doesn't complain, but that won't work so well when C tries to access the pointer. Try passing the address of the value instead `&Rate`
 
How?
I see this example code on julia on line manual:
function gethostname()
 
hostname = Array(Uint8, 128)
 
ccall( (:gethostname, "libc"), Int32,
       
(Ptr{Uint8}, Uint),
       
hostname, length(hostname))
 
return bytestring(convert(Ptr{Uint8}, hostname))
end
It seem that the "value" -> "pointer to type" conversion is done automatically by julia. When I declare a ptr{Cint} as function argument and pass the value with the ccall, julia convert it to the address of the value, I think reading the example...

Slightly subtle: the `hostname` argument is an array, which directly maps (in C) to a pointer to the first value of the array, so Julia treats it as a pointer just as C would. The `Rate` argument that Jameson pointed out is scalar, so just as in C, you need to take its address to pass it as a pointer. Julia provides the special C-like `&var` syntax to take an address in the context of a ccall.

Carlo Piga

unread,
Oct 15, 2014, 5:10:08 AM10/15/14
to juli...@googlegroups.com
Ok. Thank you very much.
The "&rate" modification in ccall arguments solved everythings.
Bye.

Patrick O'Leary

unread,
Oct 15, 2014, 8:57:50 AM10/15/14
to juli...@googlegroups.com
On Wednesday, October 15, 2014 4:10:08 AM UTC-5, Carlo Piga wrote:
Ok. Thank you very much.
The "&rate" modification in ccall arguments solved everythings.
Bye.

Great! Glad to hear of your success.

chethan pai

unread,
Jul 12, 2015, 10:03:23 AM7/12/15
to juli...@googlegroups.com
hey , i am learning Data aquastion by MCC DAQ... but i am confused with MemHandle... what is that?? (what is meant by handle for windows to store data??)  why buffer must be allocated?? and can  Cbwinbuffalloc() function work with  cbw.h file ?
Reply all
Reply to author
Forward
0 new messages