Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dim withevents COMPort as SerialPort
Dim comOpen as Boolean

Private Sub Form1_FormClosed(ByVal sender as object, ByVal e As System.Windows.Forms.FormCLosedEventArgs)Handles Me.FormClosed

With COMPort 
    .POrtName = "COM20"
    various other properties

End with

If COMPort.IsOpen then
   COMPort.Colse()
End If
End Sub

Private Sub Form1_Load(ByVal sender as  system.object, ByVal e As System.EventArgs)Handles MyBAse.Load
Dim Portnames as string() = serialPort.GetPortNames
if Portnames is nothing then 
MsgBox ("None 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

Try
 COMPort.Open()
 comOpen = ComPort.IsOpen
Catch
 comOpen = False
 MsgBox("Error Open: " & ex.Message)
End Try

If COMport.IsOPen Then  ' ERROE HERE null reference exception
 COMPort.Write("01")

End If
End Sub


Dim readBuffer as string

Private Sub COMPort_DataReceived(ByVal sender as system.Object, ByVal e As SerialDataReceivedEventArgs) HAndles COMPort.DataReceived

Try
    readBuffer  = COMPort.ReadLine()
    Me.Invoke(New EventHandler(AddressOf DoUpdate))
Catch
  MsgBox("read " &ex.Message)
End Try
End Sub

Public Sub DoUpdate(ByVal sender as Object, Byval e as System.EventArgs)

tbRx.text = readBuffer

End Sub

<pre>

So this is what I have so far

What I have tried:

Not entirely sure how to solve it. I really really really need help, I can tell everyone is going to dislike the question but I don't know what to do.

Thanks
Posted
Updated 15-Sep-16 1:40am
Comments
Patrice T 15-Sep-16 6:27am    
Ooops
Member 12292743 15-Sep-16 6:28am    
its the comment in btn Connect sub
Member 12292743 15-Sep-16 6:34am    
haha no problem
Member 12292743 15-Sep-16 11:12am    
also there is a discrepancy between what i want to write (bytes) and what i want to read( also bytes). How do I go about fixing that?

You never assign a valid value to the COMPort variable.
You should do soemthing similar to
COMPort = New SerialPort()
' set the various parameters of the serial port here

See SerialPort Class (System.IO.Ports)[^].
 
Share this answer
 
v2
Comments
Member 12292743 15-Sep-16 8:07am    
ahh right that makes sense, thanks
Member 12292743 15-Sep-16 9:33am    
also there is a discrepancy between what i want to write (bytes) and what i want to read( also bytes). How do I go about fixing that?
COMport.IsOPen

is weird, you should rather try
COMport.IsOpen

which is mire likely.

[Update]
Use the debugger to see what is COMport when error occurs.

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
v3
Comments
Member 12292743 15-Sep-16 6:36am    
yeah i typed the code in, dont ask why, it is "IsOpen" in the actual editor
Member 12292743 15-Sep-16 8:07am    
will try to understand the fundamentals of debugging, thanks

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