Pergunta

I have a FEZ Board (I don't think this matters) and I'm communicating with it through a serial port. I made a WPF application, where I open the COM port and write data over it. This all works great, but after sending data a random amount of times (sometimes 3, sometimes 4) I get an exception saying "A device attached to the system is not functioning" on the Write method of the port, when I try sending again or try closing the port after this exception I get "Access to the port is denied". When I restart the program I get the same error on the Open method (which is logical ofcourse). When I just disconnect the USB which represents the serial link and reconnect it, I can use the port again.

Here's my code:

    SerialPort port = new SerialPort("COM10", 9600);
    public MainWindow()
    {
        InitializeComponent();
        port.Open();
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        SendMessage("Relay1On\r\n");
    }

    private void button2_Click(object sender, RoutedEventArgs e)
    {
        SendMessage("Relay1Off\r\n");
    }

    private void SendMessage(string message)
    {
        try
        {
            port.Write(message);
        }
        catch (Exception exc)
        {
            listbox1.Items.Add(exc.Message);
        }
    }

    private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        port.Close();
    }

I don't think I'm doing anything weird. The code which is running on the FEZ Board is the following:

    private Gadgeteer.Interfaces.DigitalOutput _relay;

    void ProgramStarted()
    {
        try
        {
            usbSerial.Configure(9600, GT.Interfaces.Serial.SerialParity.None, GT.Interfaces.Serial.SerialStopBits.One, 8);
            usbSerial.SerialLine.LineReceivedEventDelimiter = "\r\n";
            usbSerial.SerialLine.AutoReadLineEnabled = true;
            usbSerial.SerialLine.ReadTimeout = GT.Interfaces.Serial.InfiniteTimeout;
            usbSerial.SerialLine.LineReceived += new GT.Interfaces.Serial.LineReceivedEventHandler(SerialLine_LineReceived);
            usbSerial.SerialLine.Open();
            _relay = eBlockExpansion.SetupDigitalOutput(GT.Socket.Pin.Nine, false);
        }
        catch (Exception exc)
        {
            throw exc;
        }
    }

    void SerialLine_LineReceived(GT.Interfaces.Serial sender, string data)
    {
        if (data == "Relay1On")
        {
            _relay.Write(true);
        }
        else if (data == "Relay1Off")
        {
            _relay.Write(false);            
        }
    }

But I have this feeling it has more to do with the Desktop side.

Foi útil?

Solução

That looks to me like a problem with your serial port. USB is pretty unreliable, and USB Serial Ports are dreadfully unreliable. You may be able to improve things with a different driver or sticking it into a different USB port, get a different brand of USB adapter, or ideally replace it with a PCI card.

Outras dicas

Sounds like a flaky USB-Serial adapter (I don't think there is any other kind) coupled with .NET's slightly flaky SerialPort implementation. .NET really doesn't cope well with a sudden loss of a serial port.

See here

In the past I've gone to the extent of taking all the serial port communication into a separate process just so I'd be able to kill it and restart it without killing the rest of my application.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top