2 Replies - 366 Views - Last Post: 15 February 2020 - 09:31 AM Rate Topic: -----

#1 JonasK   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-April 10

Serial port coding

Posted 03 February 2020 - 05:04 AM

Hi. In my design, the microcontroller transmits information to the PC via Bluetooth. Sending text string.
Sending consistently everything works fine. But with less transmission, 1 Sec or more - the connection hangs.
Putty, HyperTerminal, Android terminal receives text neatly. Wait until the connection is established and then
OK. Please tell me, what should the code be for the program to wait for text to be sent? Here's my code:


'Simple example of receiving serial data

Imports System
Imports System.Text
Imports System.Drawing
Imports System.IO.Ports
Imports System.Windows.Forms
Imports System.Timers           ' Enables use of Timer class

Public Class Form1
      
    Dim comPORT As String
    Dim receivedData As String = ""
    '--------------------------------------------------------------------
    Public Sub Form1_Load_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Label1.Text = "Timer"
        Timer1.Enabled = False
        comPORT = ""
        For Each sp As String In My.Computer.Ports.SerialPortNames
            comPort_ComboBox.Items.Add(sp)
        Next
    End Sub
    '--------------------------------------------------------------------
    Public Sub comPort_ComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles comPort_ComboBox.SelectedIndexChanged
        If (comPort_ComboBox.SelectedItem <> "") Then
            comPORT = comPort_ComboBox.SelectedItem
        End If
    End Sub
    '----------------------------------------------------------------------
    Public Sub connect_BTN_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles connect_BTN.Click

        If (connect_BTN.Text = "Connect") Then
            If (comPORT <> "") Then
                SerialPort1.Close()

                SerialPort1.PortName = comPORT
                SerialPort1.BaudRate = 9600
                SerialPort1.DataBits = 8
                SerialPort1.Parity = Parity.None
                SerialPort1.StopBits = StopBits.One
                SerialPort1.Handshake = Handshake.None
                SerialPort1.Encoding = System.Text.Encoding.Default
                
                SerialPort1.ReadTimeout = 10000 ' ?

                SerialPort1.Open()

                connect_BTN.Text = "Dis-connect"
                Timer1.Enabled = True
                Label1.ForeColor = Color.Red
                Label1.Text = "Timer: ON"
            Else
                MsgBox("Select a COM port first")
            End If
        Else
            SerialPort1.Close()
            connect_BTN.Text = "Connect"
            Timer1.Enabled = False
            Label1.ForeColor = Color.Blue
            Label1.Text = "Timer: OFF"
        End If
    End Sub
    '----------------------------------------------
    Public Sub Timer1_Tick_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        receivedData = ReceiveSerialData()
        RichTextBox1.Text &= receivedData
    End Sub
    '********************************************
    Function ReceiveSerialData() As String

        Dim returnStr As String = ""
        Dim Incoming As String = ""
        Dim SerialPort1 As IO.Ports.SerialPort = Nothing

        Try
            Do
                Incoming = SerialPort1.ReadExisting()
                ' Incoming = SerialPort1.ReadLine()
                If Incoming Is Nothing Then
                    Exit Do
                Else
                    returnStr &= Incoming & vbCrLf
                End If
            Loop

        Catch ex As TimeoutException
            returnStr = "Error: Serial Port read timed out."
            ' Finally
            '    If SerialPort1 IsNot Nothing Then SerialPort1.Close()
        End Try
        Return returnStr

    End Function
    '********************************************
 End Class



I think something must be different in Function ReceiveSerialData() As String ?
sorry for my English.

Is This A Good Question/Topic? 0
  • +

Replies To: Serial port coding

#2 maceysoftware   User is offline

  • Member Title
  • member icon

Reputation: 394
  • View blog
  • Posts: 1,648
  • Joined: 07-September 13

Re: Serial port coding

Posted 08 February 2020 - 02:07 AM

This doesn't seem right, you got a infinite loop in your ReceiveSerialData.

It has been about 6 years since I played with serial communications, so some of this may be wrong.

If you don't send something and let it break out often then yes, your application will hang because you will have halted the message pump for too long, the core essence of what a windows application relies on.

The serial port class listen in the background for traffic to be received, this is done on a different thread so not to block the message pump and allow your application to function while its waiting for data, when something is received it raises a event back on your main thread and provides you with a way to read the data this is what you should be doing.
Example DataReceived

You can do the listening yourself but this needs to be on a separate thread not on the main UI thread. like so:
https://docs.microso...etframework-4.8

remember to be careful of cross threading issues when involving threads.

The thing that may have caused some confusion here is timers, while they sleep on a separate thread, there Tick event is executed on the thread they were created, in this case the Main UI thread.

Timers
Was This Post Helpful? 0
  • +
  • -

#3 JonasK   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-April 10

Re: Serial port coding

Posted 15 February 2020 - 09:31 AM

maceysoftware, thanks for the reply. Yes, timer is just the wrong thing in the com.port.function. We will try to modify the code as much as possible.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1