Measurement Studio for VC++

cancel
Showing results for 
Search instead for 
Did you mean: 

Problem first time round with DAQmxWriteDigitalLines

Hello,

Please can any one help with the code below?


(start task)

(create channel)

DAQmxWriteDigitalLines(m_taskHandle, 1, 1, -1#, DAQmx_Val_GroupByChannel, m_writeArray(0), m_sampsPerChanWritten, 0)

(stop task)

m_writeArray is 2 bytes long.

When I first run the task I get a buffer overflow error. When I run it a second time it works fine? The DAQmxGetWriteDigitalLinesBytesPerChan always gives 2 bytes per channel (strange? or expected?) - I tired increasing the m_writeArray size but it size exhibits the same behaviour. Anyone know?

Cheers,

Daniel
0 Kudos
Message 1 of 5
(5,222 Views)
Daniel,

You are using the C API for DAQmx. If you are a Measurement Studio for VC++ user, then you may prefer to use the C++ API for DAQmx instead.

Assuming you want to use the C API, I have attached a short program that writes a single sample to a single channel containing a two lines. This program runs fine for me.

-Jeff
0 Kudos
Message 2 of 5
(5,204 Views)
Thanks Jeff,

I tried your code in C and it worked as you said. First time around, no problems. I'm calling these functions with VBA (from within Excel,) and for some reason first time round I still get an error. It's curious, and I don't understand it. The problem is runtime error 6, "overflow." Interestingly enough if I step through the code, and pause for a second or so between making the channel and calling the write lines function I don't have a problem. I really dont understand since the C program works well. I've included the class I used to control the lines below, any ideas?

Best Regards,

Daniel

Option Explicit

Private m_sampsPerChanWritten As Long
Private m_TaskHandle As Long
Private m_writeArray(0 To 3) As Byte
Private m_bytesPerWriteDigitalLines As Long

Private Const c_digitalLineHIGH As Long = 1
Private Const c_digitalLineLOW As Long = 0

Private Const c_terminalAddress As String = "Dev1/port0/line0:1"

Private Sub Class_Initialize()
' Create the DAQmx task.
DAQmxErrChk DAQmxCreateTask("", m_TaskHandle)

' Add a digital output channel to the task.
DAQmxErrChk DAQmxCreateDOChan(m_TaskHandle, c_terminalAddress, "", DAQmx_Val_ChanForAllLines)

'Check the number of bytes per channel
DAQmxErrChk DAQmxGetWriteDigitalLinesBytesPerChan(m_TaskHandle, m_bytesPerWriteDigitalLines)
If m_bytesPerWriteDigitalLines <> 2 Then
MsgBox ("Error. Expecting 2 bytes per channel.")
End If

'Verify the task
'DAQmxErrChk DAQmxTaskControl(m_taskHandle, DAQmx_Val_Task_Verify)

'Reserve and Commit the resources for the task
'DAQmxErrChk DAQmxTaskControl(m_taskHandle, DAQmx_Val_Task_Commit)

'Start the task
DAQmxErrChk DAQmxStartTask(m_TaskHandle)
End Sub

Private Sub Class_Terminate()
'stop the task
DAQmxErrChk DAQmxStopTask(m_TaskHandle)

' terminate the task
DAQmxErrChk DAQmxClearTask(m_TaskHandle)
End Sub


Public Sub Inflate()
m_writeArray(0) = c_digitalLineLOW
m_writeArray(1) = c_digitalLineHIGH

'write the array to the digital lines

DAQmxErrChk DAQmxWriteDigitalLines(m_TaskHandle, 1, 1, -1#, DAQmx_Val_GroupByChannel, m_writeArray(0), m_sampsPerChanWritten, 0)

End Sub

Public Sub Deflate()
m_writeArray(0) = c_digitalLineHIGH
m_writeArray(1) = c_digitalLineLOW

'write the array to the digital lines
DAQmxErrChk DAQmxWriteDigitalLines(m_TaskHandle, 1, 1, -1#, DAQmx_Val_GroupByChannel, m_writeArray(0), m_sampsPerChanWritten, 0)

End Sub

Public Sub StopAir()
m_writeArray(0) = c_digitalLineLOW
m_writeArray(1) = c_digitalLineLOW

'write the array to the digital lines
DAQmxErrChk DAQmxWriteDigitalLines(m_TaskHandle, 1, 1, -1#, DAQmx_Val_GroupByChannel, m_writeArray(0), m_sampsPerChanWritten, 0)

End Sub
0 Kudos
Message 3 of 5
(5,194 Views)
Daniel,
Is there any indication of where the overflow occurs ? Does VBA highlight any variable ?
Also what is the exact order in which you call your functions...
I tried

Class_Initialize
Inflate
Deflate
StopAir
Class_Terminate

in VB6.0 (NOT VBA) without error..
What's the order in your app?
Nandan Dharwadker
Staff Software Engineer
Measurement Studio Hardware Team
0 Kudos
Message 4 of 5
(5,170 Views)
Nandan,

Thanks for your help. I've tried narrowing down the problem on and off for the last week - it works in VB but not VBA. I'm not convinced there should be a difference between the two, it has to be something I'm doing. Anyway, I gave up (since I'm on a deadline) and wrote an ATL COM Wrapper exposing the functionality I need to Excel. This works perfectly, allowing me to use VBA to automate the object, which suits me perfectly.

Thanks again for your help.

Daniel
0 Kudos
Message 5 of 5
(5,141 Views)