Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.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,System.dll中发生类型为“System.UnauthorizedAccessException”的未处理异常 其他信息:对端口“COM3”的访问被拒绝。 当我第二次打开端口时(当我再次打开此表单时)发生错误 此问题的一个可能解决方案是将串行连接放在单独的类中,并在收到数据时提供一个事件。然后,每个窗口都可以为此事件添加事件处理程序。在下面的代码中,我尝试只维护串行连接所需的部分。我无法在我的机器上真正尝试,所以可能会有一些小问题。而且这个活动非常基础。最好定义一个满足您特定需求的委托:

System.dll中发生类型为“System.UnauthorizedAccessException”的未处理异常 其他信息:对端口“COM3”的访问被拒绝。 当我第二次打开端口时(当我再次打开此表单时)发生错误


此问题的一个可能解决方案是将串行连接放在单独的类中,并在收到数据时提供一个事件。然后,每个窗口都可以为此事件添加事件处理程序。在下面的代码中,我尝试只维护串行连接所需的部分。我无法在我的机器上真正尝试,所以可能会有一些小问题。而且这个活动非常基础。最好定义一个满足您特定需求的委托:

public class SerialConnection
{
    INIFile settings = new INIFile("C:\\Lateco\\settings.ini");
    public SerialPort SerialPort { get; set; }
    static SerialConnection connection= null;
    public event EventHandler WeightReceived;

    public static SerialConnection OpenConnection()
    {
        if(connection == null)
        {
            connection = new SerialConnection();
            string portname, baudrate, parity, databits, stopbits, handshake;

            portname = settings.Read("SERIAL PORT PROPERTIES", "PORT_NAME");
            baudrate = settings.Read("SERIAL PORT PROPERTIES", "BAUD_RATE");
            parity = settings.Read("SERIAL PORT PROPERTIES", "PARITY");
            databits = settings.Read("SERIAL PORT PROPERTIES", "DATA_BITS");
            stopbits = settings.Read("SERIAL PORT PROPERTIES", "STOP_BITS");
            handshake = settings.Read("SERIAL PORT PROPERTIES", "HANDSHAKE");

            connection.SerialPort = new SerialPort(); //error here
            connection.SerialPort.PortName = portname;
            connection.SerialPort.BaudRate = int.Parse(baudrate);
            connection.SerialPort.Parity = (Parity)Enum.Parse(typeof(Parity), parity, true);
            connection.SerialPort.DataBits = int.Parse(databits);
            connection.SerialPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), stopbits, true);
            connection.SerialPort.Handshake = (Handshake)Enum.Parse(typeof(Handshake), handshake, true);

            connection.SerialPort.Open();
            connection.SerialPort.ReadTimeout = 200;
            connection.SerialPort.DataReceived += new SerialDataReceivedEventHandler(connection.serialPort1_DataReceived);
        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            try
            {
                weight = SerialPort.ReadExisting();
                weight = weight.Substring(0, 7);

                WeightReceived?.Invoke(weight, new EventArgs());
            }
            catch (TimeoutException) { }
        }

        return connection;
    }

    public void CloseConnection()
    {
        if (SerialPort.IsOpen)
                SerialPort.Close();
    }

    ~SerialConnection()
    {
        if (SerialPort.IsOpen)
                SerialPort.Close();
    }
}
表格中
如下使用:

public partial class frmAddInventoryTransItem3 : MetroForm
    {
        public frmAddInventoryTrans ReceivingAdd { set; get; }
        public frmEditInventoryTrans ReceivingEdit { set; get; }

        string inv_type2 = null, action2 = null, document2 = null;
        private string weight;
        private SerialConnection sc = null;

        private void frmAddInventoryTransItem3_Load(object sender, EventArgs e)
        {
            txtQty.Text = 1.ToString();
            txtWeight.Text = 0.ToString("N3");
            this.ActiveControl = txtPLU;

            sc = SerialConnection.OpenConnection();
            sc.WeightReceived += new SerialDataReceivedEventHandler(WeightReceived);
        }

        private void WeightReceived(object weight, EventArgs e)
        {
                weight = weight as string;

                try
                {
                    if (this.InvokeRequired)
                        this.BeginInvoke(new EventHandler(DisplayText));
                }
                catch (ObjectDisposedException) { }
        }

        private void DisplayText(object sender, EventArgs e)
        {
            txtWeight.Text = weight;
        }
    }
}

你不能打开它两次。请尽量缩小你收到的错误范围,不要只提供整个程序。例如,错误发生在哪一行?尝试提供尽可能少的代码,这些代码仍然表现出行为。通常,只要这样做就足以让您自己找出bug所在的位置。当您提出问题时,最好提供一个最小的工作代码示例。只是为了将来。错误发生在您打开表单两次时,因此不要打开表单两次。查看我的双表单项目,了解如何只打开表单一次:@jdweng您甚至不知道在OPs解决方案中是否需要打开表单两次。你的建议解决不了这个问题。
public partial class frmAddInventoryTransItem3 : MetroForm
    {
        public frmAddInventoryTrans ReceivingAdd { set; get; }
        public frmEditInventoryTrans ReceivingEdit { set; get; }

        string inv_type2 = null, action2 = null, document2 = null;
        private string weight;
        private SerialConnection sc = null;

        private void frmAddInventoryTransItem3_Load(object sender, EventArgs e)
        {
            txtQty.Text = 1.ToString();
            txtWeight.Text = 0.ToString("N3");
            this.ActiveControl = txtPLU;

            sc = SerialConnection.OpenConnection();
            sc.WeightReceived += new SerialDataReceivedEventHandler(WeightReceived);
        }

        private void WeightReceived(object weight, EventArgs e)
        {
                weight = weight as string;

                try
                {
                    if (this.InvokeRequired)
                        this.BeginInvoke(new EventHandler(DisplayText));
                }
                catch (ObjectDisposedException) { }
        }

        private void DisplayText(object sender, EventArgs e)
        {
            txtWeight.Text = weight;
        }
    }
}