简体   繁体   English

连接到Com5:未处理System.NullReferenceException

[英]Connecting to Com5: System.NullReferenceException was unhandled

I am trying to access my COM5 port at a baud rate of 250000 bits per second using the following c# code but get the exception message below at the line selectedbaudrate = Convert.ToInt32 (cmbBaudRate.SelectedItem.ToString()); 我正在尝试使用以下c#代码以每秒250000位的波特率访问我的COM5端口,但是在以下行中获得了异常消息,其中selectedbaudrate = Convert.ToInt32 (cmbBaudRate.SelectedItem.ToString()); . Using the pronterface.py program I can access my device at this rate. 使用pronterface.py程序,我可以以此速率访问设备。 Why do I get 0 on the selectedbaudrate variable when I wrote 250000 ? 为什么我写250000时在selectedbaudrate变量上得到0

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;    
}

Exception: 例外:

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: 

cmbBaudRate或cmbBaudRate.SelectedItem为null

抱歉,简单的解决方案:我已将值250000添加到表单中的下拉值中,并且可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.NullReferenceException 未被用户代码处理 - System.NullReferenceException was unhandled by user code 表适配器填充中未处理的System.NullReferenceException - Unhandled System.NullReferenceException in Table Adapter Fill .NET WinForms应用程序在退出时由于未处理的System.NullReferenceException崩溃 - .NET WinForms app crashing on exit with unhandled System.NullReferenceException WindowsFormsApplication1.exe中发生了类型为'System.NullReferenceException'的未处理异常 - An unhandled exception of type 'System.NullReferenceException' occurred in WindowsFormsApplication1.exe C# Bunifu UI System.NullReferenceException 未处理 - C# Bunifu UI System.NullReferenceException was unhandled C#错误? -用户代码未处理System.NullReferenceException - C# Bug? - System.NullReferenceException was unhandled by user code 单击TreeNode时发生未处理的“System.NullReferenceException”类型的异常 - An unhandled exception of type 'System.NullReferenceException' occurring when clicking TreeNode 用户代码未处理ASP MVC,System.NullReferenceException - ASP MVC, System.NullReferenceException was unhandled by user code 检查system.nullreferenceException - Check system.nullreferenceexception System.NullReferenceException:EveAI - System.NullReferenceException: EveAI
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM