Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 串行端口(接收到的数据)不工作_C#_Winforms_Serial Port - Fatal编程技术网

C# 串行端口(接收到的数据)不工作

C# 串行端口(接收到的数据)不工作,c#,winforms,serial-port,C#,Winforms,Serial Port,我使用这段代码查找串行端口并在combobox中显示它们的名称 string[] ports = SerialPort.GetPortNames(); foreach (string p in ports) { comboBox1.Items.Add(p); } comboBox1.SelectedIndex = 0; 因此,当我启动程序时,默认端口应该是端口中的第一个 我正在使用此函数初始化串行端口 private void portInit() { port.Por

我使用这段代码查找串行端口并在combobox中显示它们的名称

string[] ports = SerialPort.GetPortNames();
foreach (string p in ports)
{
    comboBox1.Items.Add(p);
}
comboBox1.SelectedIndex = 0;
因此,当我启动程序时,默认端口应该是
端口中的第一个

我正在使用此函数初始化串行端口

private void portInit()
{
        port.PortName = comboBox1.Text;
        port.BaudRate = 57600;
        port.Parity = Parity.None;
        port.ReceivedBytesThreshold = 8;
        port.DataBits = 8;
        port.Handshake = Handshake.None;
        port.StopBits = StopBits.One;
        port.DataReceived += new SerialDataReceivedEventHandler(datarecievedhandler);
        port.Open();
}
    private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
    {
        port.Close();
        port.PortName = comboBox1.Text;
        port.Open();
    }
如果用户决定更改串行端口,我有这个事件处理程序

private void portInit()
{
        port.PortName = comboBox1.Text;
        port.BaudRate = 57600;
        port.Parity = Parity.None;
        port.ReceivedBytesThreshold = 8;
        port.DataBits = 8;
        port.Handshake = Handshake.None;
        port.StopBits = StopBits.One;
        port.DataReceived += new SerialDataReceivedEventHandler(datarecievedhandler);
        port.Open();
}
    private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
    {
        port.Close();
        port.PortName = comboBox1.Text;
        port.Open();
    }
我在这部分代码中初始化了我的串行端口

public Form1()
{
      InitializeComponent();
      InitializeGraphs();
      portInit();
      if (port.IsOpen)
            textBox1.Text += "-Port " + port.PortName + " is opened\r\n";
}

设备连接到第一个(默认)串行端口。当我运行我的程序时,第一个端口打开(我知道它是因为文本框而打开的),但是DataReceived事件处理程序不工作。当我在combobox中更改串行端口时,不会发生任何事情(预期行为),当我将串行端口更改回第一个串行端口时,它将打开,并且DataReceived事件处理程序工作正常。当然,我希望第一个串行端口立即工作,而不改变串行端口。我知道这个问题很难回答,但可能有人也有类似的问题。

简化问题-先让基础工作正常,然后添加额外的功能

删除重新打开端口的事件处理程序,并禁用不必要的设置,如ReceivedBytesThreshold,因为这些设置可能会产生副作用,使水域浑浊

然后你可以检查你有一个工作端口-如果你有错误的波特率,你可能永远不会收到任何数据。在继续之前,确保基础工作正常

一旦您有了一个工作的硬编码串行端口,然后逐个添加这些功能,这样您就可以看到哪一个破坏了它


此外,如果重新打开同一端口对象失败,您可以尝试处理端口并创建一个新端口。

简化问题-首先让基础工作正常,然后添加额外功能

删除重新打开端口的事件处理程序,并禁用不必要的设置,如ReceivedBytesThreshold,因为这些设置可能会产生副作用,使水域浑浊

然后你可以检查你有一个工作端口-如果你有错误的波特率,你可能永远不会收到任何数据。在继续之前,确保基础工作正常

一旦您有了一个工作的硬编码串行端口,然后逐个添加这些功能,这样您就可以看到哪一个破坏了它


此外,如果重新打开同一端口对象失败,您可以尝试处理端口并创建一个新端口。

当您将握手设置为“无”时,您可以打开握手信号。将RtsEnable和DtrEnable设置为true。当您将握手设置为None时,由您打开握手信号。将RtsEnable和DtrEnable设置为true。