none
Visual basic command send and receive RRS feed

  • Question

  • Hi guys,

    I am stuck with the command send and receive part in vb. I have included the pics and code. Can anyone please guide me in this to receive the response as shown in the pic when sending the command?

    Imports System
    Imports System.Threading
    Imports System.IO.Ports
    Imports System.ComponentModel
    
    
    Public Class Form1
    
        'Dim myport As String()
        Delegate Sub SetTextCallBack(ByVal [text] As String)
        Private readBuffer As String = String.Empty
        Private Bytenumber As Integer
        Private ByteToRead As Integer
        Private byteEnd(2) As Char
        Private comOpen As Boolean
    
    #Region "check"
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim myport As String() = System.IO.Ports.SerialPort.GetPortNames
            Portbox.Items.AddRange(myport)
            Send_btn.Enabled = False
        End Sub
    
        Private Sub Init_btn_Click(sender As System.Object, e As System.EventArgs) Handles Init_btn.Click
            SerialPort1.PortName = Portbox.Text
            SerialPort1.BaudRate = BaudRate_box.Text
            SerialPort1.Open()
            ProgressBar1.Value = 100
            Init_btn.Enabled = False
            Send_btn.Enabled = True
            Close_btn.Enabled = True
        End Sub
    
        Private Sub Send_btn_Click(sender As System.Object, e As System.EventArgs) Handles Send_btn.Click
            SerialPort1.Write(Command_box.Text & vbCr)
        End Sub
    
        Private Sub Close_btn_Click(sender As System.Object, e As System.EventArgs) Handles Close_btn.Click
            SerialPort1.Close()
            ProgressBar1.Value = 0
            Init_btn.Enabled = True
            Send_btn.Enabled = False
            Close_btn.Enabled = False
        End Sub
    #End Region
    
        Private Sub SerialPort1_DataReceived(sender As System.Object, e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
            ReceivedText(SerialPort1.ReadExisting())
        End Sub
    
        Private Sub ReceivedText(ByVal [text] As String)
            If Me.InvokeRequired Then
                Dim x As New SetTextCallBack(AddressOf ReceivedText)
                Me.Invoke(x, New Object() {(text)})
            Else
                Me.Response_box.Text &= [text]
            End If
        End Sub
    
    End Class
    

    Saturday, April 28, 2018 8:20 AM

Answers

  • Hi, 

    Thanks. Yeah the two values at the end of each message are a CRC check. As am new to this, Can you help me in calculating CRC check with some sample code?

    • Marked as answer by Sam12345vb Saturday, April 28, 2018 7:29 PM
    Saturday, April 28, 2018 10:23 AM

All replies

  • I am stuck with the command send and receive part in vb. I have included the pics and code.

    That format doesn't look right.   You need to determine if, for instance, "(79)" means the four characters "(", "7", "9", and ")" or the single character "O".   It looks to me like those two values at the end of each message are a CRC check, in which case you should calculate the values and send as bytes, not a character strings.

    Saturday, April 28, 2018 8:45 AM
  • Hi, 

    Thanks. Yeah the two values at the end of each message are a CRC check. As am new to this, Can you help me in calculating CRC check with some sample code?

    • Marked as answer by Sam12345vb Saturday, April 28, 2018 7:29 PM
    Saturday, April 28, 2018 10:23 AM
  • Hi guys,

    Can anyone help me in calculating CRC check with some sample code? As I am beginner, I am struggling in this.

    I need the above for the following,

    Saturday, April 28, 2018 11:42 AM
  • Thanks. Yeah the two values at the end of each message are a CRC check. As am new to this, Can you help me in calculating CRC check with some sample code?

    There's lots of CRC samples available on the interweb, but that is a very unusual one, so you will need to do quite a bit of original coding to get that to work. What's a 'sync character'?

    Saturday, April 28, 2018 12:13 PM
  • Can anyone help me in calculating CRC check with some sample code? As I am beginner, I am struggling in this.

    Please don't post the same question in multiple places.

    Saturday, April 28, 2018 12:14 PM
  • Thanks. Yeah the two values at the end of each message are a CRC check.

    You didn't answer the question - are the two CRC values sent as a bracketed text string or as a byte?  You have to know that before you can use the samples provided.

    Saturday, April 28, 2018 9:32 PM
  • The two CRC values sent as Bytes..
    Monday, April 30, 2018 10:06 AM
  • The two CRC values sent as Bytes..
    Then you need to change the textbox value so it is the string portion only, and append the CRC values as bytes.  Bytes are much easier than strings to work with for serial I/O, so it is probably easier to treat everything as a byte stream.
    Monday, April 30, 2018 12:34 PM
  • Thanks,, Can I have some example code for that?
    Monday, April 30, 2018 3:57 PM
  • Thanks,, Can I have some example code for that?

    For using bytes instead of strings?

            Dim cmd As Byte() = {33, 35, 64, 79, 55}
            SerialPort1.Write(cmd, 0, 5)

    See: https://msdn.microsoft.com/lt-lt/library/ms143551(v=vs.110).aspx
    Monday, April 30, 2018 9:30 PM