Author Topic: WHAT IS WRONG WITH THIS JAN'S CODE?  (Read 22839 times)

inspirational4real

  • Member
  • ***
  • Posts: 2
WHAT IS WRONG WITH THIS JAN'S CODE?
« on: January 01, 2011, 11:13:02 pm »
Hi everyone, i have been battling with Jan's code for serial data receive event and so far have not been successful taught i could get someone here to figure out what i am not doing right. this is the code i am using for now to test and textbox5 is the text box to show received text from serial port.
Code: [Select]
Imports System
Imports System.IO.Ports
Imports System.Text
Imports System.Runtime.InteropServices
Imports System.Security.Permissions
Imports Microsoft.Win32

    Public Class Form1
    Friend USEPORT As New SerialPort
    Friend MYPORT As New SerialPort
    ' Define a delegate class to handle DataReceived events.
    Friend Delegate Sub SerialDataReceivedEventHandlerDelegate(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
    ' Create an instance of the delegate.
    Private SerialDataReceivedEventHandler1 As New SerialDataReceivedEventHandler(AddressOf DataReceived) 'jan use private
    Friend MyMainForm As Form1
    Private Delegate Sub AccessFormMarshalDelegate(ByVal textToDisplay As String)
    Private AccessFormMarshalDelegate1 As AccessFormMarshalDelegate

    Private Sub AccessForm(ByVal textToDisplay As String)
        TextBox5.AppendText(textToDisplay)
    End Sub
    Friend Sub DataReceived(ByVal sender As Object, ByVal e As SerialDataReceivedEventArgs)
        ' Place code to read and process data here.
        Dim ReceivedData As String
        ReceivedData = MYPORT.ReadExisting
        MyMainForm.AccessFormMarshal(ReceivedData) -------this line has the error
    End Sub
    Private Sub AccessFormMarshal(ByVal textToDisplay As String)
        ' The parameter(s) to pass to the form.
        Dim args() As Object = {textToDisplay}
        ' The AccessForm routine contains the code that accesses the form.
        Dim AccessFormMarshalDelegate1 As New AccessFormMarshalDelegate(AddressOf AccessForm)
        ' Call AccessForm, passing the parameters in args.
        MyBase.Invoke(AccessFormMarshalDelegate1, args)
    End Sub

I have placed this in the form load event of my main form called form1
Code: [Select]
AddHandler MYPORT.DataReceived, SerialDataReceivedEventHandler1
....my other vb code continues from here.

With visual studio 2008 I get the following error captioned
Quote
NullReference exception was unhandled
with the explanation
Quote
Object reference not set to an instance of an object..
in the highlighted line.

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: WHAT IS WRONG WITH THIS JAN'S CODE?
« Reply #1 on: January 02, 2011, 11:45:29 am »
What line of code causes the error? Use single stepping to find out if needed.

Try my example code here:

http://www.lvr.com/serport.htm#my_example_code

Jan

inspirational4real

  • Member
  • ***
  • Posts: 2
Re: WHAT IS WRONG WITH THIS JAN'S CODE?
« Reply #2 on: January 02, 2011, 04:59:47 pm »
Thanks Jan for taking a look at my code, from what i have posted the code line with the problem is pointed out if you look a little closer. I was just copying what your book says word for word and i did not change anything. I think the data receive event is being triggered and the accessformMarshal part is what is given the problem. you can take a look at my code again. A simple example showing how to set up and activate the receive event is just sufficient, your example you refered me to has a whole lot of code for tiding up that makes it a bit difficult to follow. Just writing the delegate and addhandler part is just sufficient. perhaps how the code should flow should have been put together in your book. You can compare my code with what is in your book and see that  i have not made any change at all.

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: WHAT IS WRONG WITH THIS JAN'S CODE?
« Reply #3 on: January 02, 2011, 05:45:42 pm »
>I think the data receive event is being triggered and the accessformMarshal part is what is given the problem.

If you comment out the code that accesses the form does the application  run without errors? If so, try commenting out portions of that code to isolate the problem.

Jan