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:
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];
{
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);
}
}