using student_exam_client.publicClass;
using System;
using System.Configuration;
using System.IO.Ports;
using System.Text;
using System.Windows;
using System.Windows.Threading;

namespace student_client.PublicClass
{
    // Token: 0x02000027 RID: 39
    internal class serialPortClass
    {
        // Token: 0x17000036 RID: 54
        // (get) Token: 0x060001E3 RID: 483 RVA: 0x0001C4C4 File Offset: 0x0001A6C4
        // (set) Token: 0x060001E4 RID: 484 RVA: 0x0001C4CB File Offset: 0x0001A6CB
        public static int timeDelayNum { get; set; }

        // Token: 0x060001E5 RID: 485 RVA: 0x0001C4D4 File Offset: 0x0001A6D4
        public serialPortClass()
        {
            serialPortClass.serialPort.PortName = ConfigurationManager.AppSettings["serialPortNum"];
            serialPortClass.serialPort.BaudRate = 9600;
            serialPortClass.serialPort.DataBits = 8;
            serialPortClass.serialPort.Parity = Parity.None;
            serialPortClass.serialPort.StopBits = StopBits.One;
            serialPortClass.serialPort.ReadTimeout = 1000;
            serialPortClass.serialPort.WriteTimeout = 1000;
            serialPortClass.serialPort.WriteBufferSize = 2000;
            serialPortClass.timeDelayNum = 0;
            for (int i = 0; i < serialPortClass.serialReceiveByte.Length; i++)
            {
                serialPortClass.serialReceiveByte[i] = 0;
            }
            serialPortClass.serialPort.DataReceived += serialPortClass.serialPort_DataReceived;
            serialPortClass.receiveBool = false;
            serialPortClass.timerShow.Interval = new TimeSpan(0, 0, 0, 0, 70);
            serialPortClass.timerShow.Tick += serialPortClass.timerShow_Tick;
        }

        // Token: 0x060001E6 RID: 486 RVA: 0x0001C5C4 File Offset: 0x0001A7C4
        private static void timerShow_Tick(object sender, EventArgs e)
        {
            if (serialPortClass.commandStr.Equals("null"))
            {
                serialPortClass.timerShow.Stop();
                return;
            }
            if (serialPortClass.commandStr.Equals("reset"))
            {
                if (serialPortClass.checkedByteEquals(serialPortClass.resetReturnCommandByte))
                {
                    serialPortClass.timerShow.Stop();
                    serialPortClass.resetSerialReceiveByte();
                }
                else
                {
                    if (++serialPortClass.sendNum > 1)
                    {
                        serialPortClass.sendNum = 0;
                        serialPortClass.timerShow.Stop();
                        serialPortClass.Communication = "resetAbnormal";
                        MessageBox.Show("串口通信异常！");
                        return;
                    }
                    serialPortClass.sendResetCommand();
                }
            }
            if (serialPortClass.commandStr.Equals("setingPointOfFaultS700K"))
            {
                if (serialPortClass.checkedByteEquals(serialPortClass.ReturnCheckStateZongDingS700k) || serialPortClass.checkedByteEquals(serialPortClass.ReturnCheckStateZongFangS700k) || serialPortClass.checkedByteEquals(serialPortClass.ReturnCheckStateWrongS700k))
                {
                    serialPortClass.Communication = "setingPointOfFaultOk";
                    serialPortClass.timerShow.Stop();
                    serialPortClass.resetSerialReceiveByte();
                }
                else
                {
                    serialPortClass.sendNum++;
                    if (serialPortClass.sendNum == 25)
                    {
                        serialPortClass.sendDataCommandAgain();
                    }
                    if (serialPortClass.sendNum > 26)
                    {
                        serialPortClass.sendNum = 0;
                        serialPortClass.timerShow.Stop();
                        serialPortClass.resetSerialReceiveByte();
                        serialPortClass.Communication = "setingPointOfFaultAbnormal";
                        MessageBox.Show("串口通信异常,请检测线路");
                        return;
                    }
                }
            }
            if (serialPortClass.commandStr.Equals("setingPointOfFaultZYJ"))
            {
                if (serialPortClass.checkedByteEquals(serialPortClass.SetPointReturnCommandByteZYJ))
                {
                    serialPortClass.timerShow.Stop();
                    serialPortClass.resetSerialReceiveByte();
                    serialPortClass.sendNum = 0;
                }
                else
                {
                    if (++serialPortClass.sendNum > 1)
                    {
                        serialPortClass.sendNum = 0;
                        serialPortClass.timerShow.Stop();
                        serialPortClass.resetSerialReceiveByte();
                        serialPortClass.Communication = "setingPointOfFaultAbnormal";
                        MessageBox.Show("串口通信异常,请重新点击“修复”按钮");
                        return;
                    }
                    serialPortClass.sendDataCommandAgain();
                }
            }
            if (serialPortClass.commandStr.Equals("TurnOutCommandS700k"))
            {
                if (serialPortClass.checkedByteEquals(serialPortClass.TurnOutReturnCommandS700k))
                {
                    serialPortClass.Communication = "TurnOutCommandS700kOk";
                    serialPortClass.timerShow.Stop();
                    serialPortClass.resetSerialReceiveByte();
                }
                else
                {
                    if (++serialPortClass.sendNum > 1)
                    {
                        serialPortClass.sendNum = 0;
                        serialPortClass.timerShow.Stop();
                        serialPortClass.resetSerialReceiveByte();
                        serialPortClass.Communication = "TurnOutCommandS700kAbnormal";
                        MessageBox.Show("串口通信异常,请重新点击道岔控制设置或检查线路");
                        return;
                    }
                    serialPortClass.sendDataCommandAgain();
                }
            }
            if (serialPortClass.commandStr.Equals("stateCheckedS700k"))
            {
                if (serialPortClass.checkedByteEquals(serialPortClass.ReturnCheckStateZongDingS700k) || serialPortClass.checkedByteEquals(serialPortClass.ReturnCheckStateZongFangS700k) || serialPortClass.checkedByteEquals(serialPortClass.ReturnCheckStateWrongS700k))
                {
                    serialPortClass.timerShow.Stop();
                    serialPortClass.Communication = "stateCheckedS700kOk";
                    publicStaticClass.serialPortCommunication = serialPortClass.Communication;
                    if (serialPortClass.checkedByteEquals(serialPortClass.ReturnCheckStateZongDingS700k))
                    {
                        publicStaticClass.switchStateNowS700k = "ZongDing";
                    }
                    if (serialPortClass.checkedByteEquals(serialPortClass.ReturnCheckStateZongFangS700k))
                    {
                        publicStaticClass.switchStateNowS700k = "ZongFang";
                    }
                    if (serialPortClass.checkedByteEquals(serialPortClass.ReturnCheckStateWrongS700k))
                    {
                        publicStaticClass.switchStateNowS700k = "Wrong";
                    }
                    serialPortClass.resetSerialReceiveByte();
                }
                else
                {
                    serialPortClass.timeDelayNum++;
                    if (serialPortClass.timeDelayNum == 15)
                    {
                        serialPortClass.sendDataCommandAgain();
                    }
                    if (serialPortClass.timeDelayNum > 21)
                    {
                        serialPortClass.timeDelayNum = 0;
                        if (++serialPortClass.sendNum > 1)
                        {
                            serialPortClass.sendNum = 0;
                            serialPortClass.timerShow.Stop();
                            serialPortClass.resetSerialReceiveByte();
                            serialPortClass.Communication = "stateCheckedS700kAbnormal";
                            publicStaticClass.serialPortCommunication = serialPortClass.Communication;
                            return;
                        }
                        serialPortClass.sendDataCommandAgain();
                    }
                }
            }
            if (serialPortClass.commandStr.Equals("stateCheckeSerialCommunication"))
            {
                if (serialPortClass.checkedByteEquals(serialPortClass.CheckedReturnCommunication))
                {
                    serialPortClass.Communication = "SerialCommunicationOk";
                    serialPortClass.timerShow.Stop();
                    serialPortClass.resetSerialReceiveByte();
                    return;
                }
                serialPortClass.sendNum = 0;
                serialPortClass.timerShow.Stop();
                serialPortClass.resetSerialReceiveByte();
                serialPortClass.Communication = "SerialCommunicationAbnormal";
            }
        }

        // Token: 0x060001E7 RID: 487 RVA: 0x0001C948 File Offset: 0x0001AB48
        private static void resetSerialReceiveByte()
        {
            serialPortClass.commandStr = "null";
            for (int i = 0; i < serialPortClass.serialReceiveByte.Length; i++)
            {
                serialPortClass.serialReceiveByte[i] = 0;
            }
            for (int j = 0; j < serialPortClass.sendCommandByteAgain.Length; j++)
            {
                serialPortClass.sendCommandByteAgain[j] = 0;
            }
            serialPortClass.sendDataByteNum = 0;
            serialPortClass.allowSendCheckedCommunication = "allow";
        }

        // Token: 0x060001E8 RID: 488 RVA: 0x0001C9A4 File Offset: 0x0001ABA4
        private static bool checkedByteEquals(byte[] inputByte)
        {
            bool checkedBool = true;
            for (int i = 0; i < inputByte.Length; i++)
            {
                if (serialPortClass.serialReceiveByte[i] != inputByte[i])
                {
                    checkedBool = false;
                    break;
                }
            }
            return checkedBool;
        }

        // Token: 0x060001E9 RID: 489 RVA: 0x0001C9D2 File Offset: 0x0001ABD2
        private static void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            if (serialPortClass.serialPort.BytesToRead >= 2)
            {
                serialPortClass.serialPort.Read(serialPortClass.serialReceiveByte, 0, serialPortClass.serialPort.BytesToRead);
            }
        }

        // Token: 0x060001EA RID: 490 RVA: 0x0001C9FC File Offset: 0x0001ABFC
        public void serialOpen()
        {
            try
            {
                if (!serialPortClass.serialPort.IsOpen)
                {
                    serialPortClass.serialPort.Open();
                }
            }
            catch
            {
                MessageBox.Show("串口驱动异常，请重新插拔串口线并重启软件");
            }
        }

        // Token: 0x060001EB RID: 491 RVA: 0x0001CA40 File Offset: 0x0001AC40
        public static void serialClose()
        {
            try
            {
                if (serialPortClass.serialPort.IsOpen)
                {
                    serialPortClass.serialPort.DiscardInBuffer();
                    serialPortClass.serialPort.DiscardOutBuffer();
                    serialPortClass.serialPort.Close();
                }
            }
            catch
            {
                MessageBox.Show("could not close serialPort!");
            }
        }

        // Token: 0x060001EC RID: 492 RVA: 0x0001CA9C File Offset: 0x0001AC9C
        public static bool sendData(string inputStr)
        {
            bool sendBool = true;
            try
            {
                if (!serialPortClass.serialPort.IsOpen)
                {
                    bool result;
                    sendBool = (result = false);
                    return result;
                }
                byte[] bytesSend = Encoding.Default.GetBytes(inputStr);
                serialPortClass.serialPort.Write(bytesSend, 0, bytesSend.Length);
                serialPortClass.serialPort.ReadExisting();
                sendBool = true;
                serialPortClass.receiveBool = true;
                serialPortClass.timerShow.Start();
            }
            catch
            {
                sendBool = false;
            }
            return sendBool;
        }

        // Token: 0x060001ED RID: 493 RVA: 0x0001CB10 File Offset: 0x0001AD10
        public static bool sendByteData(byte[] bytesSend)
        {
            bool sendBool = true;
            try
            {
                if (!serialPortClass.serialPort.IsOpen)
                {
                    bool result;
                    sendBool = (result = false);
                    return result;
                }
                serialPortClass.serialPort.Write(bytesSend, 0, bytesSend.Length);
                serialPortClass.serialPort.ReadExisting();
                sendBool = true;
                serialPortClass.receiveBool = true;
                serialPortClass.timerShow.Start();
            }
            catch
            {
                sendBool = false;
            }
            return sendBool;
        }

        // Token: 0x060001EE RID: 494 RVA: 0x0001CB78 File Offset: 0x0001AD78
        public static bool sendByteDataAgain(byte[] bytesSend, int inputSendNum)
        {
            bool sendBool = true;
            try
            {
                if (!serialPortClass.serialPort.IsOpen)
                {
                    bool result;
                    sendBool = (result = false);
                    return result;
                }
                serialPortClass.serialPort.Write(bytesSend, 0, inputSendNum);
                serialPortClass.serialPort.ReadExisting();
                sendBool = true;
                serialPortClass.receiveBool = true;
                serialPortClass.timerShow.Start();
            }
            catch
            {
                sendBool = false;
            }
            return sendBool;
        }

        // Token: 0x060001EF RID: 495 RVA: 0x0001CBE0 File Offset: 0x0001ADE0
        public static bool sendResetCommand()
        {
            byte[] serialPortName = new byte[120];
            for (int i = 0; i < serialPortName.Length; i++)
            {
                serialPortName[i] = 0;
            }
            return serialPortClass.serialControlCommandData(serialPortName);
        }

        // Token: 0x060001F0 RID: 496 RVA: 0x0001CC14 File Offset: 0x0001AE14
        public static bool sendFaultDataCommandS700K(byte[] serialPortName)
        {
            return serialPortClass.serialControlCommandData(serialPortName);
        }

        // Token: 0x060001F1 RID: 497 RVA: 0x0001CC2C File Offset: 0x0001AE2C
        private static bool serialControlCommandData(byte[] serialPortName)
        {
            serialPortClass.sendDataByteNum = 0;
            serialPortClass.commandStr = "setingPointOfFaultS700K";
            serialPortClass.allowSendCheckedCommunication = "NotAllow";
            int[] inputIntArray = new int[publicStaticClass.allSendDataNum / 4 + (4 - publicStaticClass.allSendDataNum % 4)];
            byte[] serialByte = new byte[publicStaticClass.allSendDataNum + 5];
            for (int i = 0; i < inputIntArray.Length; i++)
            {
                inputIntArray[i] = 0;
            }
            for (int j = 0; j < serialByte.Length; j++)
            {
                serialByte[j] = 0;
            }
            serialByte[serialPortClass.sendDataByteNum++] = 231;
            for (int k = 0; k < serialPortName.Length; k++)
            {
                if (serialPortName[k] == 1)
                {
                    inputIntArray[k / 32] |= 1 << k % 32;
                }
            }
            if (serialPortName[2] == 1)
            {
                publicStaticClass.checkedFaultPointThree = "hasBeenSetFault";
            }
            else
            {
                publicStaticClass.checkedFaultPointThree = "null";
            }
            for (int l = 0; l < inputIntArray.Length; l++)
            {
                inputIntArray[l] = ~inputIntArray[l];
                for (int m = 0; m < 4; m++)
                {
                    if (serialPortClass.sendDataByteNum < 16)
                    {
                        serialByte[serialPortClass.sendDataByteNum++] = Convert.ToByte(inputIntArray[l] >> m * 8 & 255);
                    }
                }
            }
            byte[] crc16Byte = CRC16.GetCRC16ConditionNum(serialByte, serialByte.Length - 4, false);
            serialByte[serialPortClass.sendDataByteNum++] = crc16Byte[1];
            serialByte[serialPortClass.sendDataByteNum++] = crc16Byte[0];
            serialPortClass.sendDataCommandCopy(serialByte);
            serialPortClass.sendDataByteNum += 2;
            bool sendBool = serialPortClass.sendByteDataAgain(serialByte, serialPortClass.sendDataByteNum);
            CommandClass.commamdS700kSwtichChecked = (byte[])serialByte.Clone();
            serialPortClass.sendNum = 0;
            serialPortClass.timerShow.Start();
            return sendBool;
        }

        // Token: 0x060001F2 RID: 498 RVA: 0x0001CDDC File Offset: 0x0001AFDC
        public static bool sendFaultRepairedDataCommandS700K(byte[] serialPortName)
        {
            return serialPortClass.serialControlCommandData(serialPortName);
        }

        // Token: 0x060001F3 RID: 499 RVA: 0x0001CDF4 File Offset: 0x0001AFF4
        public static bool sendDataCommandAgain()
        {
            return serialPortClass.sendByteDataAgain(serialPortClass.sendCommandByteAgain, serialPortClass.sendDataByteNum);
        }

        // Token: 0x060001F4 RID: 500 RVA: 0x0001CE14 File Offset: 0x0001B014
        private static void sendDataCommandCopy(byte[] inputByte)
        {
            for (int i = 0; i < serialPortClass.sendCommandByteAgain.Length; i++)
            {
                serialPortClass.sendCommandByteAgain[i] = 0;
            }
            for (int j = 0; j < inputByte.Length; j++)
            {
                serialPortClass.sendCommandByteAgain[j] = inputByte[j];
            }
        }

        // Token: 0x060001F5 RID: 501 RVA: 0x0001CE54 File Offset: 0x0001B054
        public static bool sendTurnOutCommand(byte[] inputByte)
        {
            serialPortClass.commandStr = "TurnOutCommandS700k";
            serialPortClass.allowSendCheckedCommunication = "NotAllow";
            serialPortClass.sendDataByteNum = inputByte.Length;
            serialPortClass.sendDataCommandCopy(inputByte);
            bool sendBool = serialPortClass.sendByteDataAgain(inputByte, inputByte.Length);
            serialPortClass.sendNum = 0;
            serialPortClass.timerShow.Start();
            return sendBool;
        }

        // Token: 0x060001F6 RID: 502 RVA: 0x0001CEA0 File Offset: 0x0001B0A0
        public static bool StateCheckeS700k(byte[] inputByte)
        {
            serialPortClass.commandStr = "stateCheckedS700k";
            serialPortClass.allowSendCheckedCommunication = "NotAllow";
            serialPortClass.sendDataByteNum = inputByte.Length;
            serialPortClass.sendDataCommandCopy(inputByte);
            bool sendBool = serialPortClass.sendByteDataAgain(inputByte, inputByte.Length);
            serialPortClass.sendNum = 0;
            serialPortClass.timerShow.Start();
            return sendBool;
        }

        // Token: 0x060001F7 RID: 503 RVA: 0x0001CEEC File Offset: 0x0001B0EC
        public static bool StateCheckeSerialCommunication(byte[] inputByte)
        {
            serialPortClass.commandStr = "stateCheckeSerialCommunication";
            serialPortClass.Communication = "null";
            serialPortClass.sendDataByteNum = inputByte.Length;
            serialPortClass.sendDataCommandCopy(inputByte);
            bool sendBool = serialPortClass.sendByteDataAgain(inputByte, inputByte.Length);
            serialPortClass.sendNum = 0;
            serialPortClass.timerShow.Start();
            return sendBool;
        }

        // Token: 0x060001F8 RID: 504 RVA: 0x0001CF40 File Offset: 0x0001B140
        // Note: this type is marked as 'beforefieldinit'.
        static serialPortClass()
        {
            byte[] array = new byte[4];
            array[0] = 234;
            array[1] = 1;
            serialPortClass.CheckedCommunication = array;
            serialPortClass.CheckedReturnCommunication = new byte[]
            {
                234,
                245
            };
            serialPortClass.serialDataByte = new byte[300];
            serialPortClass.sendCommandByteAgain = new byte[300];
        }

        // Token: 0x040003B1 RID: 945
        public static SerialPort serialPort = new SerialPort();

        // Token: 0x040003B2 RID: 946
        public static byte[] serialReceiveByte = new byte[1000];

        // Token: 0x040003B3 RID: 947
        public static bool receiveBool = false;

        // Token: 0x040003B4 RID: 948
        public static DispatcherTimer timerShow = new DispatcherTimer();

        // Token: 0x040003B5 RID: 949
        public static int sendNum;

        // Token: 0x040003B6 RID: 950
        public static int sendDataByteNum = 0;

        // Token: 0x040003B7 RID: 951
        public static int dataNum = 0;

        // Token: 0x040003B8 RID: 952
        public static string commandStr = "null";

        // Token: 0x040003B9 RID: 953
        public static string Communication = "null";

        // Token: 0x040003BA RID: 954
        public static string allowSendCheckedCommunication = "null";

        // Token: 0x040003BB RID: 955
        public static byte[] resetSendCommandByte = new byte[]
        {
            231,
            239,
            204,
            254,
            232
        };

        // Token: 0x040003BC RID: 956
        public static byte[] SetPointReturnCommandByteS700k = new byte[]
        {
            231,
            245
        };

        // Token: 0x040003BD RID: 957
        public static byte[] TurnOutReturnCommandS700k = new byte[]
        {
            230,
            245
        };

        // Token: 0x040003BE RID: 958
        public static byte[] ReturnCheckStateZongDingS700k = new byte[]
        {
            233,
            254
        };

        // Token: 0x040003BF RID: 959
        public static byte[] ReturnCheckStateZongFangS700k = new byte[]
        {
            233,
            253
        };

        // Token: 0x040003C0 RID: 960
        public static byte[] ReturnCheckStateWrongS700k = new byte[]
        {
            233,
            byte.MaxValue
        };

        // Token: 0x040003C1 RID: 961
        public static byte[] resetReturnCommandByte = new byte[]
        {
            214,
            245
        };

        // Token: 0x040003C2 RID: 962
        public static byte[] SetPointReturnCommandByteZYJ = new byte[]
        {
            215,
            245
        };

        // Token: 0x040003C3 RID: 963
        public static byte[] TurnOutReturnCommandZYJ = new byte[]
        {
            214,
            245
        };

        // Token: 0x040003C4 RID: 964
        public static byte[] CheckedCommunication;

        // Token: 0x040003C5 RID: 965
        public static byte[] CheckedReturnCommunication;

        // Token: 0x040003C6 RID: 966
        public static byte[] serialDataByte;

        // Token: 0x040003C7 RID: 967
        public static byte[] sendCommandByteAgain;
    }
}
