Results 1 to 17 of 17

Thread: Find specific characters

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    63

    Find specific characters

    Hi to all.

    I use winsock to send some data in barcode printers.

    In one of my command the printer send me back this
    

    PRINTER STATUS
    ERRORS: 1 00000000 00000100
    WARNINGS: 0 00000000 00000000
    

    How it is possible to discard all the other and keep only the middle line with or without the word ERRORS:
    something like this
    ERRORS: 1 00000000 00000100
    or this
    1 00000000 00000100

    Thanks and regards.

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,254

    Re: Find specific characters

    For multiline text try splitting text on carraige return, loop thru array, something like,..
    Code:
    Dim msg As String = "PRINTER STATUS" & Environment.NewLine _
                      & "ERRORS: 1 00000000 00000100" & Environment.NewLine _
                      & "WARNINGS: 0 00000000 00000000" & Environment.NewLine
    
    Dim result As String = ""
    Dim lines = msg.Split({Environment.NewLine}, StringSplitOptions.None)
    For Each line In lines
        If line.StartsWith("ERRORS:") Then
            result = line
            Exit For
        End If
    Next
    If result <> "" Then
        MessageBox.Show(result, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Asterisk)
    End If

  3. #3
    Addicted Member thetimmer's Avatar
    Join Date
    Jan 2014
    Location
    Plano, Texas
    Posts
    243

    Re: Find specific characters

    What are you holding the return value in, a string, stringbuilder, array, ...?
    If it's a string look at
    Code:
    string.split
    _____________
    Tim

    If anyone's answer has helped you, please show your appreciation by rating that answer.
    When you get a solution to your issue remember to mark the thread Resolved.


    reference links

  4. #4
    Addicted Member thetimmer's Avatar
    Join Date
    Jan 2014
    Location
    Plano, Texas
    Posts
    243

    Re: Find specific characters

    Edgemeal, how might you do this with LINQ ?

  5. #5
    Addicted Member thetimmer's Avatar
    Join Date
    Jan 2014
    Location
    Plano, Texas
    Posts
    243

    Re: Find specific characters

    I'm not great with LINQ but here was one example.

    Code:
    Dim msg As String = "PRINTER STATUS" & Environment.NewLine _
                      & "ERRORS: 1 00000000 00000100" & Environment.NewLine _
                      & "WARNINGS: 0 00000000 00000000" & Environment.NewLine
          
      Dim var = From s In msg.Split(vbCrLf) Select s Where s.ToUpper.Contains("ERRORS")
            If var.ToString.Length >= 0 Then
                MessageBox.Show(var(0).ToString)
            Else
                MessageBox.Show("Not Found")
            End If '
    _____________
    Tim

    If anyone's answer has helped you, please show your appreciation by rating that answer.
    When you get a solution to your issue remember to mark the thread Resolved.


    reference links

  6. #6
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,254

    Re: Find specific characters

    Quote Originally Posted by thetimmer View Post
    Edgemeal, how might you do this with LINQ ?
    untested...

    Code:
    result = lines.Where(Function(n) n.StartsWith("ERRORS:"))(0)

  7. #7
    Addicted Member thetimmer's Avatar
    Join Date
    Jan 2014
    Location
    Plano, Texas
    Posts
    243

    Re: Find specific characters

    thanks
    _____________
    Tim

    If anyone's answer has helped you, please show your appreciation by rating that answer.
    When you get a solution to your issue remember to mark the thread Resolved.


    reference links

  8. #8
    Frenzied Member IanRyder's Avatar
    Join Date
    Jan 2013
    Location
    Healing, UK
    Posts
    1,230

    Re: Find specific characters

    Hi,

    Just to expand on what has already been posted and using LINQ with Edgemeal’s String Variable, how about:-

    vb.net Code:
    1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    2.   Dim msg As String = "PRINTER STATUS" & Environment.NewLine _
    3.                       & "ERRORS: 1 00000000 00000100" & Environment.NewLine _
    4.                       & "WARNINGS: 0 00000000 00000000" & Environment.NewLine
    5.  
    6.   Dim fullMiddleLine As String = msg.Split(Environment.NewLine).Skip(1).Take(1).First
    7.   MsgBox(fullMiddleLine)
    8.  
    9.   Dim partialMiddleLine As String = msg.Split(Environment.NewLine).Skip(1).Take(1).First.Split(":"c).Last
    10.   MsgBox(partialMiddleLine)
    11. End Sub

    Hope that helps.

    Cheers,

    Ian

  9. #9
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,687

    Re: Find specific characters

    Because this is string and pattern recognition then I think that RegEx would be better suited:
    Code:
    Imports System
    Imports System.Text.RegularExpressions
    Public Module Module1
    	Public Sub Main()
    		Dim msg As String = "PRINTER STATUS" & Environment.NewLine _
                      & "ERRORS: 1 00000000 00000100" & Environment.NewLine _
                      & "WARNINGS: 0 00000000 00000000" & Environment.NewLine
    
    		'Match the word ERRORS: and any whitespace or digit after it
    		Dim r As Regex = New Regex("(ERRORS:)(\s|\d)+")
    		
    		If r.IsMatch(msg) Then
    			Console.WriteLine(r.Match(msg))
    		End If
    	End Sub
    End Module

  10. #10

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    63

    Re: Find specific characters

    First of all thanks for your time

    This is the code i use to take the specific string back
    Code:
                        Dim sendBytes As [Byte]() = System.Text.ASCIIEncoding.ASCII.GetBytes("~HQES" & vbCrLf)
                        networkStream.Write(sendBytes, 0, sendBytes.Length)
                        Dim bytes1(tcpClient.ReceiveBufferSize) As Byte
                        networkStream.Read(bytes1, 0, CInt(tcpClient.ReceiveBufferSize))
                        Dim returndata As String = Encoding.ASCII.GetString(bytes1)
                        tcpClient.Close()
                        Dim Leng As Integer = returndata.Length
                        If Leng > 2 Then
                            Call Cut_String(returndata)
                        End If
    Unfortunately none of your solutions work

  11. #11
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    9,687

    Re: Find specific characters

    Is the data that gets returned truly in this format:
    Code:
    PRINTER STATUS
    ERRORS: 1 00000000 00000100
    WARNINGS: 0 00000000 00000000
    Or is that just some "example data" that you put? We would need an exact representation of the data that is returned.

  12. #12
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,283

    Re: Find specific characters

    maybe
    Code:
    Dim theErrs As String = returndata.Split(New String() {vbCr, vbLf}, StringSplitOptions.RemoveEmptyEntries)(1).Replace("ERRORS: ", "")
    MsgBox(theErrs)
    If the other solutions didn't work, then what did they do? Produce an error? If so, what was it? Or produce the wrong text? If so, what was that?

  13. #13

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    63

    Re: Find specific characters

    Hi again
    This is the answer i take back from a barcode printer exactly as i sow you in the first post.
    Me i want the 17 digits after the error.
    All of this digits accordingly the value and the digit is a different error

    Thanks again for your time!!!!!

    For the last post
    Code:
                    Dim theErrs As String = SocketData.Split(New String() {vbCr, vbLf}, StringSplitOptions.RemoveEmptyEntries)(1).Replace("ERRORS: ", "")
                    MsgBox(theErrs)
    The msgbos show "PRINTER STATUS"
    Last edited by vagelisr; Mar 12th, 2015 at 03:52 AM.

  14. #14
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,283

    Re: Find specific characters

    Quote Originally Posted by vagelisr View Post
    For the last post
    Code:
                    Dim theErrs As String = SocketData.Split(New String() {vbCr, vbLf}, StringSplitOptions.RemoveEmptyEntries)(1).Replace("ERRORS: ", "")
                    MsgBox(theErrs)
    The msgbos show "PRINTER STATUS"
    That code was grabbing the second line from the three lines of the message you posted above. As you are seeing what looks like the first line, then that would suggest that there are actually 4 lines being returned by the printer, and the first line contains only non-printable characters (other than LineFeed or CarriageReturn). You should investigate this by examining the raw bytes returned in your bytes1 array.

    The following is fairly explicit code that should allow for "invisible" lines, and is hopefully easier for you to understand ,and easier to debug/modify:
    Code:
    Dim errDigits As String = Nothing
    
    Dim theLines() As String = SocketData.Split(New String() {vbCr, vbLf}, StringSplitOptions.RemoveEmptyEntries)
    For Each line As String In theLines
        line = line.Trim
        If line.ToUpper.StartsWith("ERRORS:") Then
            errDigits = line.Replace("ERRORS:", "").Trim
            Exit For
        End If
    Next
    
    If errDigits IsNot Nothing Then
        MsgBox(errDigits)
    Else
        MsgBox("Failed")
    End If
    Didn't DDay9's code return the correct line, except it would still be starting with "ERROR:"? If that is the case, then all you needed to do was use the .Replace method on their output as I have above.
    Last edited by Inferrd; Mar 12th, 2015 at 06:50 AM.

  15. #15

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    63

    Re: Find specific characters

    Using the code of Inferrd with some changes
    Code:
                    Dim soclength As Int16 = Len(SocketData)
                    Dim errDigits As String = Nothing
                    Dim theLines() As String = SocketData.Split(New String() {vbCr, vbLf}, StringSplitOptions.RemoveEmptyEntries)
                    For Each line As String In theLines
                        line = line.Trim
                    Next
    i take back this
    soclength=8193
    line1 = ""
    line2 = " PRINTER STATUS "
    line3 = ""
    after the line 3 exit from the for loop

    Does anybody understand what if going on?????

  16. #16
    Frenzied Member
    Join Date
    Jul 2011
    Location
    UK
    Posts
    1,283

    Re: Find specific characters

    You don't say whether my code managed to display the error digits or not.

    Seeing what the printer actually sends you might shed some light on the problem. Can you save the raw data to a file and attach that file here?

    Preferably save the raw bytes returned by the network stream (the contents of your bytes1 array as in Post#10) to a file using IO.File.WriteAllBytes(someFilePath, bytes1).

    Failing that, save the SocketData string using IO.File.WriteAllText(someFilePath, SocketData).

  17. #17

    Thread Starter
    Member
    Join Date
    Nov 2010
    Posts
    63

    Re: Find specific characters

    Change the code to this
    Code:
                        Dim sendBytes As [Byte]() = System.Text.ASCIIEncoding.ASCII.GetBytes("~HQES" & vbCrLf)
                        networkStream.Write(sendBytes, 0, sendBytes.Length)
                        Dim bytes1(tcpClient.ReceiveBufferSize) As Byte
                        networkStream.Read(bytes1, 0, CInt(tcpClient.ReceiveBufferSize))
                        Dim returndata As String = Encoding.ASCII.GetString(bytes1)
                        IO.File.WriteAllBytes(AppPath() & "Socketdata1.txt", bytes1)
                        tcpClient.Close()
                        Dim Leng As Integer = returndata.Length
                        If Leng > 2 Then
                            My.Computer.FileSystem.WriteAllText(AppPath() & "Socketdata2.txt", returndata, False)
                        End If
    the 2 files Socketdata1.txt and Socketdata2.txt are exactly the same and nowhere exist the word errors or warnings
    Can anyone understand why????

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width