Data string manipulation in VB 2010
-
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.
-
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.
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 IfIncoming 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[^]
-
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 IfIncoming 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[^]
-
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 IfIncoming 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[^]
-
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.
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 :)
-
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 :)
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.
-
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.
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?
-
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?
-
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?
-
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.
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.