Link to home
Start Free TrialLog in
Avatar of cmdolcet
cmdolcetFlag for United States of America

asked on

How to check a strings integrety

When pulling data from a COM port  depending on when I pull my data I may return 600 value in my array or 2 values in my array. What I have notice is that at times my array may get a little messing pulling incorrect values. I need some idea how to fix this issue and only pull the correct value.

-0.01
-0.01
-0.01
-0.01
-0.01
-0.01
-0.01
-0.01
-0.01
-0.01
-0.01
-0.01
-0.01
-0.01
-0.01

I know at the end of my value I have a CR however some times I may see a wrong value of -0.0 which should of been -0.01
Avatar of Carlos Villegas
Carlos Villegas
Flag of United States of America image

Hi, what SerialPort class method are you using to pull your data?
Avatar of cmdolcet

ASKER

serialPort.ReadExisting
So you are splitting this into an array? something like this?
Dim myValues() As String = serialPort.ReadExisting().Split(vbCr)

Open in new window

If not, please can you post your code? the code block that fill your array.
Maybe you only want to do this?
Dim myValues() As String = serialPort.ReadExisting().Split(vbCr)
For i As Integer = 0 To myValues.Length - 1
    If myValues(i).Equals("-0.0") Then
	myValues(i) = "-0.01"
    End If
Next

Open in new window


Private DiamondbackValues As New ArrayList
    Private DiamondbackSerials As New ArrayList
    Private DiamondbackActiveCOMList As New ArrayList
    Private DiamondbackCOM_Received As Boolean = False



    'Sends the Live Stream command over to every Digital Indicator plugged into a USB port
    Public Sub GetDiamondBack_Data(ByVal serialPort As SerialPort)
        ' Dim tempArray() As String
        Dim serialarray() As String
        DiamondbackData = String.Empty

        If Not serialPort.IsOpen() Then
            serialPort.Open()
        End If

        'Must Read the Serial number first to know where to place the value in the runtime screen.
        'If the [STRM] command was set seccefully then 
        If Not StreamCommand_Sent = True Then
            serialPort.Write("[SERIAL]")
            System.Threading.Thread.Sleep(50)
            DiamondbackSerial = serialPort.ReadExisting
            If DiamondbackSerial.Length > 0 Then
                DiamondbackSerial = DiamondbackSerial.Replace("{", "").Replace("}", "").Replace("SERIAL", "").Replace(" ", "")
                If Not ActiveSerialPorts.Count = DiamondbackSerials.Count Then
                    serialarray = DiamondbackSerial.Split(","c)
                    DiamondbackSerials.Add(serialarray(0))
                End If
            Else
            End If
            System.Threading.Thread.Sleep(200)
            serialPort.Write("[STRM]")
            System.Threading.Thread.Sleep(200)
            DiamondbackData = serialPort.ReadExisting

            If DiamondbackSerial = "" Then
                DiamondbackSerial = "IndicatorReadError"
            End If

            If DiamondbackData = "" Then
                DiamondbackData = "NULL"
                DiamondbackActiveCOMList.Add(serialPort.PortName)
            Else
                DiamondbackActiveCOMList.Add(serialPort.PortName)
            End If

        ElseIf ZeroProbes = True And ProbesZero = False Then
            serialPort.Write("[ZERO]")
        Else
            Threading.Thread.Sleep(50)
            DiamondbackData = serialPort.ReadExisting
            If DiamondbackData = "" Then
                DiamondbackData = "NULL"
                serialPort.Write("[STRM]")
                System.Threading.Thread.Sleep(50)
                DiamondbackData = serialPort.ReadExisting
                If DiamondbackData = "" Then
                    DiamondbackData = "NULL"
                End If
            Else
                System.Threading.Thread.Sleep(50)
                DiamondbackData = serialPort.ReadExisting
            End If
        End If
        Dim Data As String() = DiamondbackData.Split(vbCr)
        System.Threading.Thread.Sleep(50)
        DiamondbackValues.Add(Data(0))
    End Sub

Open in new window

Hi, then do you need something like this?
Private DiamondbackValues As New ArrayList
    Private DiamondbackSerials As New ArrayList
    Private DiamondbackActiveCOMList As New ArrayList
    Private DiamondbackCOM_Received As Boolean = False



    'Sends the Live Stream command over to every Digital Indicator plugged into a USB port
    Public Sub GetDiamondBack_Data(ByVal serialPort As SerialPort)
        ' Dim tempArray() As String
        Dim serialarray() As String
        DiamondbackData = String.Empty

        If Not serialPort.IsOpen() Then
            serialPort.Open()
        End If

        'Must Read the Serial number first to know where to place the value in the runtime screen.
        'If the [STRM] command was set seccefully then 
        If Not StreamCommand_Sent = True Then
            serialPort.Write("[SERIAL]")
            System.Threading.Thread.Sleep(50)
            DiamondbackSerial = serialPort.ReadExisting
            If DiamondbackSerial.Length > 0 Then
                DiamondbackSerial = DiamondbackSerial.Replace("{", "").Replace("}", "").Replace("SERIAL", "").Replace(" ", "")
                If Not ActiveSerialPorts.Count = DiamondbackSerials.Count Then
                    serialarray = DiamondbackSerial.Split(","c)
                    DiamondbackSerials.Add(serialarray(0))
                End If
            Else
            End If
            System.Threading.Thread.Sleep(200)
            serialPort.Write("[STRM]")
            System.Threading.Thread.Sleep(200)
            DiamondbackData = serialPort.ReadExisting

            If DiamondbackSerial = "" Then
                DiamondbackSerial = "IndicatorReadError"
            End If

            If DiamondbackData = "" Then
                DiamondbackData = "NULL"
                DiamondbackActiveCOMList.Add(serialPort.PortName)
            Else
                DiamondbackActiveCOMList.Add(serialPort.PortName)
            End If

        ElseIf ZeroProbes = True And ProbesZero = False Then
            serialPort.Write("[ZERO]")
        Else
            Threading.Thread.Sleep(50)
            DiamondbackData = serialPort.ReadExisting
            If DiamondbackData = "" Then
                DiamondbackData = "NULL"
                serialPort.Write("[STRM]")
                System.Threading.Thread.Sleep(50)
                DiamondbackData = serialPort.ReadExisting
                If DiamondbackData = "" Then
                    DiamondbackData = "NULL"
                End If
            Else
                System.Threading.Thread.Sleep(50)
                DiamondbackData = serialPort.ReadExisting
            End If
        End If

        Dim Data As String() = DiamondbackData.Split(vbCr)

	For i As Integer = 0 To Data.Length - 1
	    If Data(i).Equals("-0.0") Then
		Data(i) = "-0.01"
	    End If
	Next

        DiamondbackValues.AddRange(Data)
    End Sub

Open in new window


I added this to the above code:
        Dim Data As String() = DiamondbackData.Split(vbCr)

	For i As Integer = 0 To Data.Length - 1
	    If Data(i).Equals("-0.0") Then
		Data(i) = "-0.01"
	    End If
	Next

        DiamondbackValues.AddRange(Data)

Open in new window

OK I see where you going with this, however what happens if I return a value like 65.00 and it should of been 6.05..........

I can;t hard code in every scenario
mmm then I misunderstood your question... I need to know the raw data sent by your device, please dump it to a txt file and post that here.

You said what if your device return 65.00, how you know that the correct value must be 6.05? I dont understand that buddy... please attach the data so I can bring you a better assist.
The data stream captured is below. This is just one instance of an example:
-0.01
-0.01
-0.01
-0.01
-0.01
-0.01
-0.01
-0.01
-0.01
-0.01
-0.01
-0.01
-0.01
-0.01
-0.01

The example above is a general output stream. the expected value is -0.01 which in this stream this is correct. However in some cases the stream may be all 6.05 and in one array slot i may return 65.00
Sorry buddy, I dont understand... can you try to do an example?
I think its this line of code that is messing up the format of the string

  Dim Data As String() = DiamondbackData.Split(vbCr)

how can I check the integrety to make sure it is in format that I capture the whole number and not portions. The reading is seperated by a CR
Hi, please use System.IO.File.WriteAllText method to save the contents of DiamondbackData, then post the saved file.
Before I do that I think I found my issue. I used a serial program to sniff out what was going on and when I return a long string of data (-10.01 CR -10.01CR 0.01 CR) the last reading somehow got truncated and messed up. is the code how I populate the arraylist. How can I check to make sure my data is correct?

I hope that explains it a little better.
DiamondbackData = serialPort.ReadExisting
        Dim Data As String() = DiamondbackData.Split(vbCr)
        DiamondbackValues.Add(Data(0))
        DiamondbackData = String.Empty

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Carlos Villegas
Carlos Villegas
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry! this way in your code:
DiamondbackData = serialPort.ReadExisting
If IsValidData(DiamondbackData) Then
        Dim Data As String() = DiamondbackData.Split(vbCr)
	DiamondbackValues.Add(Data(0))
End If
DiamondbackData = String.Empty

Open in new window