Exemple #1
0
        private void ProceedModbusDiagnosticData(EasyModbusItem item)
        {
            Debug.Assert(item.RxBuffer[7] == item.FunctionCode);
            Debug.Assert(item.RxBuffer[8] >= (byte)2);
            ushort returnValue = item.RxBuffer[9];

            returnValue <<= 8;
            returnValue  += item.RxBuffer[10];
            // refer to IL ETH BK DI8 DO4 2TX-PAC .pdf documentation from Hardware Vendor
            switch (returnValue)                // base on section 15.7.1 Register 7996: status register
            {
            case 1:                             // good, no error --
                FlatAPIForDDI.DataArray[1] = 0; // set shared memory, no error.
                break;

            case 0:
            case 2:
                // Need to read more data
                AddItemToProcess(new EasyModbusItem(3, 7997, 1));
                FlatAPIForDDI.DataArray[1] = 1;    // set shared memory, error 1.
                break;

            case 3:                             // Net Fail occur
                AddItemToProcess(new EasyModbusItem(3, 2004, 1));
                FlatAPIForDDI.DataArray[1] = 3; // set shared memory, error 3.
                break;

            default:
                if (item.ShouldRetry)
                {
                    AddItemToProcess(item);
                }
                break;
            }
        }
Exemple #2
0
        private void UpdateSendData(object sender)
        {
            var            modbusClientObj = (ModbusClient)sender;
            EasyModbusItem item            = new EasyModbusItem(modbusClientObj.sendData);

            _ReceiveItems.Add(item);
        }
 public void Add(EasyModbusItem data)
 {
     if (data == null)
     {
         throw new ArgumentNullException("data");
     }
     lock (_HashSet)
     {
         _HashSet.Add(data);
         _iCount++;
         Monitor.Pulse(_HashSet);
         Trace.WriteLine(string.Format("_HashSet Rx Add {0}", data));
     }
 }
        public EasyModbusItem RemoveFirstItem()
        {
            lock (_HashSet)
            {
                while (_iCount <= 0)
                {
                    Monitor.Wait(_HashSet);
                }

                _iCount--;
                var currentItem = _HashSet.First();
                var item        = new EasyModbusItem(currentItem.TxBuffer);
                item.RxBuffer = currentItem.RxBuffer;
                _HashSet.Remove(currentItem);
                Trace.WriteLine(string.Format("_HashSet Remove First {0} {1}", currentItem.FunctionCode, currentItem.StartingAddress));
                return(item);
            }
        }
Exemple #5
0
 public void AddItemToProcess(EasyModbusItem item)
 {
     //Make sure the process thread is alive first
     if (_TaskKeepAliveModbusClient != null && _TaskKeepAliveModbusClient.IsAlive)
     {
         //Push one element.
         _SendItems.Enqueue(item);
         return;
     }
     else
     {
         if (!_aExceptions.Peek().Message.Equals(_strProcessThreadNotStartedYet))
         {
             Trace.WriteLine("***Should show once***" + _strProcessThreadNotStartedYet);
             _aExceptions.Push(new Exception(_strProcessThreadNotStartedYet));
         }
     }
 }
Exemple #6
0
        private static void GetModbusExceptionResponse(EasyModbusItem item, int returnValue, StringBuilder str)
        {
            switch (returnValue)// Modbus exception response code
            {
            case 1:
                str.Append("The Function Code is unknown to the Server");
                break;

            case 2:
                str.Append("Illegal Address or Quantity");
                break;

            case 3:
                str.Append("Illegal Data");
                break;

            case 4:
                str.Append("The server failed during the execution");
                break;

            case 5:
                str.Append("The server accepted the service invocation but the service requires a relatively long time to execute. The server therefore returns only an acknowledgement of the service invocation receipt.");
                break;

            case 6:
                str.Append("The server was unable to accept the MB Request PDU. The client application has the responsibility of deciding if and when to re-send the request.");
                break;

            case 10:
                str.Append("Gateway paths not available.");
                break;

            case 11:
                str.Append("The targeted device failed to respond. The gateway generates this exception.");
                break;

            default:
                str.AppendFormat("Default case: Unknown data code, message returned form server is: {0}", new SoapHexBinary(item.RxBuffer));
                break;
            }
        }
 public void UpdateWithReturnedValues(EasyModbusItem currentEasyModbusItem, int[] ReturnedData)
 {
     if (currentEasyModbusItem == null)
     {
         throw new ArgumentNullException("CurrentEasyModbusItem");
     }
     if (ReturnedData == null)
     {
         throw new ArgumentNullException("ReturnedData");
     }
     lock (_HashSet)
     {
         _HashSet.Remove(currentEasyModbusItem);
         Trace.WriteLine(string.Format("_HashSet Remove {0} {1}", currentEasyModbusItem.FunctionCode, currentEasyModbusItem.StartingAddress));
         var itemToAdd = new EasyModbusItem(currentEasyModbusItem.TxBuffer);
         itemToAdd.DataSource = ReturnedData;
         _HashSet.Add(itemToAdd);
         Trace.WriteLine(string.Format("_HashSet Add {0} {1} -{2}", currentEasyModbusItem.FunctionCode, currentEasyModbusItem.StartingAddress, ReturnedData[0]));
         Monitor.Pulse(_HashSet);
     }
 }
 public void UpdateWithRxData(EasyModbusItem currentEasyModbusItem, byte[] rxData)
 {
     if (currentEasyModbusItem == null)
     {
         throw new ArgumentNullException("CurrentEasyModbusItem");
     }
     if (rxData == null)
     {
         throw new ArgumentNullException("RxData");
     }
     lock (_HashSet)
     {
         _HashSet.Remove(currentEasyModbusItem);
         Trace.WriteLine(string.Format("_HashSet Rx Remove {0} {1}", currentEasyModbusItem.FunctionCode, currentEasyModbusItem.StartingAddress));
         var itemToAdd = new EasyModbusItem(currentEasyModbusItem.TxBuffer);
         itemToAdd.RxBuffer = rxData;
         _HashSet.Add(itemToAdd);
         Trace.WriteLine(string.Format("_HashSet Rx Add {0} {1} -{2}{3}", currentEasyModbusItem.FunctionCode, currentEasyModbusItem.StartingAddress, rxData[0], rxData[1]));
         Monitor.Pulse(_HashSet);
     }
 }
Exemple #9
0
        private void UpdateReceiveData(object sender)
        {
            var modbusClientObj = (ModbusClient)sender;

            if (modbusClientObj != null)
            {
                EasyModbusItem item = new EasyModbusItem(modbusClientObj.sendData);
                _ReceiveItems.UpdateWithRxData(item, modbusClientObj.receiveData);
                if (_TaskProcessReceivedData == null)
                {
                    _TaskProcessReceivedData      = new Thread(ProcessReceivedData);
                    _TaskProcessReceivedData.Name = "EasyModbusWrapper_ProcessReceivedData";
                    _TaskProcessReceivedData.Start();
                }
                Trace.WriteLine(new SoapHexBinary(modbusClientObj.receiveData).ToString());
                if ((modbusClientObj.receiveData[0] & 0x01) == 0x01)
                {
                    return;
                }
            }
            //throw new NotImplementedException();
        }
Exemple #10
0
        private void ProcessWriteSingleRegister(EasyModbusItem item)
        {
            if (item.TxBuffer.SequenceEqual(item.RxBuffer))// This is successful written.
            {
                return;
            }
            int           usFunctionCodeWithError = Convert.ToInt32(item.RxBuffer[7]);
            int           returnValue             = Convert.ToInt32(item.RxBuffer[8]);
            StringBuilder str = new StringBuilder("Write Register Error:");

            str.AppendFormat("FC {0} Address:{1} Quantity: {2} returned Value: {3}.\n", item.FunctionCode, item.StartingAddress, item.Quantity, returnValue);
            str.AppendLine();
            // refer to IL ETH BK DI8 DO4 2TX-PAC .pdf documentation from Hardware Vendor
            if ((usFunctionCodeWithError & 0x0000008F) == (item.FunctionCode | 0x00000080))
            {
                GetModbusExceptionResponse(item, returnValue, str);
            }
            else
            {
                str.AppendFormat("Unknown data code, message returned form server is: {0}", new SoapHexBinary(item.RxBuffer));
            }
            _aExceptions.Push(new Exception(str.ToString()));
        }
 public EasyModbusItem(EasyModbusItem rhs) : this(rhs.FunctionCode, rhs.StartingAddress, rhs.Quantity, rhs.DataSource)
 {
     this.RxBuffer = rhs.RxBuffer;
     this.TxBuffer = rhs.TxBuffer;
 }