Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Data string manipulation in VB 2010

Data string manipulation in VB 2010

Scheduled Pinned Locked Moved Visual Basic
tutorialquestion
11 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Gus113

    Hi. How do you check to see the full length of a string coming from a RS232 port and how to 'TRIM" unwanted characters? Thanks in advance.

    L Offline
    L Offline
    Lost User
    wrote on last edited by
    #2

    The same way you do with any string from any provider. When reading from an external source (RS232, TCP/IP etc.) you need the source to also provide information as to the length of the string. Either by delimiters, meta information or similar means.

    1 Reply Last reply
    0
    • G Gus113

      Hi. How do you check to see the full length of a string coming from a RS232 port and how to 'TRIM" unwanted characters? Thanks in advance.

      R Offline
      R Offline
      Robert g Blair
      wrote on last edited by
      #3

      Depends on how you read it. In VB you should generally do something like:

      com1 = My.Computer.Ports.OpenSerialPort("COM1")
      com1.ReadTimeout = 10000
      Do
      Dim Incoming As String = com1.ReadLine()
      If Incoming Is Nothing Then
      Exit Do
      Else
      returnStr &= Incoming & vbCrLf
      End If

      Incoming is just a string - you can trim, replace chars etc to suit. ReadLine reads from the serial port, until the first newline char. Do another ReadLine to get the next bit. Alternatives: ReadChar - reads one character ReadByte - ditto for one byte ReadTo - Read up to a given char ReadExisting - Read whatever is in port buffer This is a good tutorial on how to do serial port comms in VB: http://www.me.umn.edu/courses/me2011/smartprodcourse/technotes/docs/serial-port-vb.pdf[^]

      G 2 Replies Last reply
      0
      • R Robert g Blair

        Depends on how you read it. In VB you should generally do something like:

        com1 = My.Computer.Ports.OpenSerialPort("COM1")
        com1.ReadTimeout = 10000
        Do
        Dim Incoming As String = com1.ReadLine()
        If Incoming Is Nothing Then
        Exit Do
        Else
        returnStr &= Incoming & vbCrLf
        End If

        Incoming is just a string - you can trim, replace chars etc to suit. ReadLine reads from the serial port, until the first newline char. Do another ReadLine to get the next bit. Alternatives: ReadChar - reads one character ReadByte - ditto for one byte ReadTo - Read up to a given char ReadExisting - Read whatever is in port buffer This is a good tutorial on how to do serial port comms in VB: http://www.me.umn.edu/courses/me2011/smartprodcourse/technotes/docs/serial-port-vb.pdf[^]

        G Offline
        G Offline
        Gus113
        wrote on last edited by
        #4

        Thanks Robert and Richard. Will give it a go this weekend.

        1 Reply Last reply
        0
        • R Robert g Blair

          Depends on how you read it. In VB you should generally do something like:

          com1 = My.Computer.Ports.OpenSerialPort("COM1")
          com1.ReadTimeout = 10000
          Do
          Dim Incoming As String = com1.ReadLine()
          If Incoming Is Nothing Then
          Exit Do
          Else
          returnStr &= Incoming & vbCrLf
          End If

          Incoming is just a string - you can trim, replace chars etc to suit. ReadLine reads from the serial port, until the first newline char. Do another ReadLine to get the next bit. Alternatives: ReadChar - reads one character ReadByte - ditto for one byte ReadTo - Read up to a given char ReadExisting - Read whatever is in port buffer This is a good tutorial on how to do serial port comms in VB: http://www.me.umn.edu/courses/me2011/smartprodcourse/technotes/docs/serial-port-vb.pdf[^]

          G Offline
          G Offline
          Gus113
          wrote on last edited by
          #5

          I see what you mean. I am programming for a weight scale but can't get the "Tare" values from the indicator. I've read the user manual but can't get get the code to work when the Tare button is pressed on the indicator. Any help will be much appreciated.

          R 1 Reply Last reply
          0
          • G Gus113

            I see what you mean. I am programming for a weight scale but can't get the "Tare" values from the indicator. I've read the user manual but can't get get the code to work when the Tare button is pressed on the indicator. Any help will be much appreciated.

            R Offline
            R Offline
            Robert g Blair
            wrote on last edited by
            #6

            Gus: I did a lot of code interfacing with industrial weight scales back in the late nineties (with VB6). I did have some generic routines that would talk good with most scales out there. Among the places my code was running was a cheese factory (small scales - a few kilos), and a fertilizer depot (big scales - 15 ton trucks). Not sure if I can find that code now (nearly 20 years later). Post the code you use, and the scale brand and model - I will have a look :)

            G 1 Reply Last reply
            0
            • R Robert g Blair

              Gus: I did a lot of code interfacing with industrial weight scales back in the late nineties (with VB6). I did have some generic routines that would talk good with most scales out there. Among the places my code was running was a cheese factory (small scales - a few kilos), and a fertilizer depot (big scales - 15 ton trucks). Not sure if I can find that code now (nearly 20 years later). Post the code you use, and the scale brand and model - I will have a look :)

              G Offline
              G Offline
              Gus113
              wrote on last edited by
              #7

              Hi Robert. The indicator is a XK3101(N). I have found this code to guide me in the right direction but can't get it to work the way I want it.

              Public Class Form1

              Dim myport As Array
              Dim strWeight As String
              
              Public Delegate Sub myDel(ByVal txtBox As TextBox)
              Private Sub Form1\_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
              
                  myport = SerialPort.GetPortNames
              
                  AddHandler SerialPort1.DataReceived, AddressOf SerialPort1\_DataReceived
              End Sub
              
              Private Sub SerialPort1\_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
              
                  txtBox.Invoke(New myDel(AddressOf UpdateTextBox), New Object() {txtBox})
              End Sub
              
              Public Sub UpdateTextBox(ByVal UpDateTextBox As TextBox)
              
                  Dim bytes As Integer = SerialPort1.BytesToRead
                  Dim Buffer(bytes) As Byte
              
                  SerialPort1.Read(Buffer, 0, bytes)
              
                  strWeight = System.Text.Encoding.Default.GetString(Buffer)
                  txtBox.AppendText(strWeight)
                  GetTareandWeight(strWeight)
              
              End Sub
              
              Private Sub GetTareandWeight(ByVal weight As String)
              
                  Dim strWeights() As String
                  Dim newWeight As String = Replace(weight, " ", " ")
                  strWeights = Split(weight, ",")
              
                  txtBox2.Text &= newWeight & vbCrLf
                  txtBox2.Text &= strWeights.Length.ToString & vbCrLf
                  txtBox2.Text &= "Tare: " & strWeights(1).ToString
              End Sub
              
              Private Sub Button1\_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
              
                  SerialPort1.PortName = "COM5"
                  SerialPort1.BaudRate = 9600
                  SerialPort1.DataBits = 8
                  SerialPort1.Parity = Parity.None
                  SerialPort1.StopBits = StopBits.One
                  SerialPort1.Handshake = Handshake.None
                  SerialPort1.Open()
              
                  SerialPort1.Write(vbCr)
              
              End Sub
              

              If I can get it to work, I will have to incorporate it into my main program. I get the weight readings but gives an error when I press the Tare button on the indicator. Hope you can help. I got the manual but can't put a picture here. Hope you can help.

              R 1 Reply Last reply
              0
              • G Gus113

                Hi Robert. The indicator is a XK3101(N). I have found this code to guide me in the right direction but can't get it to work the way I want it.

                Public Class Form1

                Dim myport As Array
                Dim strWeight As String
                
                Public Delegate Sub myDel(ByVal txtBox As TextBox)
                Private Sub Form1\_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
                
                    myport = SerialPort.GetPortNames
                
                    AddHandler SerialPort1.DataReceived, AddressOf SerialPort1\_DataReceived
                End Sub
                
                Private Sub SerialPort1\_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
                
                    txtBox.Invoke(New myDel(AddressOf UpdateTextBox), New Object() {txtBox})
                End Sub
                
                Public Sub UpdateTextBox(ByVal UpDateTextBox As TextBox)
                
                    Dim bytes As Integer = SerialPort1.BytesToRead
                    Dim Buffer(bytes) As Byte
                
                    SerialPort1.Read(Buffer, 0, bytes)
                
                    strWeight = System.Text.Encoding.Default.GetString(Buffer)
                    txtBox.AppendText(strWeight)
                    GetTareandWeight(strWeight)
                
                End Sub
                
                Private Sub GetTareandWeight(ByVal weight As String)
                
                    Dim strWeights() As String
                    Dim newWeight As String = Replace(weight, " ", " ")
                    strWeights = Split(weight, ",")
                
                    txtBox2.Text &= newWeight & vbCrLf
                    txtBox2.Text &= strWeights.Length.ToString & vbCrLf
                    txtBox2.Text &= "Tare: " & strWeights(1).ToString
                End Sub
                
                Private Sub Button1\_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
                
                    SerialPort1.PortName = "COM5"
                    SerialPort1.BaudRate = 9600
                    SerialPort1.DataBits = 8
                    SerialPort1.Parity = Parity.None
                    SerialPort1.StopBits = StopBits.One
                    SerialPort1.Handshake = Handshake.None
                    SerialPort1.Open()
                
                    SerialPort1.Write(vbCr)
                
                End Sub
                

                If I can get it to work, I will have to incorporate it into my main program. I get the weight readings but gives an error when I press the Tare button on the indicator. Hope you can help. I got the manual but can't put a picture here. Hope you can help.

                R Offline
                R Offline
                Robert g Blair
                wrote on last edited by
                #8

                Gus: Some comments on that code: (a) It looks like it is working. You have defined an event handler for receiving data from the serial port. You "get the weight readings", so we know that event handler works. (b) The "GetTareAndWeight" code tells me that the scale sends both Weight and Tare in the same string. Something like "10.3, 2.0". This must be working when you "get weight readings" - otherwise it would crash with an array error. (c) After receiving a weight txtBox2 (a multiline textbox) should look something like this: "10.3, 2.0" "2" "Tare: 2.0" (d) If your code received a string containing Tare only, ie, "2.0", then it will crash in "GetTareAndWeight" on the line 'txtBox2.Text &= "Tare: " & strWeights(1).ToString' Question for you Gus: "gives an error when I press the Tare button on the indicator". What is the error?

                G 2 Replies Last reply
                0
                • R Robert g Blair

                  Gus: Some comments on that code: (a) It looks like it is working. You have defined an event handler for receiving data from the serial port. You "get the weight readings", so we know that event handler works. (b) The "GetTareAndWeight" code tells me that the scale sends both Weight and Tare in the same string. Something like "10.3, 2.0". This must be working when you "get weight readings" - otherwise it would crash with an array error. (c) After receiving a weight txtBox2 (a multiline textbox) should look something like this: "10.3, 2.0" "2" "Tare: 2.0" (d) If your code received a string containing Tare only, ie, "2.0", then it will crash in "GetTareAndWeight" on the line 'txtBox2.Text &= "Tare: " & strWeights(1).ToString' Question for you Gus: "gives an error when I press the Tare button on the indicator". What is the error?

                  G Offline
                  G Offline
                  Gus113
                  wrote on last edited by
                  #9

                  Hi Robert. The error occurs at the line: txtBox2.Text &= "Tare: " & strWeight(1).ToString with error message; IndexOutOfRangeException was unhandled by user code. Index was outside the bounds of the array. How would I go about to fix the code?

                  1 Reply Last reply
                  0
                  • R Robert g Blair

                    Gus: Some comments on that code: (a) It looks like it is working. You have defined an event handler for receiving data from the serial port. You "get the weight readings", so we know that event handler works. (b) The "GetTareAndWeight" code tells me that the scale sends both Weight and Tare in the same string. Something like "10.3, 2.0". This must be working when you "get weight readings" - otherwise it would crash with an array error. (c) After receiving a weight txtBox2 (a multiline textbox) should look something like this: "10.3, 2.0" "2" "Tare: 2.0" (d) If your code received a string containing Tare only, ie, "2.0", then it will crash in "GetTareAndWeight" on the line 'txtBox2.Text &= "Tare: " & strWeights(1).ToString' Question for you Gus: "gives an error when I press the Tare button on the indicator". What is the error?

                    G Offline
                    G Offline
                    Gus113
                    wrote on last edited by
                    #10

                    I have changed the code a bit but now there is no output in txtBox2 but the the error message when I open the port.

                    R 1 Reply Last reply
                    0
                    • G Gus113

                      I have changed the code a bit but now there is no output in txtBox2 but the the error message when I open the port.

                      R Offline
                      R Offline
                      Robert g Blair
                      wrote on last edited by
                      #11

                      Gus: I think you can see that you have a simple array error. The compiler seems to have been quite clear about that. I suggest that you resolve that array error. The code will then work fine. I cannot help you any further with this issue Gus.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups