Hello, Have just started using VB.Net and have been trying to work on some updates to a very old piece of code. Currently we send messages via the serial port in the following formate i176.8005 <CR> i176.800555.44 <CR> o176.8005.222 <CR> The <CR> showing the end of a message arriving on the comm port. For some reason the handle input routine that I've written recognises every other character and outputs it correctly to the text box I'm using. But always seems to fail when it comes to the <CR> character. Displaying a weird box instead. I've tried several different ways of looking at the data and have cut and pasted the text outputted into notepad where the weird black boxes remain, but then in MSWord it happily starts a newline. Not at all sure where I'm going wrong. Am I using the wrong comparison to detect the <CR>? It never seems to make it into this part of the if statement. All other characters are being displayed fine but this isn't! Hope someone can help? Pete Private Sub SUComm1_OnComm(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SUComm1.OnComm Static SUBuff As String Dim NewChar As Char SUBuff = SUComm1.Input() Call HandleInput(SUBuff) End Sub Sub HandleInput(ByVal SUBuff As String) ' This is where you will process your input. This ' includes trapping characters, parsing strings, ' separating data fields, etc. For this case, you ' are simply going to display the data in the TextBox. Dim BufferSize, Counter As Integer Dim NextChar, Buffer As String Dim ENQInt, ETXInt, STXInt, ACKInt, CRInt As Integer Dim ENQStr, STXStr, ACKStr, ETXStr, CRStr As String Dim CommStr, IDStr, ProStr, ValStr As String STXStr = Chr(2) ETXStr = Chr(3) ENQStr = Chr(5) ACKStr = Chr(6) CRStr = Chr(13) CommStr = Chr(111) IDStr = Chr(105) ProStr = Chr(112) ValStr = Chr(118) Buffer = SUBuff If Buffer = ENQStr Then 'ENQ Received from SU send ENQ back. Not waiting for CR. SUOutput.SelectionStart = Len(SUOutput.Text) SUOutput.SelectedText = Str(Asc(Buffer)) + Chr(13) + Chr(10) PCOutput.SelectionStart = Len(PCOutput.Text) PCOutput.SelectedText = '5' + Chr(13) + Chr(10) SUComm1.Output() = ENQStr ElseIf Buffer = ETXStr Then 'ETX Received from SU send STX back. Not waiting for CR. SUOutput.SelectionStart = Len(SUOutput.Text) SUOutput.SelectedText = Str(Asc(Buffer)) + Chr(13) + Chr(10) PCOutput.SelectionStart = Len(PCOutput.Text) PCOutput.SelectedText = '2' + Chr(13) + Chr(10) SUComm1.Output() = STXStr ElseIf Buffer = CRStr Then 'CR Received from SU output to screen. SUOutput.SelectionStart = Len(SUOutput.Text) SUOutput.SelectedText = Chr(13) + Chr(10) Else If Asc(Buffer) > 32 Then 'Output buffer to the screen. SUOutput.SelectionStart = Len(SUOutput.Text) SUOutput.SelectedText = Buffer Else 'output asc value to the screen. SUOutput.SelectionStart = Len(SUOutput.Text) SUOutput.SelectedText = Str(Asc(Buffer)) End If End If End Sub
Vb.net Serial Port
Hello, Have just started using VB.Net and have been trying to work on some updates to a very old piece of code. Currently we send messages via the serial port in the following formate.
Serial Port Pinout
P: n/a
I have data coming into a serial port that I need to take action on. I have created a delegate to read the data from the port and put it into a text box. Once the data is read, I need to manipulate it. However, when I try to take any action on the data, I get errors. When I call the function GetTareandWeight, it gives me an error on the split function because the data is not done streaming in yet. How can I wait for all the data to get in from the port before processing it? John Code: Public Delegate Sub myDelegate(ByVal txtBox As TextBox) Private Sub SerialTare_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialTare.DataReceived TextBox1.Invoke(New myDelegate(AddressOf UPdateTextBox), New Object() {TextBox1}) End Sub Public Sub UPdateTextBox(ByVal UpdateTextBox As TextBox) Dim bytes As Integer = SerialTare.BytesToRead Dim buffer(bytes) As Byte SerialTare.Read(buffer, 0, bytes) strWeight = System.Text.Encoding.Default.GetString(buffer) TextBox1.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, ',') TextBox2.Text &= newweight & vbCrLf TextBox2.Text &= strweights.Length.ToString & vbCrLf TextBox2.Text &= 'Tare: ' & strweights(1).ToString End Sub