Click here to Skip to main content
15,867,834 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am only getting a "!" return, when recieving, i know the result is in HEX Format and is 31 00 00 00 ect until 49(31) bytes is recieved ending with FF.
When using emulator on my phone, the data is revieved like it should,
I am sending command in HEX to MCU that reply in HEX.

What I have tried:

VB.NET
Imports System
Imports System.ComponentModel
Imports System.Threading
Imports System.IO.Ports


Public Class Form2
    Dim buffer As Byte() = {&H0, &H10, &H1, &HFF} '' Test to send 00 10 01 FF In HEX, so MCU Can read it.
    Dim myPort As Array
    Delegate Sub SetTextCallback(ByVal [text] As String)
    Private Shared buffer2 As String = ""


    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        myPort = IO.Ports.SerialPort.GetPortNames()
        cmbBaud.Items.Add(9600)
        cmbBaud.Items.Add(19200)

        For i = 0 To UBound(myPort)
            cmbPort.Items.Add(myPort(i))
        Next
        cmbPort.Text = cmbPort.Items.Item(0)
        cmbBaud.Text = cmbBaud.Items.Item(0)
        btnDisconnect.Enabled = False
    End Sub

    Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
        SerialPort1.PortName = cmbPort.Text
        SerialPort1.BaudRate = cmbBaud.Text
        SerialPort1.Parity = IO.Ports.Parity.None
        SerialPort1.StopBits = IO.Ports.StopBits.One
        SerialPort1.DataBits = 8
        SerialPort1.Open()
        btnConnect.Enabled = False
        btnDisconnect.Enabled = True
    End Sub

    Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
        SerialPort1.Close()
        btnConnect.Enabled = True
        btnDisconnect.Enabled = False
    End Sub

    Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click

        SerialPort1.Write(buffer, 0, 4)
    End Sub

    Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        ReceivedText(SerialPort1.ReadExisting())



    End Sub


    Private Sub ReceivedText(ByVal [text] As String)
        If Me.rtbReceived.InvokeRequired Then
            Dim x As New SetTextCallback(AddressOf ReceivedText)
            Me.Invoke(x, New Object() {(text)})
        Else
            Me.rtbReceived.Text &= [text]
        End If
    End Sub

    Private Sub cmbPort_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbPort.SelectedIndexChanged
        If SerialPort1.IsOpen = False Then
            SerialPort1.PortName = cmbPort.Text
        Else
            MsgBox("Valid only if port is Closed", vbCritical)
        End If
    End Sub

    Private Sub cmbBaud_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbBaud.SelectedIndexChanged
        If SerialPort1.IsOpen = False Then
            SerialPort1.BaudRate = cmbBaud.Text
        Else
            MsgBox("Valid only if port is Closed", vbCritical)
        End If
    End Sub

End Class
Posted
Comments
Member 14209587 16-Oct-22 10:10am    
Did some more investergating, "!" is ASCII for "33" that i send from MCU. IT then only recieve first CHAR of date. not all 33 bytes.
Ralf Meier 16-Oct-22 10:24am    
I'm not quiet sure if I had complete understand your problem - but perhaps :
If you use the DataReceived-Event from the Com-Port you should realize that this Event is fired with the first incomming Character - that means that you should check if all the Characters you are awaiting are inside the Receive-Buffer - that may be a time later.
Baiscly you could check how many Chars are inside the Buffer before do anything further ...
Member 14209587 16-Oct-22 11:18am    
Ok, but how?

I have tried to do a do while , serial_datareceived1-bytes in buffer, but same result.
i know how many bytes to wait for, first char is the number, including first char.
Could it have to do with all the zero's ?
Line recieves is 33(21 hex) 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 FF.
Have no EOL Sign, but not a problem in App for phone, and dont know if that helps.
Ralf Meier 17-Oct-22 2:27am    
Basicly, when working with a Com-Port, you should allways know how the communication-protokoll is. That means : what is the request und what is the answer. If the answer is variable you normally get this information from one of the first bytes of the answer.
In the moment I don't know how to give you more help ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900