Click here to Skip to main content
15,902,874 members
Home / Discussions / Visual Basic
   

Visual Basic

 
QuestionVB2010 Serial Port Tutorial? Pin
JDBravo11-Jan-12 9:19
JDBravo11-Jan-12 9:19 
Answercheck this article out Pin
David Mujica11-Jan-12 10:02
David Mujica11-Jan-12 10:02 
GeneralRe: check this article out Pin
JDBravo11-Jan-12 10:11
JDBravo11-Jan-12 10:11 
AnswerRe: VB2010 Serial Port Tutorial? Pin
Luc Pattyn11-Jan-12 11:02
sitebuilderLuc Pattyn11-Jan-12 11:02 
GeneralRe: VB2010 Serial Port Tutorial? Pin
JDBravo11-Jan-12 11:48
JDBravo11-Jan-12 11:48 
AnswerRe: VB2010 Serial Port Tutorial? Pin
Luc Pattyn11-Jan-12 12:09
sitebuilderLuc Pattyn11-Jan-12 12:09 
GeneralRe: VB2010 Serial Port Tutorial? Pin
JDBravo12-Jan-12 4:15
JDBravo12-Jan-12 4:15 
GeneralRe: VB2010 Serial Port Tutorial? Pin
JDBravo12-Jan-12 6:19
JDBravo12-Jan-12 6:19 
Here I am again. Sorry to be bothering about something that is probably very easy but I fail to see it yet. I wrote a simple test program with a richtextbox (for receiving) and a textbox for input. There are connect and disconnect buttons and a send button (and exit to end) As suggested, trying text first just so I can see it in hyperterminal on the laptop. Not doing anything with receive yet, I just want to send something.

I made sure the null modem cable works by setting up my pc and laptop to have hyperterminal on and both can send and receive without problem, so I know the computers and the settings are OK. The program listing is below:

VB
'***********************************************************************************
'TYPE THESE BEFORE DOING ANYTHING?
'***********************************************************************************
Imports System
Imports System.ComponentModel
Imports System.Threading
Imports System.IO.Ports
'***********************************************************************************
'***********************************************************************************
Public Class TERMINAL
    Dim instance As SerialPort  'AS IN THE SAMPLE CODE
    Dim output As String
    'Dim OUTPUT As Byte
    Private mySerialPort As New SerialPort
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DONE.Click
        'BYE
        End     'CLOSE TEST PROGRAM
        'That's all folks!
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        '***********************************************************************************
        'SET THIS UP WHEN PROGRAM RUNS (FORM1 LOAD)
        'DOING EVERYTHING ON COM1 - ONLY ONE PORT ON PC AND LAPTOP
        '***********************************************************************************
        With mySerialPort           'I NEED IT TO RUN AT 9600 OR BETTER - SETTING 9600
            .PortName = "COM1"
            .BaudRate = 9600
            .DataBits = 8
            .Parity = Parity.None
            .StopBits = StopBits.One
            .Handshake = Handshake.None 'NO HANDSHAKE! I WANT TO TALK TO A CHIP (TX,RX AND GROUND WIRES ONLY)
        End With
        mySerialPort.Close()        'MAKE SURE IT IS CLOSED BEFORE START
        ''***********************************************************************************
    End Sub
    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CONNECT.Click
        '***********************************************************************************
        'CONNECT BUTTON IS CLICKED
        '***********************************************************************************
        Try
            mySerialPort.Open()         'OPEN COM1 WHEN CONNECT BUTTON IS PRESSED
        Catch ex As Exception
            MessageBox.Show(ex.Message) 'SEE IF ANY OTHER PROGRAM TOOK OVER COM1 AND GIVE WARNING
        End Try
    End Sub '***********************************************************************************
    '***********************************************************************************
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SEND.Click
        output = OUTGOING.Text
        If output = "" Then     'COMPLAINED IT IS NULL SO THIS WAS ADDED
            MsgBox("this can't be empty to send")
            Exit Sub
        End If                  '***********************************************************************************
        instance.Write(output)  'SUPPOSED TO SEND STRING OUT - GIVES ERROR ABOVE
    End Sub                     '***********************************************************************************
    '***********************************************************************************
    Private Sub DISCONNECT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DISCONNECT.Click
        mySerialPort.Close()    'DISCONNECT BUTTON CLOSES PORT
    End Sub
    '***********************************************************************************
End Class


Obviously I missed something or I'm doing something incorrectly as the program runs and it won't complain until I hit send (after opening the port OK) It gives me this error: "NullReferenceException was unhandled.
Object reference not set to an instance of an object." pointing to the instance.Write(output) line.
The "verbose" version of the error shows:
System.NullReferenceException was unhandled
  Message=Object reference not set to an instance of an object.
  Source=Serial text try
  StackTrace:
       at Serial_text_try.TERMINAL.Button1_Click(Object sender, EventArgs e) in D:\Visual Studio Personal 

Code\Serial text try\Serial text try\Form1.vb:line 53
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr 

lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at 

System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManag

er.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext 

context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext 

context)
       at System.Windows.Forms.Application.Run(ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at Serial_text_try.My.MyApplication.Main(String[] Args) in 

17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, 

Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, 

Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 


What did I do wrong? (this is frustrating) Dead | X|
I'm trying to get this going for over 3 weeks already.
Thanks for looking at it.
GeneralRe: VB2010 Serial Port Tutorial? Pin
Simon_Whale12-Jan-12 6:26
Simon_Whale12-Jan-12 6:26 
GeneralRe: VB2010 Serial Port Tutorial? Pin
JDBravo12-Jan-12 6:38
JDBravo12-Jan-12 6:38 
AnswerRe: VB2010 Serial Port Tutorial? Pin
Luc Pattyn12-Jan-12 6:30
sitebuilderLuc Pattyn12-Jan-12 6:30 
GeneralRe: VB2010 Serial Port Tutorial? Pin
JDBravo12-Jan-12 6:46
JDBravo12-Jan-12 6:46 
GeneralRe: VB2010 Serial Port Tutorial? Pin
Luc Pattyn12-Jan-12 6:57
sitebuilderLuc Pattyn12-Jan-12 6:57 
GeneralRe: VB2010 Serial Port Tutorial? Pin
JDBravo12-Jan-12 7:17
JDBravo12-Jan-12 7:17 
GeneralRe: VB2010 Serial Port Tutorial? Pin
JDBravo13-Jan-12 6:11
JDBravo13-Jan-12 6:11 
AnswerRe: VB2010 Serial Port Tutorial? Pin
Luc Pattyn13-Jan-12 6:58
sitebuilderLuc Pattyn13-Jan-12 6:58 
AnswerRe: VB2010 Serial Port Tutorial? Pin
JDBravo13-Jan-12 10:39
JDBravo13-Jan-12 10:39 
AnswerRe: VB2010 Serial Port Tutorial? Pin
Luc Pattyn13-Jan-12 13:51
sitebuilderLuc Pattyn13-Jan-12 13:51 
Questionvb.net access controls by name Pin
alejx9-Jan-12 16:10
alejx9-Jan-12 16:10 
AnswerRe: vb.net access controls by name Pin
Luc Pattyn9-Jan-12 16:28
sitebuilderLuc Pattyn9-Jan-12 16:28 
AnswerRe: vb.net access controls by name Pin
Abhinav S9-Jan-12 21:28
Abhinav S9-Jan-12 21:28 
AnswerRe: vb.net access controls by name Pin
Simon_Whale9-Jan-12 21:52
Simon_Whale9-Jan-12 21:52 
QuestionADF Scanning Pin
ivo759-Jan-12 6:37
ivo759-Jan-12 6:37 
AnswerRe: ADF Scanning Pin
Simon_Whale9-Jan-12 12:37
Simon_Whale9-Jan-12 12:37 
GeneralRe: ADF Scanning Pin
ivo7510-Jan-12 2:23
ivo7510-Jan-12 2:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.