1

次の c# コードを使用して 250000 ビット/秒のボー レートで COM5 ポートにアクセスしようとしていますが、以下の行で例外メッセージが表示されますselectedbaudrate = Convert.ToInt32 (cmbBaudRate.SelectedItem.ToString());pronterface.pyプログラムを使用すると、この速度でデバイスにアクセスできます。書いたときに変数に乗る0のはなぜですか?selectedbaudrate250000

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: 
4

2 に答える 2

1

cmbBaudRate または cmbBaudRate.SelectedItem のいずれかがヌルです

于 2012-09-30T11:00:57.077 に答える
0

申し訳ありませんが、簡単な解決策:フォームのドロップダウン値に値250000を追加しましたが、機能しました。

于 2012-09-30T15:40:45.767 に答える