[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to check a strings integrety

Posted on 2011-05-06
16
Medium Priority
?
275 Views
Last Modified: 2012-06-27
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
0
Comment
Question by:cmdolcet
  • 10
  • 6
16 Comments
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35706559
Hi, what SerialPort class method are you using to pull your data?
0
 

Author Comment

by:cmdolcet
ID: 35706675
serialPort.ReadExisting
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35707011
So you are splitting this into an array? something like this?
Dim myValues() As String = serialPort.ReadExisting().Split(vbCr)

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35707055
If not, please can you post your code? the code block that fill your array.
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35707139
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

0
 

Author Comment

by:cmdolcet
ID: 35707443

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

0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35708594
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

0
 

Author Comment

by:cmdolcet
ID: 35708632
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
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35708689
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.
0
 

Author Comment

by:cmdolcet
ID: 35708864
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
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35708952
Sorry buddy, I dont understand... can you try to do an example?
0
 

Author Comment

by:cmdolcet
ID: 35714357
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
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35717525
Hi, please use System.IO.File.WriteAllText method to save the contents of DiamondbackData, then post the saved file.
0
 

Author Comment

by:cmdolcet
ID: 35720095
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

0
 
LVL 17

Accepted Solution

by:
Carlos Villegas earned 2000 total points
ID: 35720472
Hi, I hope understand this better:

You can use this function to know is the data is valid:
Function IsValidData(ByVal data As String) As Boolean
	Dim values() As String = data.Split(New Char() {vbCr}, StringSplitOptions.RemoveEmptyEntries)

	Dim lastValue As String = Nothing
	For Each value As String In values
	    If (Not lastValue Is Nothing AndAlso Not value.Equals(lastValue)) Then
		Return False
	    End If
	    lastValue = value
	Next

	Return True
End Function

Open in new window


Then in your code:
DiamondbackData = serialPort.ReadExisting
Dim Data As String() = DiamondbackData.Split(vbCr)
If IsValidData(Data) Then
	DiamondbackValues.Add(Data(0))
End If
DiamondbackData = String.Empty

Open in new window


IsValidData return true when all the returned values are equals, this is what you need?
0
 
LVL 17

Expert Comment

by:Carlos Villegas
ID: 35720482
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

0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question