Click here to Skip to main content
15,918,125 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Stuck on how to receive data?

To give you some context, I have the "Optris CT" IR sensor, there is a list of commands which you can find by googling Optris CT commands.

Any help would be greatly appreciated.

What I have tried:

1. Declared the COM port

2. Written a function for opening port

3. Written a function for sending data - which I'm not sure if its correct


VB
Imports System.IO.Ports
Public Class Form1

  Dim ComPort As New SerialPort("COM1", 9600, Parity.None, 8, 1)

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    ComPort.Open()

  End Sub

Private Sub cmdSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSend.Click
    Dim sendArray() As Byte = New Byte() {&H01, &H0, &H0, &H0, &H0, &H0, &H0, &H0}

ComPort.Write(sendArray, 0, 8)

  End Sub
Posted
Updated 30-Aug-16 5:29am
v3

1 solution

Um.
VB.NET
Private Sub cmdSend_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStop.Click
Dim sendArray() As Byte = New Byte() {&H01, &H0, &H0, &H0, &H0, &H0, &H0, &H0}

ComPort.Write(stopArray, 0, 8)

End Sub

Did you mean to send the sendArray instead of stopArray?:
VB.NET
ComPort.Write(sendArray, 0, 8)
 
Share this answer
 
Comments
Member 12292743 30-Aug-16 11:03am    
yeah i've edited the question
Member 12292743 30-Aug-16 11:03am    
is the cmdSend Sub looking okay?
OriginalGriff 30-Aug-16 11:16am    
We can't tell - we don't have the same hardware setup you do.
What happens when you call it?
Have you looked at the manufacturers site?
Member 12292743 30-Aug-16 11:18am    
okay i understand. However how do I go about writing the function to receive hex?
OriginalGriff 30-Aug-16 11:29am    
Receive hex is simple: you just use the Read method on teh SerialPort instance:
https://msdn.microsoft.com/en-us/library/ms143549(v=vs.110).aspx
But...remember that serial ports are SLOW. 9600 baud is only around 1000 bytes per second, so if you read too soon you may not get all the bytes you expect.
It's better to use the DataReceived event and "build up" the bytes that you receive until you have the "complete" message - you will probably get one event per byte, not one per message.

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