private void timer1_Tick(object sender, EventArgs e) { //Connect to Server if (modbusClient.Connected && modbusClient.Available(1)) { try { int[] readHoldingRegisters = modbusClient.ReadHoldingRegisters(0, 10); //Read 10 Holding Registers from Server, starting with Address 1 var new_text = String.Join(" - ", readHoldingRegisters); if (old_registers != new_text) { old_registers = new_text; var to_print = readHoldingRegisters[0]; if (to_print == 1) { var number = readHoldingRegisters[1].ToString(); listBox1.SelectedIndex = -1; for (int i = 0; i < listBox1.Items.Count; i++) { if (listBox1.Items[i].ToString().StartsWith(number)) { listBox1.SelectedIndex = i; var selected_prn = listBox1.Items[i].ToString(); var selected_bin = Path.ChangeExtension(selected_prn, "bin"); listBox2.Items.Add("printing " + selected_bin); var text_to_print = File.ReadAllText(trabajo_folder + "\\" + selected_bin); RawPrinterHelper.SendStringToPrinter(printerName.Text, text_to_print); print_count++; label7.Text = print_count.ToString(); } } } listBox2.Items.Add(new_text); } } catch (Exception ex) { plcStatus.Text = ex.Message; modbusClient.Disconnect(); } } else { modbusClient.Disconnect(); connect_to_plc(); } }
public override void Initialize() { base.Initialize(); try { _modbusClient = new ModbusClient(PortSetting.CommPort.ToString()); _modbusClient.Baudrate = (int)PortSetting.BaudRate; _modbusClient.StopBits = PortSetting.StopBits; _modbusClient.Parity = PortSetting.Parity; _modbusClient.ConnectionTimeout = (int)PortSetting.ReadWriteTimeOut; _modbusClient.Available((int)PortSetting.ReadWriteTimeOut); _modbusClient.Connect(); if (!_modbusClient.Connected) { throw new ForceSimulateException(string.Format("Port {0} is not opened", PortSetting.CommPort.ToString())); } CheckStatus(2000); U.RegisterOnChanged(() => IsMoveSFTY, IsMoveSFTY_OnChanged); Simulate = eSimulate.None; } catch (Exception ex) { _modbusClient.Disconnect(); //throw new ForceSimulateException(ex); U.LogAlarmPopup(ex, this.Nickname); this.Simulate = eSimulate.SimulateDontAsk; } _updateLoop = new BackgroundWorker(); _updateLoop.DoWork += new DoWorkEventHandler(UpdateLoop); }
private static async Task Listen() { while (true) { if (BarcodeClient.Available(100)) { ReadHoldingRegsBarcode(BarcodeClient); if (_cancelBarcode.IsCancellationRequested) { return; } await Task.Delay(100, CancellationToken.None); } else { ConnectBarcode(); var args = EventArgs.Empty; OnBarcodeConnectionChanged(args); return; } } // ReSharper disable once FunctionNeverReturns }