static void Main(string[] args) { ModbusTCP modbus = new ModbusTCP("192.168.0.115", 502); modbus.OnException += Modbus_OnException; modbus.OnResponseData += Modbus_OnResponseData; modbus.ReadCoilsAsync(1, 1, 0, 2); var conn = modbus.Connected; ushort start = 16; ushort lenght = 2; // calculate the number of bytes data needs int numBytes = (lenght / 8 + (lenght % 8 > 0 ? 1 : 0)); byte[] data = new byte[numBytes]; byte[] multipleCoils = new byte[lenght]; multipleCoils[0] = 255; multipleCoils[1] = 255; // write data to device byte[] result = new byte[numBytes]; modbus.WriteMultipleCoils(1, 1, start, lenght, multipleCoils, ref result); //modbus.WriteSingleCoil(1, 1, start, false, ref result); //read data from device modbus.ReadHoldingRegisters(1, 1, start, lenght, ref data); // convert data into int value int length2 = data.Length / 2 + Convert.ToInt16(data.Length % 2 > 0); var word = new int[length2]; for (int x = 0; x < length2; x++) { word[x] = data[x * 2] * 256 + data[x * 2 + 1]; Console.WriteLine(word[x]); } //convert data into bit value BitArray bitArray = new BitArray(data); int[] numbers = new int[bitArray.Count]; for (int i = 0; i < bitArray.Length; i++) { numbers[i] = bitArray[i] ? 1 : 0; Console.WriteLine($"Coil {i} : {numbers[i]}"); } //var data1 = Convert.ToBoolean(Convert.ToByte("3")); //var coils = Convert.ToString(data[0],2); //var reversed = coils.ToArray(); //Array.Reverse(reversed); //foreach (var item in reversed) //{ // Console.WriteLine(item); //} modbus.OnException -= Modbus_OnException; modbus.OnResponseData -= Modbus_OnResponseData; modbus.Disconnect(); Console.ReadLine(); }
public void ReadPLCData() { if (!WriteAktif) { if (HetModbusTool.Connected) { try { ReadBooleanFromPLC = HetModbusTool.ReadCoils(0, 1999); AlarmFromPLC = HetModbusTool.ReadCoils(0, 1000); ReadWordFromPLCPart0 = HetModbusTool.ReadHoldingRegisters(0, 100); ReadWordFromPLCPart1 = HetModbusTool.ReadHoldingRegisters(100, 100); ReadWordFromPLCPart2 = (HetModbusTool.ReadHoldingRegisters(200, 100)); ReadWordFromPLCPart3 = HetModbusTool.ReadHoldingRegisters(300, 100); ReadWordFromPLCPart4 = HetModbusTool.ReadHoldingRegisters(400, 100); ReadWordFromPLCPart5 = HetModbusTool.ReadHoldingRegisters(500, 100); ReadWordFromPLCPart6 = HetModbusTool.ReadHoldingRegisters(600, 100); ReadWordFromPLCPart7 = HetModbusTool.ReadHoldingRegisters(700, 100); ReadWordFromPLCPart8 = HetModbusTool.ReadHoldingRegisters(800, 100); ReadWordFromPLCPart9 = HetModbusTool.ReadHoldingRegisters(900, 100); ReadWordFromPLCPart10 = HetModbusTool.ReadHoldingRegisters(1000, 100); ReadWordFromPLCPart20 = HetModbusTool.ReadHoldingRegisters(2000, 100); R006_UretimValue = HetModbusTool.ReadHoldingRegisters(R006Start_Adress, R006_Quantity); R006_UretimValueBool = HetModbusTool.ReadCoils(4202, 14); MSVU_Sanal = HetModbusTool.ReadHoldingRegisters(M101_StartAdress_VerilenUrunMiktarlari_SCADA_Gonderilen, 80); MSTI_Sanal = HetModbusTool.ReadHoldingRegisters(M101_StartAdress_ToplamIstenen_SCADA_Gonderilen, 80); MSMUM_Sanal = HetModbusTool.ReadHoldingRegisters(M101_StartAdress_ManuelUretimMiktarlari, 80); MSYM_Sanal = HetModbusTool.ReadHoldingRegisters(M101_StartAdress_YariOtoUretimMiktarlari, 80); SanalMemeory_ZamanTarihDizisi = HetModbusTool.ReadHoldingRegisters(M101_StartAdress_s_ZamanTarihDizisi, 4); } catch (Exception) { } } if (HetModbusTool.Connected) { Read_M001_Secimler.Read_M001Secimler(ReadBooleanFromPLC); Read_M002_YariOto.Read_M002YariOto(ReadBooleanFromPLC); Read_M003_Manueller.Read_M003Manueller(ReadBooleanFromPLC, ReadWordFromPLCPart3, ReadWordFromPLCPart4); Read_M004_Parametreler.ReadM004Parametreler(ReadBooleanFromPLC, ReadWordFromPLCPart3); Read_M005_Modbus.ReadM005Modbus(ReadBooleanFromPLC, ReadWordFromPLCPart4, ReadWordFromPLCPart5, ReadWordFromPLCPart20); Read_M006_Uretim.ReadM006Uretim(ReadBooleanFromPLC, ReadWordFromPLCPart3, ReadWordFromPLCPart5); Read_M007_Cfg.ReadM007Cfg(ReadBooleanFromPLC); Read_M008_CI522.ReadM008CI522(ReadBooleanFromPLC); Read_M009_HMI.ReadM009HMI(ReadBooleanFromPLC); Read_M010_Alarm.ReadM010Alarm(AlarmFromPLC); Read_M011_Butonlar.ReadM011Butonlar(ReadBooleanFromPLC); Read_M012_Lambalar.ReadM012Lambalar(ReadBooleanFromPLC); Read_M013_KantarAnimasyon.ReadM013KantarAnimasyon(ReadWordFromPLCPart0, ReadWordFromPLCPart3); Read_M014_Genel.ReadM14Genel(ReadWordFromPLCPart0, ReadWordFromPLCPart3); Read_M015_Agrega.ReadM15Agrega(ReadWordFromPLCPart1, ReadWordFromPLCPart5); Read_M016_Cimento.ReadM016Cimento(ReadWordFromPLCPart2); Read_M017_Su.ReadM017Su(ReadWordFromPLCPart2); Read_M018_Katki.ReadM018Katki(ReadWordFromPLCPart4); Read_M101_Array.Read_M101Array(MSVU_Sanal, MSTI_Sanal, MSMUM_Sanal, MSYM_Sanal, SanalMemeory_ZamanTarihDizisi); } } }