Results 1 to 11 of 11

Thread: Receiving >1 byte over serial

Threaded View

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2016
    Posts
    7

    Exclamation Receiving >1 byte over serial

    so i have an optris ct IR sensor and you can find the list of commands in this link below
    https://www.google.co.uk/url?sa=t&rc...OnGD4-8Yo2Cprg

    So I want to send the command 01 and hopefully I will receive "04 D3" if you have a look in the examples section of the pdf above.

    I have tried to use the code in the follwing link
    http://mikrotronics.blogspot.co.uk/2...VqkH1vrZWHtD8H

    However nothing appears in the text box and I'm not sure what is happening

    Last thing, when I put in some breakpoints and step into the function and go line by line I get an error message on the line SerialPort1.Write(outdata, 0, 1) in the ScrollDAta_Scroll private function which reads
    " Invalid operation exception was unhandled; the port is closed"

    Not sure what to do


    Code:
    Dim outData As Byte() = New Byte(0) {}
     
    Private Function HextoByte(ByVal msg As String) As Byte()
            msg = msg.Replace(" ", "")
     
            Dim combuffer As Byte() = New Byte(msg.Length \ 2 - 1) {}
            For i As Integer = 0 To msg.Length - 1 Step 2
                combuffer(i \ 2) = CByte(Convert.ToByte(msg.Substring(i, 2), 16))
            Next
     
            Return combuffer
        End Function
     
    Private Function BytetoHex(ByVal comByte As Byte()) As String
            Dim builder As New StringBuilder(comByte.Length * 3)
            For Each data As Byte In comByte
                builder.Append((Convert.ToString(data, 16).PadLeft(2, "0")))
            Next
            Return builder.ToString().ToUpper()
        End Function
     
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            lblDataValue.Text = ScrollData.Value.ToString()
            ScrollData.Enabled = False
            ScrollData.Maximum = 255
            ScrollData.LargeChange = 1
            btnDisconnect.Enabled = False
            tbRx.Text = "00"
     
            Dim Portnames as String() = SerialPOrt.GetPortNames
            IF Portnames is nothing then
                msgbox("No ports detected")
                me.close()
            End If
            cbComPort.Items.AddRange(Portnames)
            cbComPort.Text = Portnames(0)
        End Sub
     
    Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
     
            outData(0) = Convert.ToByte(lblDataValue.Text)
     
            Try
                SerialPort1.PortName = cboCOMPorts.Items(cboCOMPorts.SelectedIndex).ToString()
                SerialPort1.BaudRate = 9600
                SerialPort1.Open()
                SerialPort1.Write(outData, 0, 1)
                btnDisconnect.Enabled = True
                ScrollData.Enabled = True
                btnConnect.Enabled = False
            Catch ex As Exception
                btnDisconnect.PerformClick()
            End Try
        End Sub
     
     Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
            Try
                SerialPort1.DiscardInBuffer()
                SerialPort1.DiscardOutBuffer()
                SerialPort1.Close()
                ScrollData.Value = 0
                ScrollData.Enabled = False
                btnConnect.Enabled = True
                btnDisconnect.Enabled = False
            Catch ex As Exception
     
            End Try
        End Sub
     
    Private Sub ScrollData_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles ScrollData.Scroll
            lblDataValue.Text = ScrollData.Value.ToString("X")
     
            outData(0) = Convert.ToByte(ScrollData.Value)
            SerialPort1.Write(outData, 0, 1)
        End Sub
     
    Private Delegate Sub DisplayDelegate(ByVal displayChar As String)
     
        Private Sub DisplayCharacter(ByVal displayChar As String)
            tbRx.Text = displayChar
        End Sub
     
        Private Sub serialPort1_DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
            Dim rx As Integer
            rx = SerialPort1.BytesToRead
            Dim comBuff As Byte() = New Byte(rx - 1) {}
            SerialPort1.Read(comBuff, 0, rx)
            tbRx.Invoke(New DisplayDelegate(AddressOf DisplayCharacter), New Object() {BytetoHex(comBuff)})
        End Sub
    Thanks
    Last edited by VBNovice96; Sep 20th, 2016 at 09:02 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width