// Callback used by the BACnet Stack to send a BACnet message public UInt16 SendMessage(System.Byte *message, UInt16 messageLength, System.Byte *connectionString, System.Byte connectionStringLength, System.Byte networkType, Boolean broadcast) { if (connectionStringLength < 6 || messageLength <= 0) { return(0); } // Extract the connection string into a IP address and port. IPAddress ipAddress = new IPAddress(new byte[] { connectionString[0], connectionString[1], connectionString[2], connectionString[3] }); IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, (connectionString[4] + connectionString[5] * 256)); // Debug Console.WriteLine("FYI: Sending {0} bytes to {1}", messageLength, ipEndPoint.ToString()); // Copy from the unsafe pointer to a Byte array. byte[] sendBytes = new byte[messageLength]; Marshal.Copy((IntPtr)message, sendBytes, 0, messageLength); try { udpServer.Send(sendBytes, sendBytes.Length, ipEndPoint); return((UInt16)sendBytes.Length); } catch (Exception e) { Console.WriteLine(e.ToString()); } return(0); }
// Callback used by the BACnet Stack to check if there is a message to process public UInt16 RecvMessage(System.Byte *message, UInt16 maxMessageLength, System.Byte *receivedConnectionString, System.Byte maxConnectionStringLength, System.Byte *receivedConnectionStringLength, System.Byte *networkType) { try { if (udpServer.Available > 0) { // Data buffer for incoming data. byte[] receiveBytes = udpServer.Receive(ref remoteIpEndPoint); byte[] ipAddress = remoteIpEndPoint.Address.GetAddressBytes(); byte[] port = BitConverter.GetBytes(UInt16.Parse(remoteIpEndPoint.Port.ToString())); // Copy from the unsafe pointer to a Byte array. Marshal.Copy(receiveBytes, 0, (IntPtr)message, receiveBytes.Length); // Copy the Connection string Marshal.Copy(ipAddress, 0, (IntPtr)receivedConnectionString, 4); Marshal.Copy(port, 0, (IntPtr)receivedConnectionString + 4, 2); *receivedConnectionStringLength = 6; // Debug Console.WriteLine("FYI: Recving {0} bytes from {1}", receiveBytes.Length, remoteIpEndPoint.ToString()); // Return length. return((ushort)receiveBytes.Length); } } catch (Exception e) { Console.WriteLine(e.ToString()); } return(0); }
public bool SendMessage(System.UInt16 connectionId, System.Byte *payload, System.UInt16 payloadSize) { if (!_serialPort.IsOpen) { return(false); // Serial port is not open, can't send any bytes. } Console.WriteLine("FYI: Sending {0} bytes", payloadSize); // Copy from the unsafe pointer to a Byte array. byte[] message = new byte[payloadSize]; Marshal.Copy((IntPtr)payload, message, 0, payloadSize); try { _serialPort.Write(message, 0, payloadSize); // Message sent Console.Write(" "); Console.WriteLine(BitConverter.ToString(message).Replace("-", " ")); // Convert bytes to HEX string. return(true); } catch (Exception e) { Console.WriteLine(e.ToString()); return(false); } }
public void Write(System.Byte data) { unsafe { System.Byte *pPtr = &data; Write((IntPtr)pPtr, sizeof(System.Byte)); } }
// Callback used by the BACnet Stack to send a BACnet message public UInt16 SendMessage(System.Byte *message, UInt16 messageLength, System.Byte *connectionString, System.Byte connectionStringLength, System.Byte networkType, Boolean broadcast) { if (connectionStringLength < 6 || messageLength <= 0) { return(0); } // Extract the connection string into a IP address and port. IPAddress ipAddress; if (broadcast) { // Note: for the sake of this example, simply setting the last octet if the message should be sent as a broadcast. ipAddress = new IPAddress(new byte[] { connectionString[0], connectionString[1], connectionString[2], 0xFF }); } else { ipAddress = new IPAddress(new byte[] { connectionString[0], connectionString[1], connectionString[2], connectionString[3] }); } IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, (connectionString[4] + connectionString[5] * 256)); // Debug Console.WriteLine("FYI: Sending {0} bytes to {1}", messageLength, ipEndPoint.ToString()); // XML decode (debug) IntPtr xmlBuffer = Marshal.AllocHGlobal(1024 * 5); int bufferLength = CASBACnetStackAdapter.DecodeAsXML(message, messageLength, xmlBuffer, 1024 * 5); string xmlStringBuffer = Marshal.PtrToStringAnsi(xmlBuffer, bufferLength); Marshal.FreeHGlobal(xmlBuffer);// Free HGlobal memory Console.WriteLine(xmlStringBuffer); Console.WriteLine(""); // Copy from the unsafe pointer to a Byte array. byte[] sendBytes = new byte[messageLength]; Marshal.Copy((IntPtr)message, sendBytes, 0, messageLength); try { this.udpServer.Send(sendBytes, sendBytes.Length, ipEndPoint); return((UInt16)sendBytes.Length); } catch (Exception e) { Console.WriteLine(e.ToString()); } return(0); }
public int RecvMessage(System.UInt16 *connectionId, System.Byte *payload, System.UInt16 maxPayloadSize) { if (this.tcpClient == null || !this.tcpClient.Connected) { return(0); } // Data buffer for incoming data. byte[] bytes = new Byte[maxPayloadSize]; // Try to get the data from the socket. try { if (this.tcpClient.Available <= 0) { return(0); } // An incoming connection needs to be processed. int bytesRec = this.tcpClient.Receive(bytes); if (bytesRec <= 0) { return(0); } // Copy from the unsafe pointer to a Byte array. byte[] message = new byte[bytesRec]; Marshal.Copy(bytes, 0, (IntPtr)payload, bytesRec); // Debug Show the data on the console. Console.WriteLine("FYI: Recived {0} bytes from {1}", bytesRec, this.tcpClient.RemoteEndPoint.ToString()); Console.Write(" "); Console.WriteLine(BitConverter.ToString(bytes).Replace("-", " ").Substring(0, bytesRec * 3)); // Convert bytes to HEX string. return(bytesRec); } catch (System.Net.Sockets.SocketException e) { if (e.ErrorCode == 10054) { // Client disconnected. This is normal. return(0); } Console.WriteLine(e.ToString()); return(0); } }
// Callback used by the BACnet Stack to check if there is a message to process public UInt16 RecvMessage(System.Byte *message, UInt16 maxMessageLength, System.Byte *receivedConnectionString, System.Byte maxConnectionStringLength, System.Byte *receivedConnectionStringLength, System.Byte *networkType) { try { if (this.udpServer.Available > 0) { // Data buffer for incoming data. byte[] receiveBytes = this.udpServer.Receive(ref this.RemoteIpEndPoint); byte[] ipAddress = RemoteIpEndPoint.Address.GetAddressBytes(); byte[] port = BitConverter.GetBytes(UInt16.Parse(RemoteIpEndPoint.Port.ToString())); // Copy from the unsafe pointer to a Byte array. Marshal.Copy(receiveBytes, 0, (IntPtr)message, receiveBytes.Length); // Copy the Connection string Marshal.Copy(ipAddress, 0, (IntPtr)receivedConnectionString, 4); Marshal.Copy(port, 0, (IntPtr)receivedConnectionString + 4, 2); *receivedConnectionStringLength = 6; // Debug Console.WriteLine("FYI: Recving {0} bytes from {1}", receiveBytes.Length, RemoteIpEndPoint.ToString()); // XML decode (debug) IntPtr xmlBuffer = Marshal.AllocHGlobal(1024 * 5); int bufferLength = CASBACnetStackAdapter.DecodeAsXML(message, (ushort)receiveBytes.Length, xmlBuffer, 1024 * 5); string xmlStringBuffer = Marshal.PtrToStringAnsi(xmlBuffer, bufferLength); Marshal.FreeHGlobal(xmlBuffer);// Free HGlobal memory Console.WriteLine(xmlStringBuffer); Console.WriteLine(""); // Return length return((ushort)receiveBytes.Length); } } catch (Exception e) { Console.WriteLine(e.ToString()); } return(0); }
public bool SendMessage(System.UInt16 connectionId, System.Byte *payload, System.UInt16 payloadSize) { if (this.tcpClient == null || !this.tcpClient.Connected) { return(false); } Console.WriteLine("FYI: Sending {0} bytes to {1}", payloadSize, this.tcpClient.RemoteEndPoint.ToString()); // Copy from the unsafe pointer to a Byte array. byte[] message = new byte[payloadSize]; Marshal.Copy((IntPtr)payload, message, 0, payloadSize); try { // Send the message if (this.tcpClient.Send(message) == payloadSize) { // Message sent Console.Write(" "); Console.WriteLine(BitConverter.ToString(message).Replace("-", " ")); // Convert bytes to HEX string. return(true); } } catch (System.Net.Sockets.SocketException e) { if (e.ErrorCode == 10054) { // Client disconnected. This is normal. return(false); } Console.WriteLine(e.ToString()); return(false); } // Could not send message for some reason. return(false); }
public int RecvMessage(System.UInt16 *connectionId, System.Byte *payload, System.UInt16 maxPayloadSize) { if (!_serialPort.IsOpen) { return(0); // Serial port is not open, can't recive any bytes. } try { byte[] incommingMessage = new byte[maxPayloadSize]; int incommingMessageSize = _serialPort.Read(incommingMessage, 0, maxPayloadSize); if (incommingMessageSize <= 0) { // Nothing recived. return(0); } // Copy from the unsafe pointer to a Byte array. byte[] message = new byte[incommingMessageSize]; Marshal.Copy(incommingMessage, 0, (IntPtr)payload, incommingMessageSize); // Debug Show the data on the console. Console.WriteLine("FYI: Recived {0} bytes", incommingMessageSize); Console.Write(" "); Console.WriteLine(BitConverter.ToString(incommingMessage).Replace("-", " ").Substring(0, incommingMessageSize * 3)); // Convert bytes to HEX string. return(incommingMessageSize); } catch (TimeoutException) { // Timeout while wating return(0); } catch (Exception e) { Console.WriteLine(e.ToString()); return(0); } }
// Callback used by the BACnet Stack to set Charstring property values to the user public bool CallbackGetPropertyCharString(UInt32 deviceInstance, UInt16 objectType, UInt32 objectInstance, UInt32 propertyIdentifier, System.Byte *value, UInt32 *valueElementCount, UInt32 maxElementCount, System.Byte encodingType, bool useArrayIndex, UInt32 propertyArrayIndex) { Console.WriteLine("FYI: Request for CallbackGetPropertyCharString. objectType={0}, objectInstance={1}, propertyIdentifier={2}, propertyArrayIndex={3}", objectType, objectInstance, propertyIdentifier, propertyArrayIndex); switch (objectType) { case CASBACnetStackAdapter.OBJECT_TYPE_DEVICE: if (deviceInstance == database.Device.Instance && objectInstance == database.Device.Instance) { if (propertyIdentifier == CASBACnetStackAdapter.PROPERTY_IDENTIFIER_OBJECT_NAME) { *valueElementCount = CASBACnetStackAdapter.UpdateStringAndReturnSize(value, maxElementCount, database.Device.Name); return(true); } else if (propertyIdentifier == CASBACnetStackAdapter.PROPERTY_IDENTIFIER_MODEL_NAME) { *valueElementCount = CASBACnetStackAdapter.UpdateStringAndReturnSize(value, maxElementCount, database.Device.ModelName); return(true); } else if (propertyIdentifier == CASBACnetStackAdapter.PROPERTY_IDENTIFIER_VENDOR_NAME) { *valueElementCount = CASBACnetStackAdapter.UpdateStringAndReturnSize(value, maxElementCount, database.Device.VendorName); return(true); } else if (propertyIdentifier == CASBACnetStackAdapter.PROPERTY_IDENTIFIER_DESCRIPTION) { *valueElementCount = CASBACnetStackAdapter.UpdateStringAndReturnSize(value, maxElementCount, database.Device.Description); return(true); } else if (propertyIdentifier == CASBACnetStackAdapter.PROPERTY_IDENTIFIER_APPLICATIONSOFTWAREVERSION) { string version = APPLICATION_VERSION + "." + CIBuildVersion.CIBUILDNUMBER; * valueElementCount = CASBACnetStackAdapter.UpdateStringAndReturnSize(value, maxElementCount, version); return(true); } } break; case CASBACnetStackAdapter.OBJECT_TYPE_ANALOG_INPUT: if (objectInstance == database.AnalogInputAutoIncrement.Instance) { if (propertyIdentifier == CASBACnetStackAdapter.PROPERTY_IDENTIFIER_OBJECT_NAME) { *valueElementCount = CASBACnetStackAdapter.UpdateStringAndReturnSize(value, maxElementCount, database.AnalogInputAutoIncrement.Name); return(true); } } else if (objectInstance == database.AnalogInputManualIncrement.Instance) { if (propertyIdentifier == CASBACnetStackAdapter.PROPERTY_IDENTIFIER_OBJECT_NAME) { *valueElementCount = CASBACnetStackAdapter.UpdateStringAndReturnSize(value, maxElementCount, database.AnalogInputManualIncrement.Name); return(true); } } break; case CASBACnetStackAdapter.OBJECT_TYPE_BINARY_INPUT: if (objectInstance == database.BinaryInput.Instance) { if (propertyIdentifier == CASBACnetStackAdapter.PROPERTY_IDENTIFIER_OBJECT_NAME) { *valueElementCount = CASBACnetStackAdapter.UpdateStringAndReturnSize(value, maxElementCount, database.BinaryInput.Name); return(true); } } break; case CASBACnetStackAdapter.OBJECT_TYPE_MULTI_STATE_INPUT: if (objectInstance == database.MultiStateInput.Instance) { if (propertyIdentifier == CASBACnetStackAdapter.PROPERTY_IDENTIFIER_OBJECT_NAME) { *valueElementCount = CASBACnetStackAdapter.UpdateStringAndReturnSize(value, maxElementCount, database.MultiStateInput.Name); return(true); } else if (propertyIdentifier == CASBACnetStackAdapter.PROPERTY_IDENTIFIER_STATETEXT && useArrayIndex) { if (propertyArrayIndex <= database.MultiStateInput.StateText.Length) { *valueElementCount = CASBACnetStackAdapter.UpdateStringAndReturnSize(value, maxElementCount, database.MultiStateInput.StateText[propertyArrayIndex - 1]); return(true); } } } break; case CASBACnetStackAdapter.OBJECT_TYPE_TREND_LOG: if (objectInstance == database.TrendLog.Instance) { if (propertyIdentifier == CASBACnetStackAdapter.PROPERTY_IDENTIFIER_OBJECT_NAME) { *valueElementCount = CASBACnetStackAdapter.UpdateStringAndReturnSize(value, maxElementCount, database.TrendLog.Name); return(true); } } break; case CASBACnetStackAdapter.OBJECT_TYPE_TREND_LOG_MULTIPLE: if (objectInstance == database.TrendLogMultiple.Instance) { if (propertyIdentifier == CASBACnetStackAdapter.PROPERTY_IDENTIFIER_OBJECT_NAME) { *valueElementCount = CASBACnetStackAdapter.UpdateStringAndReturnSize(value, maxElementCount, database.TrendLogMultiple.Name); return(true); } } break; default: break; } Console.WriteLine(" FYI: Not implmented. propertyIdentifier={0}", propertyIdentifier); return(false); // Could not handle this request. }
public static extern uint ReadRegisters(System.UInt16 connectionId, System.Byte address, System.Byte function, System.UInt16 startingAddress, System.UInt16 length, System.IntPtr data, System.UInt16 maxData, System.Byte *exceptionCode);
public static extern uint WriteRegisters(System.UInt16 connectionId, System.Byte address, System.Byte function, System.UInt16 startingAddress, System.IntPtr data, System.UInt16 valuesSize, System.Byte *exceptionCode);
void CallbackHookPropertyTime(UInt32 id, System.Byte service, UInt16 objectType, UInt32 objectInstance, UInt32 propertyIdentifier, bool usePropertyArrayIndex, UInt32 propertyArrayIndex, System.Byte hour, System.Byte minute, System.Byte second, System.Byte hundrethSecond, System.Byte *connectionString, System.Byte connectionStringLength, System.Byte networkType, UInt16 network, System.Byte *sourceAddress, System.Byte sourceAddressLength) { Console.WriteLine("CallbackHookPropertyTime id=[{0}], objectType=[{1}], objectInstance=[{2}], propertyIdentifier=[{3}], hour=[{4}], minute=[{4}], second=[{4}], hundrethSecond=[{4}] ", id, objectType, objectInstance, propertyIdentifier, hour, minute, second, hundrethSecond); }
void CallbackHookPropertyUInt(UInt32 id, System.Byte service, UInt16 objectType, UInt32 objectInstance, UInt32 propertyIdentifier, bool usePropertyArrayIndex, UInt32 propertyArrayIndex, UInt32 value, System.Byte *connectionString, System.Byte connectionStringLength, System.Byte networkType, UInt16 network, System.Byte *sourceAddress, System.Byte sourceAddressLength) { Console.WriteLine("CallbackHookPropertyUInt id=[{0}], objectType=[{1}], objectInstance=[{2}], propertyIdentifier=[{3}], value=[{4}] ", id, objectType, objectInstance, propertyIdentifier, value); }
public bool GetModbusValue(System.Byte slaveAddress, System.Byte function, System.UInt16 startingAddress, System.UInt16 length, System.Byte *data, System.UInt16 maxDataSize, System.Byte *errorCode) { Console.WriteLine("FYI: GetModbusValue slaveAddress=[{0}], function=[{1}], startingAddress=[{2}], length=[{3}]", slaveAddress, function, startingAddress, length); if (startingAddress > SETTING_MODBUS_DATABASE_MAX_SIZE) { // Out of range *errorCode = CASModbusAdapter.EXCEPTION_02_ILLEGAL_DATA_ADDRESS; } switch (function) { case CASModbusAdapter.FUNCTION_03_READ_HOLDING_REGISTERS: case CASModbusAdapter.FUNCTION_04_READ_INPUT_REGISTERS: // Convert the USHORT into BYTE byte[] dataAsBytes = new byte[length * 2]; Buffer.BlockCopy(this.database, startingAddress, dataAsBytes, 0, length * 2); Marshal.Copy(dataAsBytes, 0, (IntPtr)data, length * 2); return(true); default: break; } return(false); }
void CallbackHookIAm(UInt32 deviceIdentifier, UInt32 maxApduLengthAccepted, System.Byte segmentationSupported, UInt16 vendorIdentifier, System.Byte *connectionString, System.Byte connectionStringLength, System.Byte networkType, UInt16 network, System.Byte *sourceAddress, System.Byte sourceAddressLength) { Console.WriteLine("CallbackHookIAm deviceIdentifier=[{0}]", deviceIdentifier); }
void CallbackHookIHave(UInt32 deviceIdentifier, UInt16 objectType, UInt32 objectInstance, System.Byte *objectName, UInt32 objectNameLength, System.Byte objectNameEncoding, System.Byte *connectionString, System.Byte connectionStringLength, System.Byte networkType, UInt16 network, System.Byte *sourceAddress, System.Byte sourceAddressLength) { Console.WriteLine("CallbackHookIHave deviceIdentifier=[{0}], objectType=[{1}], objectInstance=[{2}]", deviceIdentifier, objectType, objectInstance); }
void CallbackHookSimpleAck(System.Byte originalInvokeId, UInt32 serverAckChoice, System.Byte *connectionString, System.Byte connectionStringLength, System.Byte networkType, UInt16 network, System.Byte *sourceAddress, System.Byte sourceAddressLength) { Console.WriteLine("CallbackHookSimpleAck originalInvokeId=[{0}], serverAckChoice=[{1}]", originalInvokeId, serverAckChoice); }
void CallbackHookAbort(System.Byte originalInvokeId, bool sentByServer, UInt32 abortReason, System.Byte *connectionString, System.Byte connectionStringLength, System.Byte networkType, UInt16 network, System.Byte *sourceAddress, System.Byte sourceAddressLength) { Console.WriteLine("CallbackHookAbort originalInvokeId=[{0}], abortReason=[{1}]", originalInvokeId, abortReason); }
void CallbackHookReject(System.Byte originalInvokeId, UInt32 rejectReason, System.Byte *connectionString, System.Byte connectionStringLength, System.Byte networkType, UInt16 network, System.Byte *sourceAddress, System.Byte sourceAddressLength) { Console.WriteLine("CallbackHookReject originalInvokeId=[{0}], rejectReason=[{1}]", originalInvokeId, rejectReason); }
void CallbackHookError(System.Byte originalInvokeId, UInt32 errorChoice, UInt32 errorClass, UInt32 errorCode, System.Byte *connectionString, System.Byte connectionStringLength, System.Byte networkType, UInt16 network, System.Byte *sourceAddress, System.Byte sourceAddressLength, bool useObjectProperty, UInt16 objectType, UInt32 objectInstance, UInt32 propertyIdentifier) { Console.WriteLine("CallbackHookError originalInvokeId=[{0}], errorChoice=[{1}], errorClass=[{2}]", originalInvokeId, errorChoice, errorClass); }
public bool SetModbusValue(System.Byte slaveAddress, System.Byte function, System.UInt16 startingAddress, System.UInt16 length, System.Byte *data, System.UInt16 dataSize, System.Byte *errorCode) { Console.WriteLine("FYI: SetModbusValue slaveAddress=[{0}], function=[{1}], startingAddress=[{2}], length=[{3}], dataSize=[{4}]", slaveAddress, function, startingAddress, length, dataSize); if (startingAddress > SETTING_MODBUS_DATABASE_MAX_SIZE || startingAddress + length > SETTING_MODBUS_DATABASE_MAX_SIZE) { // Out of range *errorCode = CASModbusAdapter.EXCEPTION_02_ILLEGAL_DATA_ADDRESS; } switch (function) { case CASModbusAdapter.FUNCTION_06_PRESET_SINGLE_REGISTER: case CASModbusAdapter.FUNCTION_10_FORCE_MULTIPLE_REGISTERS: { short[] value = new short[length]; Marshal.Copy((IntPtr)data, value, 0, length); for (ushort offset = 0; offset < length; offset++) { this.database[startingAddress + offset] = (ushort)value[offset]; } return(true); } default: break; } return(false); }
void CallbackHookPropertyDate(UInt32 id, System.Byte service, UInt16 objectType, UInt32 objectInstance, UInt32 propertyIdentifier, bool usePropertyArrayIndex, UInt32 propertyArrayIndex, System.Byte year, System.Byte month, System.Byte day, System.Byte weekday, System.Byte *connectionString, System.Byte connectionStringLength, System.Byte networkType, UInt16 network, System.Byte *sourceAddress, System.Byte sourceAddressLength) { Console.WriteLine("CallbackHookPropertyDate id=[{0}], objectType=[{1}], objectInstance=[{2}], propertyIdentifier=[{3}], year=[{4}], month=[{4}], day=[{4}], weekday=[{4}] ", id, objectType, objectInstance, propertyIdentifier, year, month, day, weekday); }
void CallbackHookTimeout(System.Byte originalInvokeId, System.Byte *connectionString, System.Byte connectionStringLength, System.Byte networkType, UInt16 network, System.Byte *sourceAddress, System.Byte sourceAddressLength) { Console.WriteLine("CallbackHookTimeout originalInvokeId=[{0}]", originalInvokeId); }
void CallbackHookPropertyReal(UInt32 id, System.Byte service, UInt16 objectType, UInt32 objectInstance, UInt32 propertyIdentifier, bool usePropertyArrayIndex, UInt32 propertyArrayIndex, float value, System.Byte *connectionString, System.Byte connectionStringLength, System.Byte networkType, UInt16 network, System.Byte *sourceAddress, System.Byte sourceAddressLength) { Console.WriteLine("CallbackHookPropertyReal id=[{0}], objectType=[{1}], objectInstance=[{2}], propertyIdentifier=[{3}], value=[{4}] ", id, objectType, objectInstance, propertyIdentifier, value); if (objectType == CASBACnetStackAdapter.OBJECT_TYPE_ANALOG_INPUT && objectInstance == 0 && propertyIdentifier == CASBACnetStackAdapter.PROPERTY_IDENTIFIER_PRESENT_VALUE) { // Got the value Console.WriteLine("Example: Send this value to Azure"); } }