I am using the Raspberry PI running Raspbian and Julia to interface with the MCC 172 DSP board. The MCC 172 has a complete c library. I am working on making those calls accessible to Julia using the ccall functionality.
I find that I learn best through examples, and have not yet found an example for initialising a buffer in Julia to pass to the c-program through ccall and have the c-program fill the buffer and return it to Julia. The documentation for the c-function is at
C Library Reference — MCC DAQ HAT Library 1.4.0 documentation near the bottom.
My code snippet is:
# Reads status and multiple samples from an analog input scan.
# documentation https://mccdaq.github.io/daqhats/c.html#analog-input-scan-option-flags
function mcc172_a_in_scan_read(address::Integer, samples_per_channel::UInt32, mcc172_num_channels::Integer, timeout::Float64)
status = Ref{UInt16}(0) # Initialize
buffer_size_samples = samples_per_channel * UInt32(mcc172_num_channels) # Initialize
buffer = Ref{Array{Float64,1}} #(0.0,buffer_size)
samples_read_per_channel = Ref{UInt32}(0) # Initialize
@show(status[], buffer_size_samples[], samples_read_per_channel[], buffer)
success = ccall((:mcc172_a_in_scan_read, "/usr/local/lib/libdaqhats.so"),
Cint, (UInt8, Ref{UInt16}, UInt32, Cdouble, Ref{Array{Float64,1}}, UInt32, Ref{UInt32}),
UInt8(address), status, samples_per_channel, timeout, buffer, buffer_size_samples, samples_read_per_channel)
@show(status[], buffer_size_samples[], samples_read_per_channel[])
return "Fini"
end
The combination printout and error message I get follows, where line 306 actually is the line success = ccall(…
julia> mcc172_a_in_scan_read(UInt8(0),UInt32(1024), 4, 4.0)
status[] = 0x0000
buffer_size_samples[] = 0x00001000
samples_read_per_channel[] = 0x00000000
buffer = Ref{Array{Float64,1}}
ERROR: MethodError: Cannot `convert` an object of type Type{Ref{Array{Float64,1}}} to an object of type Array{Float64,1}
Closest candidates are:
convert(::Type{T}, ::AbstractArray) where T<:Array at array.jl:554
convert(::Type{T}, ::T) where T<:AbstractArray at abstractarray.jl:14
convert(::Type{T}, ::LinearAlgebra.Factorization) where T<:AbstractArray at /home/pi/work/build/usr/share/julia/stdlib/v1.5/LinearAlgebra/src/factorization.jl:55
...
Stacktrace:
[1] Base.RefValue{Array{Float64,1}}(::Type{T} where T) at ./refvalue.jl:8
[2] convert(::Type{Ref{Array{Float64,1}}}, ::Type{T} where T) at ./refpointer.jl:63
[3] cconvert(::Type{T} where T, ::Type{T} where T) at ./essentials.jl:388
[4] mcc172_a_in_scan_read(::UInt8, ::UInt32, ::Int32, ::Float64) at /home/pi/daqhatsJulia/daqhats.jl:306
[5] top-level scope at REPL[160]:1
I think I need to initialize buffer differently or modify the type tuple for the buffer type in the ccall function.
Help is appreciated.