Results 1 to 11 of 11

Thread: Receiving >1 byte over serial

  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.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Receiving >1 byte over serial

    Rather than redirecting us to code posted somewhere else, especially when it's not your code you're actually using, post the code there... Just paste it, highlight it, then click the "#" in the toolbar above... that's the fastest way to get the code into the post and keep the format so we can read it.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2016
    Posts
    7

    Re: Receiving >1 byte over serial

    Quote Originally Posted by techgnome View Post
    Rather than redirecting us to code posted somewhere else, especially when it's not your code you're actually using, post the code there... Just paste it, highlight it, then click the "#" in the toolbar above... that's the fastest way to get the code into the post and keep the format so we can read it.

    -tg
    put it in, sorry about that

  4. #4
    Frenzied Member
    Join Date
    Dec 2014
    Location
    VB6 dinosaur land
    Posts
    1,190

    Re: Receiving >1 byte over serial

    Quote Originally Posted by VBNovice96 View Post
    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"
    You apparently didn't click btnConnect which calls the Open method. If you did then there must have been an exception which then called btnDisconnect's Click event.

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2016
    Posts
    7

    Re: Receiving >1 byte over serial

    I am still not sure why this is not working, any help would be appreciated

  6. #6
    New Member
    Join Date
    Sep 2016
    Posts
    12

    Re: Receiving >1 byte over serial

    I dont see any settings for Data Bits.... Stop Bits..... or Parity...... Make sure this is correct. I do see the BaudRate is set to 9600, make sure this is also correct.

  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2016
    Posts
    7

    Question Re: Receiving >1 byte over serial

    Quote Originally Posted by Tiaan0001 View Post
    I dont see any settings for Data Bits.... Stop Bits..... or Parity...... Make sure this is correct. I do see the BaudRate is set to 9600, make sure this is also correct.
    I have put those settings in, still nothing?

  8. #8
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: Receiving >1 byte over serial

    So, if you step through the code now, you no longer get
    " Invalid operation exception was unhandled; the port is closed" exception?

    Are you confident that the serial cabled is wired correctly, i.e. is it straight through or does it cross over?
    Which pin is the PLC receiving on, pin 2 or pin 3?
    Do you have another application that can talk to the PLC that runs on that computer so you know the hardware connections are good?
    Can you put an oscilloscope on the transmit pin from the PC to verify the data is being transmitted?

  9. #9

    Thread Starter
    New Member
    Join Date
    Sep 2016
    Posts
    7

    Re: Receiving >1 byte over serial

    Quote Originally Posted by passel View Post
    So, if you step through the code now, you no longer get
    " Invalid operation exception was unhandled; the port is closed" exception?

    Are you confident that the serial cabled is wired correctly, i.e. is it straight through or does it cross over?
    Which pin is the PLC receiving on, pin 2 or pin 3?
    Do you have another application that can talk to the PLC that runs on that computer so you know the hardware connections are good?
    Can you put an oscilloscope on the transmit pin from the PC to verify the data is being transmitted?
    Yeah i dont get the exception anymore

    With regards to the serial cable, the sensor is connected to my PC through a USB cable.

    Pdf outlining the list of commands here

    https://www.google.co.uk/url?sa=t&rc...OnGD4-8Yo2Cprg

  10. #10
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: Receiving >1 byte over serial

    So, the sensor has a USB connection on its end?
    I assume you had to do some driver installation that came with the device and the USB connection ends up looking like a COM port on the computer.
    Did you download the free CompactConnect software and run it to see if it could find the device and operate with it and display information?
    In particular, note which COM port it says it connects on, and if that is the same COM port that you're trying to use.

  11. #11

    Thread Starter
    New Member
    Join Date
    Sep 2016
    Posts
    7

    Re: Receiving >1 byte over serial

    Quote Originally Posted by passel View Post
    So, the sensor has a USB connection on its end?
    I assume you had to do some driver installation that came with the device and the USB connection ends up looking like a COM port on the computer.
    Did you download the free CompactConnect software and run it to see if it could find the device and operate with it and display information?
    In particular, note which COM port it says it connects on, and if that is the same COM port that you're trying to use.
    Yes it does have a USB connection
    It worked fine after installing the drivers. Yeah its on COM20 and i'm trying to use that one

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