Пример #1
0
        public long getNewbestRecordNumber()
        {
            modbusClient = ConnectUserControl.getModbusClient();
            if (modbusClient != null && modbusClient.Connected == true)
            {
                try
                {
                    int[] bufNewbestRecordNumber = new int[4];
                    bufNewbestRecordNumber    = modbusClient.ReadHoldingRegisters(1064, 4);
                    bufNewbestRecordNumber[2] = 0;
                    bufNewbestRecordNumber[3] = 0;
                    long NewbestRecordNumber = Convert.ToInt64(ModbusClient.ConvertRegistersToLong(bufNewbestRecordNumber));

                    return(NewbestRecordNumber);
                }
                catch (Exception)
                {
                    Console.WriteLine("Error: Неожиданный обрыв подключения");
                    return(-1);//
                }
            }
            else
            {
                return(-1);
            }
        }
Пример #2
0
        public static string getDateTimeFromDevice()
        {
            try
            {
                int[] time = new int[4];
                time    = modbusClient.ReadHoldingRegisters(1057 - 1, 4);
                time[2] = 0;
                time[3] = 0;
                ulong time1 = Convert.ToUInt64(ModbusClient.ConvertRegistersToLong(time));
                return(ConvertTime(time1.ToString()));
            }
            catch (Exception ex)
            {
                if (stateConnection)
                {
                    if (ex.Message == "connection timed out")
                    {
                        MessageBox.Show("Подключение не установленно. Время ожидания подключения истекло");

                        stateConnection = false;

                        Action action = () => Instance.labelConnectStatus.Text = "Подключение не установлено";
                        Instance.labelConnectStatus.Invoke(action);

                        action = () => Instance.ButtonConnectDevice.Enabled = true;
                        Instance.ButtonConnectDevice.Invoke(action);

                        DialogResult questConnect = MessageBox.Show("Подключение потеряно", "", MessageBoxButtons.RetryCancel);
                        if (questConnect == DialogResult.Cancel)
                        {
                            //Environment.Exit(0);
                        }
                        else if (questConnect == DialogResult.Retry)
                        {
                            stateConnection = true;
                            connectSKU(statIpAdress, Convert.ToInt32(statPort));
                        }
                    }
                    else
                    {
                        //другие исключения
                    }
                }
                return(timeNow);
            }
        }
Пример #3
0
        public static void connectSKU(string ip, int port)
        {
            try
            {
                modbusClient = new ModbusClient(ip, port);
                //modbusClient.ConnectionTimeout = 50000;
                modbusClient.Connect();
                int[] zavnomer = new int[4];
                zavnomer    = modbusClient.ReadHoldingRegisters(3 - 1, 4);
                zavnomer[2] = 0;
                zavnomer[3] = 0;
                ulong zavnomer1 = Convert.ToUInt64(ModbusClient.ConvertRegistersToLong(zavnomer));
                MessageBox.Show("Подключение установлено. Заводской № " + zavnomer1.ToString());

                Action action = () => Instance.labelConnectStatus.Text = "Подключение установлено";
                Instance.labelConnectStatus.Invoke(action);

                action = () => Instance.ButtonConnectDevice.Enabled = false;
                Instance.ButtonConnectDevice.Invoke(action);
            }
            catch (ConnectionException ex)
            {
                if (ex.Message == "connection timed out")
                {
                    MessageBox.Show("Подключение не установленно. Время ожидания подключения истекло");
                }
                else
                {
                    MessageBox.Show(ex.Message);
                }
            }
            catch (SocketException)
            {
                {
                    MessageBox.Show("Не удалось подключиться к устройству.");
                }
            }
        }
Пример #4
0
        public static void RecordDBDataLogs()
        {
            List <int[]> logs    = new List <int[]>();
            LogLoader    logLoad = new LogLoader();
            //logs = logLoad.getLogs();
            int   NumberWorks;
            int   NumberBrigada;
            float MaxMoment;
            float MovingInResolitions;
            float MaxSpeed;
            float ShutoffValve;
            float DurationEvents;
            int   indexMaxNumberWorksDB  = getNetAndOldNumberWorks();
            long  indexNumberWorksDevice = logLoad.getNewbestRecordNumber();

            if (indexNumberWorksDevice > indexMaxNumberWorksDB)
            {
                if (indexMaxNumberWorksDB == 0)
                {
                    logs = logLoad.getLogs(indexMaxNumberWorksDB);
                }
                else
                {
                    logs = logLoad.getLogs(indexMaxNumberWorksDB + 1);
                    indexMaxNumberWorksDB++;
                }
                int[] sizelog = new int[logs.Count];
                for (int i = 0; i < logs.Count; i++)
                {
                    sizelog[i] = logs[i][15];
                    DateTime dateTime = new DateTime(1997, 1, 1);
                    int[]    time1    = new int[4];
                    time1[0]            = logs[i][2];
                    time1[1]            = logs[i][3];
                    NumberWorks         = i + indexMaxNumberWorksDB;
                    dateTime            = dateTime.AddDays(logs[i][0]).AddMilliseconds(ModbusClient.ConvertRegistersToLong(time1) * 10);
                    NumberBrigada       = logs[i][4];
                    MaxMoment           = ModbusClient.ConvertRegistersToFloat(ConvertToArray(logs[i][5], logs[i][6]));
                    MovingInResolitions = ModbusClient.ConvertRegistersToFloat(ConvertToArray(logs[i][7], logs[i][8]));
                    MaxSpeed            = ModbusClient.ConvertRegistersToFloat(ConvertToArray(logs[i][9], logs[i][10]));
                    ShutoffValve        = ModbusClient.ConvertRegistersToFloat(ConvertToArray(logs[i][11], logs[i][12]));
                    DurationEvents      = ModbusClient.ConvertRegistersToFloat(ConvertToArray(logs[i][13], logs[i][14]));
                    RecordInDbData("INSERT INTO Story (NumberWorks, DataTime, NumberBrigada, MaxMoment, MovingInResolitions, MaxSpeed, ShutoffValve, DurationEvents ) " +
                                   "VALUES ('" + NumberWorks + "','" + dateTime.ToString() + "','" + NumberBrigada + "','" + MaxMoment + "','" + MovingInResolitions + "','" + MaxSpeed + "','" + ShutoffValve + "','" + DurationEvents + "')");
                }
                logLoad.getDataGrafiksOfDeviceAsync(sizelog, indexMaxNumberWorksDB);
            }
        }
Пример #5
0
        private void FetchFromModbusServer()
        {
            if (ModbusHost.HasValue && ModbusAddress1.Value > 0)
            {
                int registerToRead;
                switch (DataType.Value)
                {
                case DataTypeEnum.INT32:
                case DataTypeEnum.FLOAT:
                    registerToRead = 2;
                    break;

                case DataTypeEnum.LONG:
                case DataTypeEnum.DOUBLE:
                    registerToRead = 4;
                    break;

                case DataTypeEnum.INT16_SIGNED:
                case DataTypeEnum.INT16_UNSIGNED:
                default:
                    registerToRead = 1;
                    break;
                }
                ModbusClient.RegisterOrder regOrder;
                if (!RegisterOrder.HasValue || RegisterOrder.Value == ByteOrderEnum.LOW_HIGH)
                {
                    regOrder = ModbusClient.RegisterOrder.LowHigh;
                }
                else
                {
                    regOrder = ModbusClient.RegisterOrder.HighLow;
                }
                ModbusClient modbusClient = null;
                try
                {
                    modbusClient = new ModbusClient(ModbusHost.Value, ModbusPort.Value);
                    modbusClient.ConnectionTimeout = 5000;
                    modbusClient.Connect();
                    modbusClient.UnitIdentifier = (byte)ModbusID.Value;

                    int[] readHoldingRegisters;
                    switch (FunctionCode.Value)
                    {
                    case FunctionCodeEnum.FC_04:
                        readHoldingRegisters = modbusClient.ReadInputRegisters(ModbusAddress1.Value, registerToRead);
                        break;

                    case FunctionCodeEnum.FC_03:
                    default:
                        readHoldingRegisters = modbusClient.ReadHoldingRegisters(ModbusAddress1.Value, registerToRead);
                        break;
                    }

                    double result     = 0;
                    string result_str = "";

                    switch (DataType.Value)
                    {
                    case DataTypeEnum.INT32:
                        // probably signed ...
                        result = ModbusClient.ConvertRegistersToInt(readHoldingRegisters, regOrder);
                        break;

                    case DataTypeEnum.FLOAT:
                        result = ModbusClient.ConvertRegistersToFloat(readHoldingRegisters, regOrder);
                        break;

                    case DataTypeEnum.LONG:
                        result = ModbusClient.ConvertRegistersToLong(readHoldingRegisters, regOrder);
                        break;

                    case DataTypeEnum.DOUBLE:
                        result = ModbusClient.ConvertRegistersToDouble(readHoldingRegisters, regOrder);
                        break;

                    case DataTypeEnum.INT16_SIGNED:
                        result = readHoldingRegisters[0];
                        break;

                    case DataTypeEnum.INT16_UNSIGNED:
                        // unsigned
                        for (int i = 0; i < (readHoldingRegisters.Length); i++)
                        {
                            int tmp = readHoldingRegisters[i];
                            if (tmp == -32768)     // fix for 0x00
                            {
                                tmp = 0;
                            }
                            if (tmp < 0)     // no negative values !
                            {
                                tmp = tmp + (int)Math.Pow(2, 16);
                            }

                            result     = result + (tmp * Math.Pow(2, (16 * ((readHoldingRegisters.Length) - (i + 1)))));
                            result_str = result_str + " 0x" + tmp.ToString("X4");
                        }
                        break;

                    default:
                        result_str = "internal: invalid datatype";
                        break;
                    }

                    OutputValue1.Value = result;
                    ErrorMessage.Value = result_str;
                    this.SchedulerService.InvokeIn(new TimeSpan(0, 0, TimeSpan.Value), FetchFromModbusServer);
                }
                catch (Exception e)
                {
                    this.ErrorMessage.Value = e.ToString();
                    this.SchedulerService.InvokeIn(new TimeSpan(0, 1, 0), FetchFromModbusServer);
                }
                finally
                {
                    if (modbusClient != null)
                    {
                        modbusClient.Disconnect();
                    }
                }
            }
        }