gpt4 book ai didi

c# - 连接到 Com5 : System. NullReferenceException 未处理

转载 作者:太空宇宙 更新时间:2023-11-03 19:12:02 24 4
gpt4 key购买 nike

我尝试使用以下 C# 代码以每秒 250000 位的波特率访问我的 COM5 端口,但在下面的行 selectedbaudrate = Convert.ToInt32 (cmbBaudRate.SelectedItem.ToString()) 处收到异常消息);。使用pronterface.py程序我可以以此速率访问我的设备。当我写入 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 为空

关于c# - 连接到 Com5 : System. NullReferenceException 未处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12660672/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com