简体   繁体   English

C# - 无法在发布模式下打开串口

[英]C# - Can't open serial port in release mode

I'm having trouble using my serial port in Visual Studio 2019 using release mode.我在使用发布模式的 Visual Studio 2019 中使用串行端口时遇到问题。

When calling serial.Open() I don't get any errors nor exceptions, but Serial.isOpen return false and I can't write anything on the port without catching the exception "port closed".调用serial.Open()我没有收到任何错误或异常,但Serial.isOpen返回 false 并且我无法在端口上写入任何内容而不捕获异常“端口关闭”。

This problem only occurs in release mode.此问题仅在发布模式下发生。 Does anyone has tips ?有没有人有提示?

Here is my code这是我的代码

I initialise the port,我初始化端口,

static SerialPort serialPi = new SerialPort();

Then I the set the parameters right before opening the port然后我在打开端口之前设置参数

 public static void openPort(string portName, int baudRate)
        {
            try
            {
                serialPi.BaudRate = baudRate;
                serialPi.PortName = portName;
                serialPi.Open();
                serialPiPlugged = true;
            }
            catch
            {
                MessageBox.Show("ERROR WHILE OPENING PORT");
            }
        }

The baudrate come from a combobox with a list of baudrates (converted to int) and the port name from a combobox filled with a list of available port.波特率来自带有波特率列表(转换为 int)的组合框和来自填充了可用端口列表的组合框的端口名称。

Also, openPort(string,int) Is called from another class.此外, openPort(string,int)是从另一个类调用的。

I figured it out.我想到了。

The problem was that Mission Planner (the program I am working on) has a class SerialPort with some modifications on the main methods such as open or close.问题是 Mission Planner(我正在开发的程序)有一个 SerialPort 类,对主要方法(例如打开或关闭)进行了一些修改。

I was using this class instead of the one in System.IO.Ports;我正在使用这个类而不是 System.IO.Ports 中的那个;

Thank you for your answers !谢谢您的回答 !

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM