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
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
Re: Receiving >1 byte over serial
Quote:
Originally Posted by
techgnome
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
Re: Receiving >1 byte over serial
Quote:
Originally Posted by
VBNovice96
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.
Re: Receiving >1 byte over serial
I am still not sure why this is not working, any help would be appreciated
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.
Re: Receiving >1 byte over serial
Quote:
Originally Posted by
Tiaan0001
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?
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?
Re: Receiving >1 byte over serial
Quote:
Originally Posted by
passel
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
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.
Re: Receiving >1 byte over serial
Quote:
Originally Posted by
passel
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