Digital I/O

Showing results for 
Search instead for 
Did you mean: 

Reading data in a loop - after 256 samples input operation always returns 0 (zero)

Hi All,

I'm having problems reading values using USB-6501 device. This is what I do (the following is a pseudo code; in reality I'm using C#):

1.1 Create task A (used to write data/output)
1.2 Create output channel associated with task A (using Dev1/port0/lines0:1, one channel for all lines)
1.3 Create writer object based on the task A's stream
1.4 Start task A.
2.1 Create task B (used to read data/input)
Create input channel associated with task B (using Dev1/port1/lines0:1, one channel for all lines)
2.3 Create reader object based on the task B's stream
2.4 Start task B.

Now, what I do is a simple loop like this:

3.1 for (int data=0; data < 255; data++)
3.2 {
3.3    int dataWritten = data & 0x3;
// bit mask because I'm using only two lines for input/output
3.4    writer.Write(dataWritten);
3.5    int dataRead = reader.Read();
3.6    Assert.AreEqual(dataWritten, dataRead); // checks if they are equal
3.7 }

4.1 Stop task A
4.2 Stop task B
4.3 Dispose all created objects.

The problem is all of this works when executed for the first time. When I try to run it again the reader object (created in step 2.3) always returns 0 (line 3.5). I have noticed that to make it work again I need to reset the device (using MAX).

I guess I must be doing sth wrong.
Thank you in advance. 🙂

0 Kudos
Message 1 of 3
UPDATE: I've noticed one more thing. It looks like the application works or not depending on the last written value. When the last written value is 0x3 (all output lines set to '1'/+5V) everything works fine - I can execute the app as many times as I want. However when the last written value is different (i.e. 0x0, 0x1 or 0x2) the next time I run the app the reader object always returns zero.

Any ideas on what may be wrong? Any help greatly appreciated!

0 Kudos
Message 2 of 3
It's a bit odd to replay to my own posts, but I believe some people read this forum so maybe it will help someone.

Here is the constructor for my DigitalInputPort class:

      public DigitalInputPort(string lines) : base(lines)
         // Create an digital input channel
         m_PortChannel = PortTask.DOChannels.CreateChannel(lines, String.Empty,
         // Create channel writer object which will be used to read data from the port
         m_PortReader = new DigitalSingleChannelReader(PortTask.Stream);

         PortTask.Start(); // PortTask is a Task object created in the base class

Can you see the bug? 🙂



Yes, it is the following line:

    m_PortChannel = PortTask.DOChannels.CreateChannel(lines, String.Empty,

As I'm interested in reading data (after all the class name - DigitalInputPort - wasn't chosen by a mistake) I should have used Digital Input Channel, hence the corrected code looks like this:

    m_PortChannel = PortTask.DIChannels.CreateChannel(lines, String.Empty,

The question is why the original code worked at all? It looks like you can read from task's stream even if there is only an output channel associated with this task. Seems to be a bit strange to me... Anyway I'm happy I have solved the problem.

Greetings from Poland/Europe. 🙂

Message Edited by Bartek G. on 03-01-2006 03:48 AM

0 Kudos
Message 3 of 3