public void Start() { //Метод производит подключение к котроллеру и устанавливает связь //Если соединение успешно то вызывает поток-таймеры. и внутри них выполнение действия по таймеру. try { buffer = new byte[amount]; bufferPLC = new byte[amount]; bufferSQL = new byte[amount]; //запуск таймеров 100ms(100ms), 101ms(SQL), 200ms(message), 1000ms(1s) stan = new Prodave(); int res = stan.LoadConnection(Connect, 2, conn, slot, rack); if (res != 0) { LogSystem.Write(name + " start", Direction.ERROR, "Error connection!. Error - " + stan.Error(res), ConnectCurX, ConnectCurY, true); } else { //LogSystem.Write(name+" start", Direction.Ok, "Connect OK!", ConnectCurY, ConnectCurY, true); int resSAC = stan.SetActiveConnection(Connect); if (resSAC == 0) { LogSystem.Write(name + " start", Direction.Ok, "Соединение активно.", ConnectCurY, ConnectCurY, true); CreateTable(); //В случае успешного подключения к контроллеру формируем таблицу для формирования данных и последующего сохранения в БД TTimer100ms = new Timer(new TimerCallback(TicTimer100ms), null, 0, 100); if (plctodbmessage) { TTimerMessage = new Timer(new TimerCallback(TicTimerMessage), null, 0, 200); } if (plctodb101ms) { TTimerSQL = new Timer(new TimerCallback(TicTimerSQL), null, 0, 101); } if (plctodb1s) { TTimer1s = new Timer(new TimerCallback(TicTimer1s), null, 0, 1000); } } else { LogSystem.Write(name + " start", Direction.WARNING, "Соединение не активировано. " + stan.Error(resSAC), ConnectCurY, ConnectCurY, true); } } } catch (Exception ex) { /*все исключения кидаем в пустоту*/ LogSystem.Write(name + " start-" + ex.Source, Direction.ERROR, "Start Error-" + ex.Message, ConnectCurY, ConnectCurY, true); } }
private void btnSave_Click(object sender, RoutedEventArgs e) { //TODO запуск таймеров 100ms(100ms), 101ms(SQL), 200ms(message), 1000ms(1s) stan = new Prodave(); byte[] conn = new byte[] { 192, 168, 0, 11 }; int res = stan.LoadConnection(Connect, 2, conn, 3, 0); if (res != 0) { Console.WriteLine("Error connection! " + stan.Error(res)); LogSystem.WriteEventLog("ProDaveStan", "Test", "Error connection!. Error - " + stan.Error(res), EventLogEntryType.Error); } else { LogSystem.WriteEventLog("ProDaveStan", "Test", "Connect OK!", EventLogEntryType.Information); int resSAC = stan.SetActiveConnection(Connect); if (resSAC == 0) { Console.WriteLine("Соединение активно."); LogSystem.WriteEventLog("ProDaveStan", "Test", "Соединение активно.", EventLogEntryType.Information); //Connect100ms(); TTimer100ms = new Timer(new TimerCallback(TicTimer100ms), null, 0, 100); TTimerMessage = new Timer(new TimerCallback(TicTimerMessage), null, 0, 200); TTimerSQL = new Timer(new TimerCallback(TicTimerSQL), null, 0, 101); TTimer1s = new Timer(new TimerCallback(TicTimer1s), null, 0, 1000); } else { Console.WriteLine("Соединение не активировано. " + stan.Error(resSAC)); LogSystem.WriteEventLog("ProDaveStan", "Test", "Соединение не активировано. " + stan.Error(resSAC), EventLogEntryType.Error); System.Diagnostics.Debug.WriteLine("Error - Соединение не активировано."); } } }
private void PLC() { try { int i = curTop; //начальная позиция по Top int y = curTop + 2; //Конечная позиция по Top Prodave rs2 = new Prodave(); buffer = new byte[amount]; bufferPLC = new byte[amount]; bufferSQL = new byte[amount]; int resultReadField = 5; while (true) { Thread.Sleep(100); if (resultReadField != 0) { int res = rs2.LoadConnection(Connect, 2, conn, slot, rack); if (res != 0) { //Console.WriteLine("error" + rs2.Error(res)); LogSystem.Write(namePLC + " start", Direction.ERROR, "Error connection!. Error - " + rs2.Error(res), curLeft, i, true); } else { int resSAC = rs2.SetActiveConnection(Connect); } } int Byte_Col_r = 0; resultReadField = rs2.field_read('M', 0, startBuffer, amount, out buffer, out Byte_Col_r); if (resultReadField == 0) { //LogSystem.Write(namePLC + " start", Direction.Ok, "Соединение активно.", curLeft, (i+1), true); //Буфер PLC Thread PLS100ms = new Thread(BufferToBuffer); PLS100ms.Start(); //Буфер SQL 100mc Thread PLS101ms = new Thread(BufferSQLToBufferPLC); PLS101ms.Start(); //Буфер сообщений //Буфер 1с } else { rs2.UnloadConnection(Connect); LogSystem.Write(namePLC + " 100ms", Direction.ERROR, "Error.Read fied PLC. " + rs2.Error(resultReadField), curLeft, i, true); } } //if (i < y) //{ // i = i + 1; // Console.SetCursorPosition(curLeft, i); // Console.Write(namePLC + " 100ms - " + DateTime.Now.ToString("HH:mm:ss.fff")); //} //else //{ // i = curTop; // Console.SetCursorPosition(curLeft, i); // Console.Write(namePLC + " 100ms - " + DateTime.Now.ToString("HH:mm:ss.fff")); //} } catch (Exception ex) { /*все исключения кидаем в пустоту*/ LogSystem.Write(namePLC + " start-" + ex.Source, Direction.ERROR, "Start Error-" + ex.Message, curLeft, curTop, true); } }
private void PLC() { try { int i = 100; //начальная позиция по Top int y = 2; //Конечная позиция по Top Prodave rs2 = new Prodave(); buffer = new byte[amount]; bufferPLC = new byte[amount]; bufferSQL = new byte[amount]; int resultReadField = 5; while (true) { Thread.Sleep(100); if (resultReadField != 0) { int res = rs2.LoadConnection(connect, 2, IPconnPLC, SlotconnPC, RackconnPC); if (res != 0) { //Console.WriteLine("error" + rs2.Error(res)); LogSystem.Write(NamePLC + " start", Direction.ERROR, "Error connection!. Error - " + rs2.Error(res), 100, 0, true); } else { int resSAC = rs2.SetActiveConnection(connect); } } int Byte_Col_r = 0; resultReadField = rs2.field_read('M', 0, StartAdressTag, amount, out buffer, out Byte_Col_r); if (resultReadField == 0) { //LogSystem.Write(NamePLC + " start", Direction.Ok, "Соединение активно.", 100, 1, true); //Буфер PLC Thread PLS100ms = new Thread(BufferToBuffer); PLS100ms.Start(); //Буфер SQL 100mc Thread PLS101ms = new Thread(BufferSQLToBufferPLC); PLS101ms.Start(); //Буфер сообщений //Буфер 1с } else { rs2.UnloadConnection(connect); LogSystem.Write(NamePLC + " 100ms", Direction.ERROR, "Error.Read fied PLC. " + rs2.Error(resultReadField), 100, 0, true); } } } catch (Exception ex) { /*все исключения кидаем в пустоту*/ LogSystem.Write(NamePLC + " start-" + ex.Source, Direction.ERROR, "Start Error-" + ex.Message, 100, 0, true); } }