Beispiel #1
0
        private Result NormalFindHW()
        {
            string[]             comPorts            = new string[0];
            string               str                 = "";
            Result               result              = Result.eERR_INITIAL_STATE;
            int                  retries             = 4;
            SerialPort_Corrected serialPortCorrected = SerialPortSTB;

            lock (serialPortCorrected)
            {
                if (CommState == CommunicationState.Ready)
                {
                    STBLogger.AddEvent((object)ToString(), STBLogger.EventLevel.Information, "Find HW: The comm driver was already initialized.", "Comm Drv CommunicationState:" + ((object)CommState).ToString() + " msgOut:" + FindHWmsgOut);
                    result = Result.eERR_NOT_READY;
                }
                else
                {
                    try
                    {
                        SerialPortSTB.Close();
                        Thread.Sleep(1);
                        SerialPortSTB              = new SerialPort_Corrected();
                        SerialPortSTB.BaudRate     = SerialComDriver.SetPortBaudRate(Convert.ToInt32(ConnectionString[1]));
                        SerialPortSTB.Parity       = SerialComDriver.SetPortParity((Parity)Convert.ToInt32(ConnectionString[2]));
                        SerialPortSTB.DataBits     = SerialComDriver.SetPortDataBits(Convert.ToInt32(ConnectionString[3]));
                        SerialPortSTB.StopBits     = SerialComDriver.SetPortStopBits((StopBits)Convert.ToInt32(ConnectionString[4]));
                        SerialPortSTB.Handshake    = SerialComDriver.SetPortHandshake((Handshake)Convert.ToInt32(ConnectionString[5]));
                        SerialPortSTB.Encoding     = (Encoding) new ASCIIEncoding();
                        SerialPortSTB.ReadTimeout  = 150;
                        SerialPortSTB.WriteTimeout = 500;
                        SerialPortSTB.Encoding     = Encoding.GetEncoding("Windows-1252");
                    }
                    catch (Exception ex)
                    {
                        STBLogger.AddEvent((object)this, STBLogger.EventLevel.Information, ex.ToString(), ex.Message + ex.Source + ex.StackTrace);
                    }
                }
                if (result == Result.eERR_INITIAL_STATE)
                {
                    PortScan(ref comPorts);
                    for (int comIdx = 0; comIdx < comPorts.Length; ++comIdx)
                    {
                        try
                        {
                            SerialPortSTB.PortName = comPorts[comIdx];
                            SetCommStatus("Scanning port: " + comPorts[comIdx]);
                            SerialPortSTB.Open();
                            GC.SuppressFinalize((object)SerialPortSTB.BaseStream);
                            SerialPortSTB.Write(" v\x0001\x0016\x00AD\r");
                            Thread.Sleep(60);

                            SerialPortSTB.ReadExisting();
                            Thread.Sleep(60);

                            for (int retry = 0; retry < retries; retry++)
                            {
                                SerialPortSTB.Write(FindHWmsgOut);
                                Thread.Sleep(60);

                                string id = SerialPortSTB.ReadExisting();
                                Thread.Sleep(60);

                                if (id.Length > 0)
                                {
                                    if (id.IndexOf(FindHWmsgIn) != -1)
                                    {
                                        SetCommStatus(id);
                                        result = Result.eERR_SUCCESS;
                                        break;
                                    }
                                    else
                                    {
                                        STBLogger.AddEvent((object)this, STBLogger.EventLevel.Information, "Find HW: a different board was found", "Port:" + comPorts[comIdx] + " Message:" + string.Format(" 0x{0:x2} 0x{1:x2} 0x{2:x2} 0x{3:x2} 0x{4:x2} 0x{5:x2} 0x{6:x2}", (object)Convert.ToInt16(id[0]), (object)Convert.ToInt16(id[1]), (object)Convert.ToInt16(id[2]), (object)Convert.ToInt16(id[3]), (object)Convert.ToInt16(id[4]), (object)Convert.ToInt16(id[5]), (object)Convert.ToInt16(id[6])));
                                    }
                                }
                            }
                            if (result != Result.eERR_SUCCESS)
                            {
                                SerialPortSTB.Close();
                                str = string.Empty;
                            }
                            else
                            {
                                break;
                            }
                        }
                        catch (TimeoutException)
                        {
                            SerialPortSTB.Close();
                        }
                        catch (Exception ex1)
                        {
                            STBLogger.AddEvent((object)this, STBLogger.EventLevel.Information, ex1.ToString(), "Port:" + comPorts[comIdx] + " " + ex1.Message + ex1.Source + ex1.StackTrace);
                            try
                            {
                                SerialPortSTB.Close();
                            }
                            catch (Exception) { }
                        }
                    }
                    if (Result.eERR_INITIAL_STATE == result)
                    {
                        result = Result.eERR_OTHER_ERROR;
                    }
                }
                if (result == Result.eERR_SUCCESS)
                {
                    if (CommState == CommunicationState.HWFind)
                    {
                        lock (objectLock)
                            CommState = CommunicationState.Ready;
                        CommCallbackCaller(CommEvent.CommOpen);
                    }
                }
                else if (CommState == CommunicationState.HWFind)
                {
                    lock (objectLock)
                        CommState = CommunicationState.HWFind;
                }
                return(result);
            }
        }
Beispiel #2
0
        private Result ExtendedFindHW()
        {
            string[] comPorts = new string[0];
            string   str      = "";
            Result   result   = Result.eERR_INITIAL_STATE;

            byte[] outMsg     = new byte[0];
            byte[] numArray   = new byte[0];
            byte[] decodedMsg = new byte[0];
            SerialPort_Corrected serialPortCorrected = SerialPortSTB;

            lock (serialPortCorrected)
            {
                if (CommState == CommunicationState.Ready)
                {
                    STBLogger.AddEvent((object)ToString(),
                                       STBLogger.EventLevel.Information,
                                       "Find HW: The comm driver was already initialized.",
                                       "Comm Drv CommunicationState:" + ((object)CommState).ToString() + " msgOut:" + FindHWmsgOut
                                       );
                    result = Result.eERR_NOT_READY;
                }
                else
                {
                    try
                    {
                        SerialPortSTB.Close();
                        Thread.Sleep(1);
                        SerialPortSTB              = new SerialPort_Corrected();
                        SerialPortSTB.BaudRate     = SerialComDriver.SetPortBaudRate(Convert.ToInt32(ConnectionString[1]));
                        SerialPortSTB.Parity       = SerialComDriver.SetPortParity((Parity)Convert.ToInt32(ConnectionString[2]));
                        SerialPortSTB.DataBits     = SerialComDriver.SetPortDataBits(Convert.ToInt32(ConnectionString[3]));
                        SerialPortSTB.StopBits     = SerialComDriver.SetPortStopBits((StopBits)Convert.ToInt32(ConnectionString[4]));
                        SerialPortSTB.Handshake    = SerialComDriver.SetPortHandshake((Handshake)Convert.ToInt32(ConnectionString[5]));
                        SerialPortSTB.Encoding     = (Encoding) new ASCIIEncoding();
                        SerialPortSTB.ReadTimeout  = 150;
                        SerialPortSTB.WriteTimeout = 500;
                        SerialPortSTB.Encoding     = Encoding.GetEncoding("Windows-1252");
                    }
                    catch (Exception ex)
                    {
                        STBLogger.AddEvent((object)this, STBLogger.EventLevel.Information, ex.ToString(), ex.Message + ex.Source + ex.StackTrace);
                    }
                }

                if (result == Result.eERR_INITIAL_STATE)
                {
                    PortScan(ref comPorts);
                    for (int comIdx = 0; comIdx < comPorts.Length; ++comIdx)
                    {
                        try
                        {
                            int numPotocols = ActiveProtocol.NumProtocols();
                            for (int protocolIdx = 0; protocolIdx < numPotocols; ++protocolIdx)
                            {
                                ActiveProtocol.SetProtocol((ProtocolId)protocolIdx);
                                SerialPortSTB.PortName = comPorts[comIdx];
                                SerialPortSTB.BaudRate = Convert.ToInt32(ActiveProtocol.ComplexCommand(ComplexMessage.Baudrate));

                                SerialPortSTB.Open();
                                SetCommStatus("Scanning port: " + comPorts[comIdx]);
                                GC.SuppressFinalize((object)SerialPortSTB.BaseStream);

                                SerialPortSTB.Write(ActiveProtocol.ComplexCommand(ComplexMessage.PartialReset));
                                SerialPortSTB.Write(ActiveProtocol.ComplexCommand(ComplexMessage.PartialReset));
                                SerialPortSTB.Write(ActiveProtocol.ComplexCommand(ComplexMessage.PartialReset));
                                SerialPortSTB.Write(ActiveProtocol.ComplexCommand(ComplexMessage.PartialReset));
                                SerialPortSTB.Write(ActiveProtocol.ComplexCommand(ComplexMessage.PartialReset));
                                Thread.Sleep(60);

                                SerialPortSTB.ReadExisting();
                                Thread.Sleep(60);

                                ActiveProtocol.BuildMessage(OperationId.Identify, new byte[0], ref outMsg);
                                SerialPortSTB.Write(outMsg, 0, outMsg.Length);
                                Thread.Sleep(60);

                                string response = SerialPortSTB.ReadExisting();
                                Thread.Sleep(60);
                                byte[] bytes = Encoding.GetEncoding("Windows-1252").GetBytes(response);
                                result = ActiveProtocol.DecodeMessageAndCheck(bytes, OperationId.Identify, ref decodedMsg);
                                if (protocolIdx == 2 && result == Result.eERR_PARAMETER && bytes.Length > 9)
                                {
                                    bytes[9] = (byte)11;
                                    result   = ActiveProtocol.DecodeMessageAndCheck(bytes, OperationId.Identify, ref decodedMsg);
                                }
                                if (result == Result.eERR_SUCCESS)
                                {
                                    if (protocolIdx == 2)
                                    {
                                        ID = string.Format("HW:{0} SW:{1} BL:0000", (object)bytes[6].ToString("X4"), (object)bytes[9].ToString("X4"));
                                    }
                                    else
                                    {
                                        SetCommStatus(Encoding.GetEncoding("Windows-1252").GetString(decodedMsg));
                                    }
                                    result = Result.eERR_SUCCESS;
                                    break;
                                }
                                else if (result != Result.eERR_SUCCESS)
                                {
                                    SerialPortSTB.Close();
                                    str = string.Empty;
                                }
                                else
                                {
                                    break;
                                }
                            }
                        }
                        catch (TimeoutException)
                        {
                            SerialPortSTB.Close();
                        }
                        catch (Exception ex1)
                        {
                            STBLogger.AddEvent((object)this, STBLogger.EventLevel.Information, ex1.ToString(), "Port:" + comPorts[comIdx] + " " + ex1.Message + ex1.Source + ex1.StackTrace);
                            try
                            {
                                SerialPortSTB.Close();
                            }
                            catch (Exception) { }
                        }
                    }
                    if (Result.eERR_INITIAL_STATE == result)
                    {
                        result = Result.eERR_OTHER_ERROR;
                    }
                }
                if (result == Result.eERR_SUCCESS)
                {
                    if (CommState == CommunicationState.HWFind)
                    {
                        lock (objectLock)
                            CommState = CommunicationState.Ready;
                        CommCallbackCaller(CommEvent.CommOpen);
                    }
                }
                else if (CommState == CommunicationState.HWFind)
                {
                    lock (objectLock)
                        CommState = CommunicationState.HWFind;
                }
                return(result);
            }
        }