public void AddressWithInterfaceTest() { IntbusDevice intbusDevice = new IntbusDevice(new UART0(), 5); int expected = 37; int actual = intbusDevice.AddressWithInterface; Assert.AreEqual(expected, actual); }
public void ConvertToIntbusFrameTest() { IntbusDevice intbusDevice = new IntbusDevice(new UART0(), address: 1); IntbusDevice intbusDeviceSlave = new IntbusDevice(new OWI(), address: 1); intbusDevice.AddIntbusDevice(intbusDeviceSlave); List <byte> modbusFrame = new List <byte> { 0x01, 0x04, 0x02, 0x12, 0x34, 0xB4, 0x47 }; List <byte> expected = new List <byte> { 0x21, 0xA1, 0x01, 0x04, 0x02, 0x12, 0x34, 0xA3, 0x34 }; List <byte> actual = intbusDeviceSlave.ConvertToIntbusFrame(modbusFrame); CollectionAssert.AreEqual(expected, actual); }
public void ConvertToIntbusFrameWithPrePostfixBytesTest() { IntbusDevice intbusDevice = new IntbusDevice(new SPI(), address: 1); intbusDevice.PrefixBytes.Add(0xFF); intbusDevice.PostfixBytes.Add(0xFF); List <byte> modbusFrame = new List <byte> { 0x01, 0x04, 0x00, 0x05, 0x00, 0x01, 0x00, 0x00 }; List <byte> expected = new List <byte> { 0xFF, 0x61, 0x01, 0x04, 0x00, 0x05, 0x00, 0x01, 0x4A, 0x16, 0xFF }; List <byte> actual = intbusDevice.ConvertToIntbusFrame(modbusFrame); CollectionAssert.AreEqual(expected, actual); }
private void MbPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { int byteRecieved = 0; byte[] receivedBytes = null; int mbAddress = 0xFFFF; string message = null; List <byte> intbusFrame; try { byteRecieved = (sender as SerialPort).BytesToRead; receivedBytes = new byte[byteRecieved]; (sender as SerialPort).Read(receivedBytes, 0, byteRecieved); mbAddress = receivedBytes.First(); if (!this.modbusAddressDictionary.ContainsKey(mbAddress)) { throw new Exception($"Ошибка запрашиваемый адрес({mbAddress}) modbus не существует " + $"в Intbus пространстве"); } this.currentDevice = this.modbusAddressDictionary[mbAddress]; intbusFrame = this.currentDevice.ConvertToIntbus(receivedBytes.ToList()); if (intbusFrame == null) { throw new Exception($"Exception: {this.currentDevice.Name} ConvertToIntbus: " + $"{BitConverter.ToString(intbusFrame.ToArray())}"); } } catch (Exception ex) { this.WriteLog(ex.Message); this.WriteLog(ex.StackTrace); return; } if (this.IntPort != null && this.IntPort.IsOpen) { message = null; List <byte> currentPreambule = this.currentDevice.CalculatePreambule(); for (int i = currentPreambule.Count; i < intbusFrame.Count; i++) { message += String.Format("{0:X2} ", intbusFrame[i]); } ///////////////////////////////// receivedBytes = intbusFrame.ToArray(); this.IntPort.DiscardOutBuffer(); this.IntPort.DiscardInBuffer(); this.IntPort.Write(receivedBytes, 0, receivedBytes.Length); while (this.IntPort.BytesToWrite != 0) { ; } this.sendTime = DateTime.Now; if (this.timeoutTask != null) { cancelTimeoutToken.Cancel(); this.timeoutTask.Wait(); cancelTimeoutToken.Dispose(); cancelTimeoutToken = new CancellationTokenSource(); this.timeoutTaskToken = cancelTimeoutToken.Token; } ; this.timeoutTask = Task.Factory.StartNew(() => { while (true) { if (this.timeoutTaskToken.IsCancellationRequested) { return; } if (DateTime.Now - sendTime >= TimeSpan.FromMilliseconds(this.responseTimeout)) { byteRecieved = this.IntPort.BytesToRead; byte[] responseBytes = new byte[byteRecieved]; this.IntPort.Read(responseBytes, 0, byteRecieved); this.intbusRecievedBuffer.AddRange(responseBytes); if (this.intbusRecievedBuffer.Count == 0) { this.SerialDataIntBUSResponse("TIMEOUT, NO RESPONSE"); } else { this.SerialDataIntBUSResponse("TIMEOUT, CRC ERROR"); } return; } } }); string preambule = BitConverter.ToString(currentDevice.CalculatePreambule().ToArray()).Replace('-', ' '); this.WriteLog($"{this.currentDevice.Name} TX: " + $"[{preambule}] {message}"); } }