/// <summary> /// 2 bytes Id (uint16) /// 2 bytes for content length (uint16) /// 1 byte for read/write mode (0=Read) /// 2 bytes for the variable name length (uint16) /// N bytes for the variable name to be read (ASCII) /// </summary> /// <param name="varName"></param> /// <param name="outputString"></param> /// <returns></returns> private static byte[] ReadMessageRequest(string varName, out string outputString) { Random rnd = new Random(); string id = (rnd.Next(0, 99)).ToString("X2"); // 2Bytes Id (uint16) string contentLength = (varName.Length + 3).ToString("X2");; //2 bytes contentLength (uint16) string mode = "0"; //1 byte for read/write format string nameLength = varName.Length.ToString("X2"); //2 bytes for the variable name length outputString = id + contentLength + mode + nameLength + varName; MessageSendFormat readRequest = new MessageSendFormat(id, contentLength, mode, nameLength, varName); return(readRequest.messageReady); }
/// <summary> /// 2 bytes Id (uint16) /// 2 bytes for content length (uint16) /// 1 byte for read/write mode (1=Write) /// 2 bytes for the variable name length (uint16) /// N bytes for the variable name to be written (ASCII) /// 2 bytes for the variable value length (uint16) /// M bytes for the variable value to be written (ASCII) /// </summary> /// <param name="varName"></param> /// <param name="varValue"></param> /// <param name="outputString"></param> /// <returns></returns> private static byte[] WriteMessageRequest(string varName, string varValue, out string outputString) { Random rnd = new Random(); string messageId = (rnd.Next(0, 99)).ToString("X2"); //2 bytes for id string reqLength = (varName.Length + varValue.Length + 5).ToString("X2"); //2 bytes for content length string functionType = "1"; //1 byte for write mode string varNameLength = varName.Length.ToString("X2"); //2 bytes for variable name length //N bytes for variable name to be written string varValueLength = varValue.Length.ToString("X2"); //2 bytes for the variable value length. //N bytes for the variable value to be written. outputString = messageId + reqLength + functionType + varNameLength + varName + varValueLength + varValue; MessageSendFormat readRequest = new MessageSendFormat(messageId, reqLength, functionType, varNameLength, varName, varValueLength, varValue); return(readRequest.messageReady); }