Click here to Skip to main content
15,887,827 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm developing a C# Windows forms application that continuously reads and writes on about 100 serial ports. In fact, I read data from some serial ports with DataReceived event, and write translated data on related other ports. For this purpose, I use six MOXA Nport 5630-16 Port Serial Device Server to convert ethernet to 96 virtual serial ports. I'm observing strange behavior in some cases. Some serial ports are closed or disappear and I can not open them again. So, I have to restart the software and/or in some cases Windows. In fact, there is not any reaction at all on the port, and my attempt to restart the port (via the app) failed. Here comes the exception on the Open() call. It's totally unpredictable and random! Does anyone has got any idea for detecting this situation and successfully restarting the port?

Thanks, Farhad Somayehee

What I have tried:

C#



public void InitialCameraPorts()
        {
            CameraPorts = new SerialPort[CameraPortNames.Count];
            LogRow __error = new LogRow();
            LogRow __log = new LogRow();
            
            __log._datetime = DateTime.Now;
            __log._logtext = "start of opening cameras port.";
            Insert2LogFile(__log, true, true);

            for (int _i = 0; _i < CameraPorts.Length; _i++)
            {
                try
                {
                    CameraPorts[_i] = new SerialPort(CameraPortNames[_i], 9600, Parity.None, 8, StopBits.Two);
                    CameraPorts[_i].Open();
                }
                catch (Exception ex)
                {
                    __error._datetime = DateTime.Now;
                    __error._logtext = string.Format("camera port {0} could not opened.", CameraPortNames[_i]);
                    Insert2ErrorFile(__error, true, true);

                    __error._logtext = ex.ToString();
                    Insert2ErrorFile(__error, true, true);
                }
            }
            __log._datetime = DateTime.Now;
            __log._logtext = "end of opening cameras port.";
            Insert2LogFile(__log, true, true);
        }


private void wrapper_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            int _listIndex = 0;

            try
            {
                SerialPort _SENDER = (SerialPort)sender;
                _listIndex = MatrixPortNames.FindIndex(x => x == _SENDER.PortName);
                int _datacount = _SENDER.BytesToRead;
                byte[] _inputData = new byte[_datacount];

                //var handler = DataReceived;

                //if (handler != null)
                {
                    MatrixPortsTime[_listIndex] = DateTime.Now;

                    _SENDER.Read(_inputData, 0, _datacount);
                    foreach (byte _B in _inputData)
                    {
                        _Panasonic_Buffer[_listIndex].Add(_B);
                    }
                }
            }
            catch (Exception ex)
            {
                LogRow __error = new LogRow();

                __error._datetime = DateTime.Now;
                __error._logtext = string.Format("Error in matrix port .", MatrixPortNames[_listIndex]);
                Insert2ErrorFile(__error, true, true);

                __error._logtext = ex.ToString();
                Insert2ErrorFile(__error, true, true);
            }
        }
Posted
Updated 23-Dec-21 21:58pm

1 solution

Talk to the manufacturers - we don't have any access to the actual hardware, so we can't even begin to emulate the problem, and duplicating the problem is the first step to finding out what causes it, and then how to fix it.

Sorry, but we really can't help you here.
 
Share this answer
 
Comments
farshadsomayehee 26-Dec-21 4:20am    
Thank you so much for your help and attention.
Farshad Somayehee
Luc Pattyn 26-Dec-21 10:26am    
Hi,

if/when you get a solution to your problem, please be so kind and publish it here, to the benefit of others who may have a similar problem.

Thanks in advance.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900