/// <summary> /// Event with callend when raced a Fetch-Event by a other Peer. /// For testing it must be filled with pseudo data be tested in the UNIT tests. /// </summary> /// <param name="data"></param> public void OnFetchData(JToken data) { string path = data["path"].ToString(); string Event = data["Event"].ToString(); lock (_dataBuffer) { switch (Event) { case "add": _dataBuffer.Add(path, data["value"]); break; case "fetch": _dataBuffer[path] = data["value"]; break; case "change": _dataBuffer[path] = data["value"]; break; } BusActivityDetection?.Invoke(this, new LogEvent(data.ToString())); } }
public void Write(object index, int data) { this.command = data; _master.WriteSingleRegister((ushort)Convert.ToInt32(index), (ushort)data); BusActivityDetection?.Invoke(this, new LogEvent("Data(ushort) have been written successfully to the register")); }
public ushort arr2; // For test purpose public void WriteArray(ushort index, ushort[] data) { this.arr1 = data[0]; this.arr1 = data[1]; _master.WriteMultipleRegisters(index, data); BusActivityDetection?.Invoke(this, new LogEvent("Data(ushort array) have been written successfully to multiple registers")); }
private void OnSet(bool success, JToken token) { if (!success) { JetBusException exception = new JetBusException(token); _mException = new Exception(exception.Error.ToString()); } _mSuccessEvent.Set(); BusActivityDetection?.Invoke(this, new LogEvent("Set data" + success)); }
private void OnSet(/*bool success, */ JToken token) { /* * if (!success) * { * JetBusException exception = new JetBusException(token); * _mException = new Exception(exception.Error.ToString()); * } */ _mSuccessEvent.Set(); BusActivityDetection?.Invoke(this, new LogEvent("Set data" + true)); }
/// <summary> /// Event with callend when raced a Fetch-Event by a other Peer. /// </summary> /// <param name="data"></param> protected virtual void OnFetchData(JToken data) { string path = data["path"].ToString(); int i = 0; lock (_dataJTokenBuffer) { switch (data["event"].ToString()) { case "add": _dataJTokenBuffer.Add(path, data["value"]); if (int.TryParse(data["value"].ToString(), out i)) // checks if the data is a number, which can be converted to an integer at the path. { _dataIntegerBuffer.Add(path, Convert.ToInt32(data["value"].ToString())); } break; case "fetch": _dataJTokenBuffer[path] = data["value"]; if (int.TryParse(data["value"].ToString(), out i)) // checks if the data is a number, which can be converted to an integer at the path. { _dataIntegerBuffer[path] = Convert.ToInt32(data["value"].ToString()); } break; case "change": _dataJTokenBuffer[path] = data["value"]; if (int.TryParse(data["value"].ToString(), out i)) // checks if the data is a number, which can be converted to an integer at the path. { _dataIntegerBuffer[path] = Convert.ToInt32(data["value"].ToString()); } break; } this.ConvertJTokenToStringArray(); RaiseDataEvent?.Invoke(this, new DataEvent(DataUshortArray, DataStrArray)); BusActivityDetection?.Invoke(this, new LogEvent(data.ToString())); } }
// This method establishs a connection to the device. Therefore an IP address and the port number // for the TcpClient is need. The client itself is used for the implementation of the ModbusIpMaster. public void Connect() { try { _client = new TcpClient(_iPAddress, _port); _master = ModbusIpMaster.CreateIp(_client); _connected = true; BusActivityDetection?.Invoke(this, new LogEvent("Connection has been established successfully")); } catch (Exception) { _connected = false; // If the connection establishment has not been successful - connected=false. BusActivityDetection?.Invoke(this, new LogEvent("Connection has NOT been established successfully")); } }
private void OnFetch(bool success, JToken token) { if (!success) { this._connected = false; JetBusException exception = new JetBusException(token); _mException = new Exception(exception.Error.ToString()); } // // Wake up the waiting thread where call the construktor to connect the session // this._connected = true; _mSuccessEvent.Set(); BusActivityDetection?.Invoke(this, new LogEvent("Fetch-All success: " + success + " - buffersize is " + _dataJTokenBuffer.Count)); }
// This method publishes the event (MessageEvent) and read the register, afterwards the message(from the register) will be sent back to WTX120. // This method is declared as a virtual method to allow derived class to override the event call. //protected virtual void ReadRegisterPublishing(MessageEvent<ushort> e) public virtual void ReadRegisterPublishing(DataEvent e) // 25.4 Comment : 'virtual' machte hier probleme beim durchlaufen :o { // virtual new due to tesing - 3.5.2018 try { // Read the data: e.Message's type - ushort[] //e.Args = masterParam.ReadHoldingRegisters(this.StartAdress, this.getNumOfPoints); e.ushortArgs = _master.ReadHoldingRegisters(StartAdress, NumOfPoints); _connected = true; BusActivityDetection?.Invoke(this, new LogEvent("Read successful: Registers have been read")); } catch (ArgumentException) { Console.WriteLine("\nNumber of points has to be between 1 and 125.\n"); } catch (InvalidOperationException) { BusActivityDetection?.Invoke(this, new LogEvent("Read failed : Registers have not been read")); _connected = false; Connect(); Thread.Sleep(100); } _data = e.ushortArgs; // copy of the event to avoid that a race condition is prevented, if the former subscriber directly logs off after the last // condition( and after if(handler!=null) ) and before the event is triggered. //RaiseDataEvent?.Invoke(this, e); var handler = RaiseDataEvent; //If a subscriber exists: if (handler != null) { handler(this, e); } }
public void FetchAll() { //this.OnFetchData(this.simulateJTokenInstance("123", "add", 123)); Matcher matcher = new Matcher(); FetchId id; _peer.Fetch(out id, matcher, OnFetchData, null, 500); // Onfetch = null (given by 'JetBusConnection'), timeoutms=500; bool success = true; this.ConvertJTokenToStringArray(); if (this.behavior != Behavior.ReadFail_DataReceived) { RaiseDataEvent?.Invoke(this, new DataEvent(DataUshortArray, DataStrArray)); } BusActivityDetection?.Invoke(this, new LogEvent("Fetch-All success: " + success + " - buffersize is " + _dataBuffer.Count)); }
public void ReadRegisterPublishing(DataEvent e) { // Behavoir : Kann in Standard oder Filler Mode sein, kann unterschiedliche "NumInputs" haben. Dementsprechend abhängig // ist die Anzahl der eingelesenen Werte. Erstmal vom einfachen Fall ausgehen! switch (this.behavior) { case Behavior.WriteHandshakeTestSuccess: if (_dataWTX[4] == 0x0000) { _dataWTX[4] = 0x4000; } else if (_dataWTX[4] == 0x4000) { _dataWTX[4] = 0x0000; } break; case Behavior.InFillerMode: //data word for a application mode being in filler mode: Bit .0-1 = 1 || 2 (2 is the given value for filler mode according to the manual, but actually it is 1.) _dataWTX[5] = 0x1; break; case Behavior.InStandardMode: //data word for a application mode being in standard mode, not in filler mode: Bit .0-1 = 0 _dataWTX[5] = 0x00; break; case Behavior.CalibrationFail: //Handshake bit: if (_dataWTX[5] >> 14 == 0) { _dataWTX[5] = 0x4000; } else if (_dataWTX[5] >> 14 == 1) { _dataWTX[5] = 0x0000; } break; case Behavior.CalibrationSuccess: //Handshake bit: if (_dataWTX[5] >> 14 == 0) { _dataWTX[5] = 0x4000; } else if (_dataWTX[5] >> 14 == 1) { _dataWTX[5] = 0x0000; } break; case Behavior.MeasureZeroFail: // Net value in hexadecimal: _dataWTX[0] = 0x00; _dataWTX[1] = 0x2710; // Gross value in hexadecimal: _dataWTX[2] = 0x00; _dataWTX[3] = 0x2710; //Handshake bit: if (_dataWTX[5] >> 14 == 0) { _dataWTX[5] = 0x4000; } else if (_dataWTX[5] >> 14 == 1) { _dataWTX[5] = 0x0000; } break; case Behavior.MeasureZeroSuccess: // Net value in hexadecimal: _dataWTX[0] = 0x00; _dataWTX[1] = 0x00; // Gross value in hexadecimal: _dataWTX[2] = 0x00; _dataWTX[3] = 0x00; //Handshake bit: if (_dataWTX[5] >> 14 == 0) { _dataWTX[5] = 0x4000; } else if (_dataWTX[5] >> 14 == 1) { _dataWTX[5] = 0x0000; } break; case Behavior.ReadFail: // If there is a connection fail, all data attributes get 0 as value. for (int index = 0; index < _dataWTX.Length; index++) { _dataWTX[index] = 0x0000; } _logObj = new LogEvent("Read failed : Registers have not been read"); BusActivityDetection?.Invoke(this, _logObj); break; case Behavior.ReadSuccess: // The most important data attributes from the WTX120 device: _dataWTX[0] = 0x0000; _dataWTX[1] = 0x4040; _dataWTX[2] = 0x0000; _dataWTX[3] = 0x4040; _dataWTX[4] = 0x0000; _dataWTX[5] = 0x0000; _logObj = new LogEvent("Read successful: Registers have been read"); BusActivityDetection?.Invoke(this, _logObj); break; case Behavior.t_UnitValue_Success: _dataWTX[5] = 0x100; break; case Behavior.t_UnitValue_Fail: _dataWTX[5] = 0x0000; break; case Behavior.kg_UnitValue_Success: _dataWTX[5] = 0x0000; break; case Behavior.kg_UnitValue_Fail: _dataWTX[5] = 0xFFFF; break; case Behavior.g_UnitValue_Success: _dataWTX[5] = 0x80; break; case Behavior.g_UnitValue_Fail: _dataWTX[5] = 0x0000; break; case Behavior.lb_UnitValue_Success: _dataWTX[5] = 0x180; break; case Behavior.lb_UnitValue_Fail: _dataWTX[5] = 0x0000; break; case Behavior.NetGrossValueStringComment_0D_Success: _dataWTX[5] = 0x0000; break; case Behavior.NetGrossValueStringComment_1D_Success: _dataWTX[5] = 0x10; break; case Behavior.NetGrossValueStringComment_2D_Success: _dataWTX[5] = 0x20; break; case Behavior.NetGrossValueStringComment_3D_Success: _dataWTX[5] = 0x30; break; case Behavior.NetGrossValueStringComment_4D_Success: _dataWTX[5] = 0x40; break; case Behavior.NetGrossValueStringComment_5D_Success: _dataWTX[5] = 0x50; break; case Behavior.NetGrossValueStringComment_6D_Success: _dataWTX[5] = 0x60; break; // Simulate for testing 'Scale range': case Behavior.ScaleRangeStringComment_Range1_Fail: _dataWTX[4] = 0x200; break; case Behavior.ScaleRangeStringComment_Range2_Fail: _dataWTX[4] = 0x0000; break; case Behavior.ScaleRangeStringComment_Range3_Fail: _dataWTX[4] = 0x100; break; case Behavior.ScaleRangeStringComment_Range1_Success: _dataWTX[4] = 0x0000; break; case Behavior.ScaleRangeStringComment_Range2_Success: _dataWTX[4] = 0x100; break; case Behavior.ScaleRangeStringComment_Range3_Success: _dataWTX[4] = 0x200; break; // Simulate for testing 'Limit status': case Behavior.LimitStatusStringComment_Case0_Fail: _dataWTX[4] = 0xC; break; case Behavior.LimitStatusStringComment_Case1_Fail: _dataWTX[4] = 0x8; break; case Behavior.LimitStatusStringComment_Case2_Fail: _dataWTX[4] = 0x0000; break; case Behavior.LimitStatusStringComment_Case3_Fail: _dataWTX[4] = 0x4; break; case Behavior.LimitStatusStringComment_Case0_Success: _dataWTX[4] = 0x0000; break; case Behavior.LimitStatusStringComment_Case1_Success: _dataWTX[4] = 0x4; break; case Behavior.LimitStatusStringComment_Case2_Success: _dataWTX[4] = 0x8; break; case Behavior.LimitStatusStringComment_Case3_Success: _dataWTX[4] = 0xC; break; // Simulate for testing 'Weight moving': case Behavior.WeightMovingStringComment_Case0_Fail: _dataWTX[4] = 0x0010; break; case Behavior.WeightMovingStringComment_Case1_Fail: _dataWTX[4] = 0x0000; break; case Behavior.WeightMovingStringComment_Case0_Success: _dataWTX[4] = 0x0000; break; case Behavior.WeightMovingStringComment_Case1_Success: _dataWTX[4] = 0x0010; break; // Simulate for testing 'Weight type': case Behavior.WeightTypeStringComment_Case0_Fail: _dataWTX[4] = 0x0080; break; case Behavior.WeightTypeStringComment_Case1_Fail: _dataWTX[4] = 0x0000; break; case Behavior.WeightTypeStringComment_Case0_Success: _dataWTX[4] = 0x0000; break; case Behavior.WeightTypeStringComment_Case1_Success: _dataWTX[4] = 0x0080; break; case Behavior.LogEvent_Fail: _logObj = new LogEvent("Read failed : Registers have not been read"); BusActivityDetection?.Invoke(this, _logObj); break; case Behavior.LogEvent_Success: _logObj = new LogEvent("Read successful: Registers have been read"); BusActivityDetection?.Invoke(this, _logObj); break; default: /* * for (int index = 0; index < _dataWTX.Length; index++) * { * _dataWTX[index] = 0; * } * _logObj = new LogEvent("Read failed : Registers have not been read"); * BusActivityDetection?.Invoke(this, _logObj); */ break; } RaiseDataEvent?.Invoke(this, new DataEvent(this._dataWTX, new string[0])); /* * var handler = RaiseDataEvent; * * //If a subscriber exists: * if (handler != null) handler(this, new DataEvent(_dataWTX)); */ }