private void SetAllValuesToCounter(Counter selectedCounter) { const int maxPackageLenght = 20; bool sentSuccessful; byte[] packageToSlave = new byte[maxPackageLenght]; byte[] packageFromSlave = new byte[maxPackageLenght]; packageToSlave = TransferData.MakePackage(selectedCounter.CounterAddress, (byte)ModbusCommand.SetNumberOfPulses, (byte)(selectedCounter.CounterNumber - 1), selectedCounter.SetCounterValue); //packageToSlave = TransferData.MakePackage(slaveAddress, (byte)ModbusCommand.FindDevices); sentSuccessful = TransferData.SendPackage(selectedCounter.UsedSerialPort, packageToSlave); }
private void FindDevice() { const byte commandPosition = 1; const int maxPackageLenght = 20; const int deviceTypePosition = 3; byte deviceNumber = 1; bool sentSuccessful; if (!serialPort.IsOpen) { serialPort.Open(); } for (byte slaveAddress = startAddress; slaveAddress < endAddress; ++slaveAddress) { byte[] packageToSlave = new byte[maxPackageLenght]; byte[] packageFromSlave = new byte[maxPackageLenght]; packageToSlave = TransferData.MakePackage(slaveAddress, (byte)ModbusCommand.FindDevices); sentSuccessful = TransferData.SendPackage(serialPort, packageToSlave); if (TransferData.ReadAnswer(serialPort, packageFromSlave)) { if (packageFromSlave[commandPosition] == (byte)ModbusCommand.FindDevices) { if (AllSlaveDevices != null) { AllSlaveDevices.Add(new FoundSlaveDevice(slaveAddress, packageFromSlave[deviceTypePosition], deviceNumber)); } else { AllSlaveDevices = new List <FoundSlaveDevice> { new FoundSlaveDevice(slaveAddress, packageFromSlave[deviceTypePosition], deviceNumber) }; } deviceNumber++; } devicesFound = true; } } serialPort.Close(); }
private void FindDevice() { const byte commandPosition = 1; foreach (string portName in availablePortNames) { byte[] packageToSlave = new byte[20]; byte[] packageFromSlave = new byte[20]; if (usedSerialPort.IsOpen) { usedSerialPort.Close(); } usedSerialPort.PortName = portName; if (!usedSerialPort.IsOpen) { usedSerialPort.Open(); usedSerialPort.Close(); usedSerialPort.Open(); } packageToSlave = TransferData.MakePackage(converterAddress, (byte)ModbusCommand.FindMaster); TransferData.SendPackage(usedSerialPort, packageToSlave); if (TransferData.ReadAnswer(usedSerialPort, packageFromSlave)) { if (packageFromSlave[commandPosition] == (byte)ModbusCommand.FindMaster) { converterFounded = true; } } if (usedSerialPort.IsOpen) { usedSerialPort.Close(); } } }