38.9.5.7 Reading Text in VB

There are two ways to read text in VB as shown below.

1. Using ReadDeviceStr in VB to read text

In this case, you need to specify (fix) the location size to store the already read text.

Public Sub Sample1()
Dim strData As String * 10 'Correct specification method specifying the read size
'Dim strData As String 'Wrong specification method not specifying the text size

Dim IErr As Long

IErr = ReadDeviceStr("ReadDeviceStrD", "ReadDeviceVariantD", strData, 10)
If IErr <> 0 Then
MsgBox "Read Error = " & IErr
Else
MsgBox "Read String = " & strData
End If

End Sub

2. Using ReadDeviceVariant in VB to read text

If not specifying the location size to store the already read text, use Variant type.

Public Sub Sample2()

Dim IErr As Long
Dim vrData As Variant 'Define read-data storage as Variant type
IErr = ReadDeviceVariant("GP1", "LS100", vrData, 10, EASY_AppKind_Str)
If IErr <> 0 Then
MsgBox "Read Error = " & IErr
Else
MsgBox "Read String = " & vrData
End If
End Sub

It should be noted that WinGP SDK uses NULL at the end of the text. Thus, text acquired by the above method has the NULL character at the end, which needs to be removed.

The following shows sample functions to shorten the text up to the NULL.

Public Function TrimNull(strData As String) As String
Dim i As Integer
i = InStr(1, strData, Chr$(0), vbBinaryCompare)
If 0 < i Then
TrimNull = Left(strData, i - 1)
Else
TrimNull = strData
End If
End Function