public void Write(System.Char data) { unsafe { System.Char *pPtr = &data; Write((IntPtr)pPtr, sizeof(System.Char)); } }
// Update a string and return its size private UInt32 UpdateStringAndReturnSize(System.Char *value, UInt32 maxElementCount, string stringAsValue) { byte[] nameAsBuffer = ASCIIEncoding.ASCII.GetBytes(stringAsValue); UInt32 valueElementCount = maxElementCount; if (nameAsBuffer.Length < valueElementCount) { valueElementCount = Convert.ToUInt32(nameAsBuffer.Length); } Marshal.Copy(nameAsBuffer, 0, (IntPtr)value, Convert.ToInt32(valueElementCount)); return(valueElementCount); }
/// <summary> /// Converts the value to its equivalent string representation and puts it into StringBuilder. /// Returns into StringBuilder: /// The string representation of the value of this instance, consisting of a /// negative sign if the value is negative, and a sequence of digits ranging /// from 0 to 9 with no leading zeroes. /// </summary> public unsafe static void ToStringBuilder( SmartInt value, System.Int32 ndp, System.Text.StringBuilder sb ) { const System.Int32 vzero = (System.Int32) '0'; if (value == SmartInt.BadValue) { sb.Append("BadValue"); return; } if (value == 0) { sb.Append('0'); return; } // 12345678911 System.Char *buffer = stackalloc System.Char[10]; System.Int32 v1, v2; System.Int32 ndigits = 10; if (value < 0) { sb.Append("-"); v1 = -value; } else { v1 = value; } for (int i = 9; i >= 0; i--) { if (v1 > 0) { ndigits--; } v2 = v1 / 10; buffer[i] = (System.Char)(vzero + v1 - (((v2 << 2) + v2) << 1)); v1 = v2; } int leading_zeros = ndp - 10 + ndigits; while (leading_zeros > 0) { sb.Append('0'); leading_zeros--; } for (int i = ndigits; i < 10; i++) { sb.Append(buffer[i]); } return; }
// 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.Char *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 = UpdateStringAndReturnSize(value, maxElementCount, database.Device.name); return(true); } else if (propertyIdentifier == CASBACnetStackAdapter.PROPERTY_IDENTIFIER_MODEL_NAME) { *valueElementCount = UpdateStringAndReturnSize(value, maxElementCount, database.Device.modelName); return(true); } else if (propertyIdentifier == CASBACnetStackAdapter.PROPERTY_IDENTIFIER_VENDOR_NAME) { *valueElementCount = UpdateStringAndReturnSize(value, maxElementCount, database.Device.vendorName); return(true); } else if (propertyIdentifier == CASBACnetStackAdapter.PROPERTY_IDENTIFIER_APPLICATIONSOFTWAREVERSION) { string version = APPLICATION_VERSION + "." + CIBuildVersion.CIBUILDNUMBER; * valueElementCount = UpdateStringAndReturnSize(value, maxElementCount, version); return(true); } else if (propertyIdentifier == 512 + 1) { string version = database.Device.proprietaryProperty513; * valueElementCount = UpdateStringAndReturnSize(value, maxElementCount, version); return(true); } else if (propertyIdentifier == 512 + 2) { string version = database.Device.proprietaryProperty514; * valueElementCount = UpdateStringAndReturnSize(value, maxElementCount, version); return(true); } } break; default: break; } Console.WriteLine(" FYI: Not implmented. propertyIdentifier={0}", propertyIdentifier); return(false); // Could not handle this request. }
// Read string from char pointer private string GetStringFromCharPointer(System.Char *value, UInt32 maxElementCount, Byte encodingType) { byte[] nameAsBuffer = new Byte[maxElementCount]; Marshal.Copy((IntPtr)value, nameAsBuffer, 0, Convert.ToInt32(maxElementCount)); return(ASCIIEncoding.ASCII.GetString(nameAsBuffer)); }