Reading EIO bits using eReadNames | LabJack
 

Reading EIO bits using eReadNames

5 posts / 0 new
Last post
Jay
jay_coder's picture
Reading EIO bits using eReadNames

I have been successful in writing 1 to EIO bits but when I try to read the EIO bits (after writing once) then the EIO bit toggles.

I have the following code for programming 1 to EIO4 and then reading the state of EIO0 to EIO4. It reads 1 for OFF bits (and turns off the switch after reading 1) and 0 for ON. 

Reading the EIO bits another time gives all 1 (which means that the state of the EIO4 bit has toggled which makes sense because the switch was turned OFF) but I don't understand why would eReadNames reset the bit after reading the state of the bit. 

 

Thanks in advance. Please let me know. 

Jay

=== MATLAB CODE===

%
% Demonstrates how to use the eWriteNames (LJM_eWriteNames) function using .NET.
%
% [email protected]
%

clc  % Clear the MATLAB command window
clear  % Clear the MATLAB variables

% Make the LJM .NET assembly visible in MATLAB
ljmAsm = NET.addAssembly('LabJack.LJM');

% Creating an object to nested class LabJack.LJM.CONSTANTS
t = ljmAsm.AssemblyHandle.GetType('LabJack.LJM+CONSTANTS');
LJM_CONSTANTS = System.Activator.CreateInstance(t);

handle = 0;

try
    % Open first found LabJack

    % Any device, Any connection, Any identifier
    [ljmError, handle] = LabJack.LJM.OpenS('ANY', 'ANY', 'ANY', handle)

    % T7 device, Any connection, Any identifier
    % [ljmError, handle] = LabJack.LJM.OpenS('T7', 'ANY', 'ANY', handle);

    % T4 device, Any connection, Any identifier
    % [ljmError, handle] = LabJack.LJM.OpenS('T4', 'ANY', 'ANY', handle);

    % Any device, Any connection, Any identifier
    % [ljmError, handle] = LabJack.LJM.Open(LJM_CONSTANTS.dtANY, ...
    %     LJM_CONSTANTS.ctANY, 'ANY', handle);

    showDeviceInfo(handle);

    % Setup and call eWriteNames to write values.
    numFrames = 2;
    aNames = NET.createArray('System.String', numFrames)
    aNames(1) = 'EIO4'
    aNames(2) = 'TEST_UINT16'
    aValues = NET.createArray('System.Double', numFrames)
    aValues(1) = 2.5  % 2.5 V
    aValues(2) = 12345  % 12345
    LabJack.LJM.eWriteNames(handle, numFrames, aNames, aValues, 0)

    disp('eWriteNames:')
    for i=1:numFrames,
        disp(['  Name: ' char(aNames(i)) ', Value: ' num2str(aValues(i))])
    end
catch e
    showErrorMessage(e)
    LabJack.LJM.CloseAll()
    return
end

    numFrames = 5;
    aNames = NET.createArray('System.String', numFrames)
    aNames(1) = 'EIO0'
    aNames(2) = 'EIO1';
    aNames(3) = 'EIO2'
    aNames(4) = 'EIO3';
    aNames(5) = 'EIO4'
    aValues = NET.createArray('System.Double', numFrames)
    ErrorAddress = 0;
LabJack.LJM.eReadNames(handle, numFrames, aNames, aValues,ErrorAddress)


    disp('eReadNames results:')
    for i=1:numFrames,
        disp(['  Name: ' char(aNames(i)) ', Value: ' num2str(aValues(i))])
    end
    
a=0;
try
    % Close handle
    LabJack.LJM.Close(handle);
catch e
    showErrorMessage(e)
end
 

LabJack Support
labjack support's picture
The normal FIO/EIO/CIO/MIO

The normal FIO/EIO/CIO/MIO/DIO registers will always set the direction and state, so when you read EIO4 you are setting the direction to input, then reading the state. If you are not pulling the IO low, the state of your input should be a logic high since there is a weak 100kohm pull-up resistor to 3.3V.

In order to read or write the state or direction without modifying anything you will need to use our bitmask registers:

https://labjack.com/support/datasheets/t-series/digital-io#bitmask

Jay
jay_coder's picture
Ahh, I understood that now.

Ahh, I understood that now. Thanks. 

Should I write, 
LabJack.LJM.DIO_STATE(handle, 1, 'EIO4', aValues,0)?

Sorry, I am new to labjack. It would be great if you could have any inputs. Please let me know. Thank you so much! 

LabJack Support
labjack support's picture
Your call is not valid, DIO

Your call is not valid, DIO_STATE is a register name rather than a function like eReadNames. DIO_STATE could go in the aNames array. However, I think you can get rid of the eReadNames call altogether and just read DIO_STATE with eReadName. This is because DIO_STATE contains the state information for all of the IO including FIO/EIO/CIO/MIO. Since you are only interested in EIO ports, you could actually read EIO_STATE to only read the EIO state information. Your call could look like the following:

[ljmError, value] = LabJack.LJM.eReadName(handle, 'EIO_STATE', 0)

"value" in the above would be a binary encoded value with each EIO state. bit0 holds EIO0 state, bit1 holds EIO1 state, etc. If value = 15 (binary 0000 1111) EIO0-EIO3 would be 1, logic high. EIO4-7 would be 0, logic low. If value = 240 (binary 1111 0000) EIO4-7 would be high, EIO0-3 would be low. If value = 1 (binary 0000 0001) EIO0 is high, EIO1-7 are low.

Jay
jay_coder's picture
Thank you, everything works

Thank you, everything works fine now.