How to Format SerialPort received Data

NetProggi

New member
Joined
Sep 30, 2006
Messages
2
Programming Experience
1-3
Hi,

I wrote a Programm that gets data from a scale via SerialPort.
That works fine - but I don´t know wher to format the Data.
I get fore example the Data
S S 135.4 g <Return>.
I would like that the Text-Box only displays 135,4 g

If I would like to format in the Delegate-Part I olways get Errors because the lengh of the String is wrong. And I think I must say, if it starts with S S.

My try you can see is as comment.

:confused: Can anyone tell me how to format the incoming Data? - On witch event?
I think when I make a TextBox changed Event I will get a loop.
How can I chek if receive of Data is compleate?



Your´s
Stefan
VB.NET:
[FONT=Arial][SIZE=2][COLOR=black]Private Sub cmd_wiegen_uebernehmen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_wiegen_uebernehmen.Click[/COLOR][/SIZE][/FONT]
 
[FONT=Arial][COLOR=black][SIZE=2][FONT=Arial][COLOR=black][FONT=Arial][COLOR=black]' Anzeigefeld leeren[/COLOR][/FONT][/COLOR][/FONT][/SIZE]
 
[FONT=Arial][COLOR=black][SIZE=2][FONT=Arial][COLOR=black]TextBox_Waagendisplay.Text = Nothing[/COLOR][/FONT][/SIZE]
 
[SIZE=2][FONT=Arial][COLOR=black]ToolStripStatusLabel_Gewicht.Text = Nothing[/COLOR][/FONT][/SIZE]
 
[SIZE=2][FONT=Arial][COLOR=black]' Befehl an Schnittstelle / Gerät senden[/COLOR][/FONT][/SIZE]
 
[SIZE=2][FONT=Arial][COLOR=black]If ComboBox_Waagen_Modell.SelectedItem = "MODEM" Then[/COLOR][/FONT][/SIZE]
 
[SIZE=2][FONT=Arial][COLOR=black]Serielle_Schnittstelle.WriteLine("ATI3" & (Chr(13)))[/COLOR][/FONT][/SIZE]
 
[SIZE=2][FONT=Arial][COLOR=black]End If[/COLOR][/FONT][/SIZE]
 
 
 
[SIZE=2][FONT=Arial][COLOR=black]If ComboBox_Waagen_Modell.SelectedItem = "Mettler-Toledo" Then[/COLOR][/FONT][/SIZE]
 
[SIZE=2][FONT=Arial][COLOR=black]Serielle_Schnittstelle.WriteLine("S" & (Chr(13)))[/COLOR][/FONT][/SIZE]
 
[SIZE=2][FONT=Arial][COLOR=black]End If[/COLOR][/FONT][/SIZE]
 
[SIZE=2][FONT=Arial][COLOR=black]End Sub[/COLOR][/FONT][/SIZE]
 
[SIZE=2][FONT=Arial][COLOR=black]===========================================================================================[/COLOR][/FONT][/SIZE]
 
[SIZE=2][FONT=Arial][COLOR=black]Private Sub Serielle_Schnittstelle_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles Serielle_Schnittstelle.DataReceived[/COLOR][/FONT][/SIZE]
 
[SIZE=2][FONT=Arial][COLOR=black]Try[/COLOR][/FONT][/SIZE]
 
[SIZE=2][FONT=Arial][COLOR=black]TextBox_Waagendisplay.Invoke(New myDelegate(AddressOf updateTextBox), New Object() {})[/COLOR][/FONT][/SIZE]
 
[SIZE=2][FONT=Arial][COLOR=black]Catch ex As Exception[/COLOR][/FONT][/SIZE]
 
[SIZE=2][FONT=Arial][COLOR=black]MsgBox(ex.Message, MsgBoxStyle.Exclamation, "Fehler aufgetreten... ...Gerät / Port richtig eingestellt?")[/COLOR][/FONT][/SIZE]
 
[SIZE=2][FONT=Arial][COLOR=black]Exit Sub[/COLOR][/FONT][/SIZE]
 
[SIZE=2][FONT=Arial][COLOR=black]End Try[/COLOR][/FONT][/SIZE]
 
[SIZE=2][FONT=Arial][COLOR=black]End Sub[/COLOR][/FONT][/SIZE][/COLOR][/FONT][/COLOR][/FONT]
 
[FONT=Arial][SIZE=2][COLOR=black]============================================================================================[/COLOR][/SIZE][/FONT]
 
[FONT=Arial][COLOR=black][SIZE=2][FONT=Arial][COLOR=black][FONT=Arial][COLOR=black]Public Delegate Sub myDelegate()[/COLOR][/FONT][/COLOR][/FONT][/SIZE]
 
[FONT=Arial][COLOR=black][SIZE=2][FONT=Arial][COLOR=black]Public Sub updateTextBox()[/COLOR][/FONT][/SIZE]
 
[SIZE=2][FONT=Arial][COLOR=black]With TextBox_Waagendisplay[/COLOR][/FONT][/SIZE]
 
[SIZE=2][FONT=Arial][COLOR=black].Font = New Font("Microsoft Sans Serif", 16.0!, FontStyle.Bold)[/COLOR][/FONT][/SIZE]
 
[SIZE=2][B][FONT=Arial][COLOR=black][B]'If ComboBox_Waagen_Modell.SelectedItem = "Mettler-Toledo" Then[/B][/COLOR][/FONT][/B][/SIZE]
 
[B][B][SIZE=2][FONT=Arial][COLOR=black]'.AppendText(Serielle_Schnittstelle.ReadExisting.Substring(3, 11)) ' hier splitten[/COLOR][/FONT][/SIZE][/B]
 
[B][SIZE=2][FONT=Arial][COLOR=black]'Else[/COLOR][/FONT][/SIZE][/B]
 
[B][SIZE=2][FONT=Arial][COLOR=black].AppendText(Serielle_Schnittstelle.ReadExisting)[/COLOR][/FONT][/SIZE][/B]
 
[B][SIZE=2][FONT=Arial][COLOR=black]'End If[/COLOR][/FONT][/SIZE][/B]
[/B]
[SIZE=2][FONT=Arial][COLOR=black].ScrollToCaret()[/COLOR][/FONT][/SIZE]
 
[SIZE=2][FONT=Arial][COLOR=black]End With[/COLOR][/FONT][/SIZE]
 
[SIZE=2][FONT=Arial][COLOR=black]' Daten / Anzeige im Waagendisplay in die Zwischenablage kopieren[/COLOR][/FONT][/SIZE]
 
[SIZE=2][B][FONT=Arial][COLOR=black][B]Dim Uebergabewert_PA As String = TextBox_Waagendisplay.Text.Substring(0, (TextBox_Waagendisplay.Text.Length) - (2))[/B][/COLOR][/FONT][/B][/SIZE]
 
[B][B][SIZE=2][FONT=Arial][COLOR=black]Clipboard.SetDataObject(Uebergabewert_PA, True)[/COLOR][/FONT][/SIZE][/B]
 
[B][SIZE=2][FONT=Arial][COLOR=black]'Gewicht nochmal in Statusleiste anzeigen[/COLOR][/FONT][/SIZE][/B]
 
[B][SIZE=2][FONT=Arial][COLOR=black]ToolStripStatusLabel_Gewicht.Text = TextBox_Waagendisplay.Text[/COLOR][/FONT][/SIZE][/B]
[/B]
[SIZE=2][FONT=Arial][COLOR=black]End Sub
[/COLOR][/FONT][/SIZE][/COLOR][/FONT][/COLOR][/FONT]
 
Last edited by a moderator:
Dont change the text of a textbox, in the event that fires when textbox text is chhanged - youre right that this would be a loop problem.

It depends where you want to cook this data. You can do it in the class that reads the data from the serial port, but then all future classes you write that use this same class will have the data edited in the same way. If that is fine, then do that. I would prefer to do it in a higher level class - i.e. the one that requires the data to be in a certain format
 
Back
Top