KB 0004
Writing to a ControlLogix String Variable.


CATEGORY

ActiveX Controls

 

QUESTION

How do I write to a ControlLogix string variable?

 

ANSWER

A string variable is actually a structure composed of two atomic data types shown in the table below.

You must perform two writes, one to length field and one to the data field.

Format: 

Field Type Description
LEN DINT String length in bytes
DATA SINT[82] String data, up to 82 bytes allowed

To access the string's length field, specify '.LEN' at the end of the tag name.

MyString.Len
String1.Len

To access the string's data field, specify '.DATA' at the end of the tag name.

MyString.Data
String1.Data

 

VB6 EXAMPLE CODE

'------------------------------------------------------------------------------
' This VB6 subroutine writes a string value to a ControlLogix tag of type string
'
' Parameters
'   t is the ControlLogix tag name
'   s is the string value to write
'------------------------------------------------------------------------------
Sub WriteLogixString(t As String, s As String)
  Asabtcp1.Function = FUNC_AB_LOGIX_WRITE_ELEMENT

  ' Write string length
  Asabtcp1.Memstart = t & ".LEN"
  Asabtcp1.SetDataLongM 0, Len(s)
  Asabtcp1.MemQty = 1
  Asabtcp1.MemType = "D"
  Asabtcp1.SyncRefresh
  If Asabtcp1.Result <> 0 Then
    MsgBox "Error " & Hex(Asabtcp1.Result) & " writing string length. " & Asabtcp1.ResultString
    Exit Sub
  End If

  ' Write string data
  Asabtcp1.Memstart = t & ".DATA"
  Asabtcp1.MemType = "S"
  Asabtcp1.MemQty = Len(s)
  For i = 0 To Len(s) - 1
    Asabtcp1.SetDataByteM i, AscB(Mid(s, i + 1, 1))
  Next
  Asabtcp1.SyncRefresh
  If Asabtcp1.Result <> 0 Then
    MsgBox "Error " & Hex(Asabtcp1.Result) & " writing string data. " & Asabtcp1.ResultString
    Exit Sub
  End If