469,903 Members | 1,499 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,903 developers. It's quick & easy.

Reading from a Serial Port output start byte

Expand|Select|Wrap|Line Numbers
  1. Imports System.IO.Ports
  2.  
  3. Public Class serial_chat
  4.  
  5.     Delegate Sub OutputTextBack(ByVal [text] As String)
  6.  
  7.     Private Sub serial_chat_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  8.         For Each port In IO.Ports.SerialPort.GetPortNames
  9.             ComboBox1.Items.Add(port)
  10.  
  11.         Next
  12.     End Sub
  13.  
  14.     Private Sub Connect_Click(sender As Object, e As EventArgs) Handles Connect.Click
  15.         Try
  16.             If Connect.Text = "Connect" Then
  17.                 SerialPort2.PortName = ComboBox1.SelectedItem.ToString()
  18.                 SerialPort2.Open()
  19.                 GroupBox2.Enabled = True
  20.                 ComboBox1.Enabled = False
  21.             Else
  22.                 SerialPort2.Close()
  23.                 GroupBox2.Enabled = False
  24.                 Connect.Text = "Connect"
  25.  
  26.             End If
  27.         Catch ex As Exception
  28.             MessageBox.Show(ex.Message)
  29.  
  30.         End Try
  31.     End Sub
  32.  
  33.     Private Sub Send_Click(sender As Object, e As EventArgs) Handles Send.Click
  34.         Try
  35.             SerialPort2.Write(TextBox1.Text & vbCr)
  36.             RichTextBox1.Text += "Me : " & TextBox1.Text & vbCr
  37.             TextBox1.Clear()
  38.  
  39.         Catch ex As Exception
  40.             MessageBox.Show(ex.Message)
  41.  
  42.         End Try
  43.     End Sub
  44.  
  45.     Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort2.DataReceived
  46.  
  47.         Receivedtext(SerialPort2.ReadExisting())
  48.  
  49.     End Sub
  50.     Sub UpdateReceviedText()
  51.  
  52.         Dim bytes As String = SerialPort2.BytesToRead
  53.         Dim buffer As String = SerialPort2.BytesToRead
  54.             buffer = SerialPort2.ReadExisting()
  55.             bytes = SerialPort2.ReadExisting()
  56.  
  57.         If Mid(bytes, 1, buffer) = " me :  # - !" Then
  58.             SerialPort2.Write("Ok")
  59.         End If
  60.  
  61.     End Sub
  62.  
  63.     Private Sub Receivedtext(ByVal [Text] As String)
  64.  
  65.         If Me.RichTextBox1.InvokeRequired Then
  66.             Dim x As New OutputTextBack(AddressOf UpdateReceviedText)
  67.             Me.Invoke(x, New Object() {(Text)})
  68.         Else
  69.             Me.RichTextBox1.Text &= [Text] & vbCrLf
  70.         End If
  71.     End Sub
  72. End Class
i want my serial port start bit read should be "#" and end with "!" when it get both character in text form ,then it should received ok message in terminal or virtual com serial port.
Please help its my task and i am newbee vb programmer.Kindly revert ASAP.
Nov 29 '21 #1
0 4932

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Simon | last post: by
9 posts views Thread by Mircea | last post: by
2 posts views Thread by evle | last post: by
6 posts views Thread by John Wright | last post: by
9 posts views Thread by Hal Vaughan | last post: by
reply views Thread by Salome Sato | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.