Question Using Serial Port in Multiple Subs

mrjlaunch

New member
Joined
Jan 25, 2013
Messages
4
Programming Experience
Beginner
Hi, I'm trying to create a form to send and receive data on a serial port.
The idea is:
  1. When the form loads it opens the COM port
  2. A separate Sub handles any new data on the port and saves it in a text box
  3. When a button or enter key is pressed the string in a text box is sent to the serial port

The problem I'm having is when the program is running and any reference to "Com_Data" is called outside of the "Terminal_Enter" sub this happens:

NullReferenceException occurred
I've highlighted where the breaks occur

Code:
[COLOR=#0000ff]Imports[/COLOR] System.IO.Ports

[COLOR=#0000ff]Public Class[/COLOR] [COLOR=#008080]Terminal[/COLOR]

    [COLOR=#0000ff]Public WithEvents[/COLOR] Com_Data [COLOR=#0000ff]As[/COLOR] IO.Ports.SerialPort                           [COLOR=#008000]'Set COM_Data as the Serial Port[/COLOR]

    [COLOR=#008000]'When form "Terminal" is opening[/COLOR]
    [COLOR=#0000ff]Private Sub[/COLOR] Terminal_Enter(sender [COLOR=#0000ff]As[/COLOR] System.Object, e [COLOR=#0000ff]As[/COLOR] System.EventArgs) [COLOR=#0000ff]Handles MyBase[/COLOR].Load
        [COLOR=#0000ff]Try[/COLOR]                                                                                 [COLOR=#008000]'Try opening the ComPort[/COLOR]
            [COLOR=#0000ff]Using[/COLOR] Com_Data =
                [COLOR=#0000ff]My[/COLOR].Computer.Ports.OpenSerialPort(Main.Com_Port, Main.Com_Baud, IO.Ports.Parity.None, 8, IO.Ports.StopBits.One)              [COLOR=#008000] 'Open ComPort[/COLOR]
                Com_Data.ReadTimeout = Main.Com_Timeout                                         [COLOR=#008000]'Set timeout for recieving response[/COLOR]
                Com_Data.WriteTimeout = 100                                                     [COLOR=#008000]'Wait upto 0.1 seconds for a write to complete[/COLOR]
                Com_Data.DtrEnable = [COLOR=#0000ff]False[/COLOR]                                                      [COLOR=#008000]'Turn off DTR[/COLOR]
            [COLOR=#0000ff]End Using[/COLOR]
            Terminal_Txt_Recieve.Text = Main.Com_Port + [COLOR=#b22222]" Open"[/COLOR]
        [COLOR=#0000ff]Catch[/COLOR]
            Terminal_Txt_Recieve.Text = [COLOR=#b22222]"Unable to open "[/COLOR] + Main.Com_Port                   [COLOR=#008000]'If an error occured opening the com port, then handle this error[/COLOR]
        [COLOR=#0000ff]End Try[/COLOR]
   [COLOR=#0000ff] End Sub[/COLOR]

    [COLOR=#008000]'When form "Terminal" is closing[/COLOR]
   [COLOR=#0000ff] Private Sub[/COLOR] Terminal_Leave(sender [COLOR=#0000ff]As[/COLOR] System.Object, e [COLOR=#0000ff]As[/COLOR] System.EventArgs) [COLOR=#0000ff]Handles MyBase[/COLOR].FormClosing
         [highlight]Com_Data.Close()[/highlight]                                                       [COLOR=#008000] 'Close ComPort[/COLOR]
        Main.Enabled = [COLOR=#0000ff]True[/COLOR]                                                    [COLOR=#008000] 'Re-enable main form[/COLOR]
    [COLOR=#0000ff]End Sub[/COLOR]

    [COLOR=#008000]'Recieve Data on Serial Port[/COLOR]
    [COLOR=#0000ff]Private Sub[/COLOR] Receiver([COLOR=#0000ff]ByVal[/COLOR] sender [COLOR=#0000ff]As Object[/COLOR], [COLOR=#0000ff]ByVal[/COLOR] e [COLOR=#0000ff]As[/COLOR] SerialDataReceivedEventArgs) [COLOR=#0000ff]Handles[/COLOR] Com_Data.DataReceived
        [COLOR=#0000ff]Do[/COLOR]
            Terminal_Txt_Recieve.Text &= Com_Data.ReadLine()                   [COLOR=#008000] 'Read string on COM Port and copy to text box[/COLOR]
        [COLOR=#0000ff]Loop Until [/COLOR]Com_Data.BytesToRead = 0
    [COLOR=#0000ff]End Sub[/COLOR]

    [COLOR=#008000]'Button "Term_Send" is Clicked[/COLOR]
    [COLOR=#0000ff]Private Sub[/COLOR] Term_Send_Click(sender [COLOR=#0000ff]As[/COLOR] System.Object, e [COLOR=#0000ff]As[/COLOR] System.EventArgs) [COLOR=#0000ff]Handles[/COLOR] Term_Send.Click
         [highlight]Com_Data.WriteLine(Terminal_Txt_Send.Text + vbCr)[/highlight]                       [COLOR=#008000]'Send string and carriage return[/COLOR]
    [COLOR=#0000ff]End Sub[/COLOR]

    [COLOR=#008000]'Text Box "Terminal_Txt_Send" Enter Key Pressed[/COLOR]
    [COLOR=#0000ff]Private Sub[/COLOR] Terminal_Txt_Send_KeyPress([COLOR=#0000ff]ByVal[/COLOR] sender[COLOR=#0000ff] As[/COLOR] [COLOR=#0000ff]Object[/COLOR], [COLOR=#0000ff]ByVal[/COLOR] e[COLOR=#0000ff] As[/COLOR] System.Windows.Forms.KeyPressEventArgs) [COLOR=#0000ff]Handles[/COLOR] Terminal_Txt_Send.KeyPress
       [COLOR=#0000ff] If[/COLOR] e.KeyChar = Convert.ToChar(Keys.Enter) [COLOR=#0000ff]Then[/COLOR]                          [COLOR=#008000]'If the "Enter" key has been pressed then[/COLOR]
             [highlight]Com_Data.WriteLine(Terminal_Txt_Send.Text + vbCr)[/highlight]                   [COLOR=#008000]'Send string in text box and carriage return[/COLOR]
            e.Handled = [COLOR=#0000ff]True[/COLOR]                                                    [COLOR=#008000]'The enter key has been processed and will not alter the string in text box[/COLOR]
        [COLOR=#0000ff]End If[/COLOR]
    [COLOR=#0000ff]End Sub[/COLOR]

[COLOR=#0000ff]End Class[/COLOR]
I'm guessing somehow "Com_Data" is not-defined/unavailable outside of the sub "Terminal_Enter" but my VB knowledge isn't that great and I don't know how to fix this?
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,171
Location
Sydney, Australia
Programming Experience
10+
You have two different 'Com_Data' variables. A 'Using' statement declares variable, so the 'Com_Data' variable you use in 'Terminal_Enter' is not the same one as you use elsewhere. Regardless of the variables, the whole point of a 'Using' block is to create and destroy an object. Do you really want to destroy the SerialPort immediately after creating it?

In short, get rid of the 'Using' statement. It doesn't belong there.
 
Top Bottom