2012-09-30 51 views
1

我想以每秒250000位的波特率访问我的COM5端口,但使用以下c#代码,但在行selectedbaudrate = Convert.ToInt32 (cmbBaudRate.SelectedItem.ToString());处获得以下异常消息。使用pronterface.py程序,我可以以此速率访问我的设备。当我写250000时,为什么我会在selectedbaudrate变量上获得0连接到Com5:System.NullReferenceException是未处理的

private void buttonOpenPort_Click(object sender, EventArgs e)         
{ 
    string selectedportname;                  
    int selectedbaudrate; 
    selectedportname = combportnumber.SelectedItem.ToString();         
    selectedbaudrate = Convert.ToInt32 (cmbBaudRate.SelectedItem.ToString());     
    port = new SerialPort(selectedportname, selectedbaudrate, Parity.None, 8, StopBits.One);  
    port.DtrEnable = true; 
    port.Open();                    
    port.DataReceived += serialPort1_DataReceived;  
} 

例外:

System.NullReferenceException was unhandled 
     Message=Object reference not set to an instance of an object. 
     Source=PC Host 
     StackTrace: 
      at WindowsFormsApplication1.Form1.buttonOpenPort_Click(Object sender, EventArgs e) in D:\Catia V5 Projects\HF08 Hexapod\Version 05\Host PC Software\PC Host 13\PC Host\Form1.cs:line 106 
      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.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.IMsoComponentManager.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 WindowsFormsApplication1.Program.Main() in D:\Catia V5 Projects\HF08 Hexapod\Version 05\Host PC Software\PC Host 13\PC Host\Program.cs:line 18 
      at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
      at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
      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: 
+1

你是否已经通过调试器中的代码检查变量的值? – ChrisF

+0

ChrisF,是的奇怪的是,当我指定250000时'selectedbaudrate'为0。 –

+1

然后问题在于应该设置'selectedBaudRate'的代码,或者在设置它后它会被覆盖。 – ChrisF

回答

0

对不起球员,简单的解决方案:我在形式的值250000加到下拉值和它的工作。

1

要么cmbBaudRate或cmbBaudRate.SelectedItem为null

+0

虽然我指定了250000,但它为什么为空? –

+0

cmbBAudRate是,我想是一个组合框。当你的代码执行时它可能还没有创建(== cmbBaudRate为null)。或者,当时可能没有选择组合框中的项目(== cmbBaudRAte.SelectedItem为空) –

+0

@Arthur Mamou-Mani:我觉得你错误地解释了NullReferenceException。这并不意味着该值为零,这意味着您正试图对对象使用未初始化的引用。 –

相关问题