Jump to content

Recommended Posts

I'm trying to write values and I'm not sure what the binary WRITE message format is, in relation to the READ message format.

I couldn't find this answered in the PDF or on the forums yet. I read a summary in the PDF that byte 12 should be changed to 68 and the write values should be appended to byte 32 (which confused me further as that's after the footer checksum).

This is what I have for READ:

0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 24 25 26 27 28 29 | 30 31 32
stx-------------- id FE 01 00 00 00 cn 00 specific--------- lengt chksm | numbr ot FF addr- | chksm ex

What would be the correct format for WRITE?

Link to comment
Share on other sites

The document on our website describes how to read values in Binary Protocol.

The documented command is only useful for reading (while writing is still in ASCII).

However, few years ago we have added a new command that allows you to read and write using Binary Command (And this command actually allows you have a read and write request on the same command).

This command is not documented on our website, but if you know C# then you can take the .Net communication driver sources (which are available on our website) and understand the structure of the command.

  • Upvote 1
Link to comment
Share on other sites

Please ignore my unapproved post. I found some related information in BinaryMessage.cs and WriteOperand.cs. I'm not very proficient at C# but I noticed there are a lot of variables at play, making it difficult for me to narrow down an actual command string.

Do you have specifics on which methods or strings I should be referring to?

Link to comment
Share on other sites

Thank you, that's come in handy. Now I'm onto writing specific operands, I have gotten MB and MI working (reading and writing), ML, MF, DW (reading only).

I'm not sure what the method for writing those last 3 are, I know that I have to reverse the byte order for MIs, is it similar for MLs? I'm assuming that ML is 4 hex bytes and DW is 6.

I have to write this for every operand type. I'm still running through the C# in relation to those.

Link to comment
Share on other sites

I don't remember anything of reversing the byte order of MIs, or any operand (maybe Float), here is a code from the .Net communication driver that takes the value and convert it to bytes:

private IEnumerable<byte> AddWriteData(object[] writeData, byte writeOperandId)
    {
	    List<byte> results = new List<byte>();
	    string operandName = writeOperandId.GetOperandNameByValueForFullBinary();
	    switch (operandName)
	    {
		    case "MB":
		    case "SB":
		    case "XB":
		    case "INPUT":
		    case "Output":
		    case "TimerRunBit":
		    case "CounterRunBit":
		    case "RTC":
			    foreach (object value in writeData)
			    {
				    UInt16 bitValue = BitConverter.ToUInt16(BitConverter.GetBytes(Convert.ToSByte(value)), 0);
				    if (bitValue > 0)
					    results.Add(1);
				    else
					    results.Add(0);
				    if (writeData.Length == 1)
					    results.Add(0);
			    }
			    break;
		    case "MI":
		    case "SI":
		    case "XI":
		    case "CounterCurrent":
		    case "CounterPreset":
			    foreach (object value in writeData)
			    {
				    results.AddRange(BitConverter.GetBytes(Convert.ToInt16(value)));
			    }
			    break;
		    case "ML":
		    case "SL":
		    case "XL":
			    foreach (object value in writeData)
			    {
				    results.AddRange(BitConverter.GetBytes(Convert.ToInt32(value)));
			    }
			    break;
		    case "TimerCurrent":
		    case "TimerPreset":
			    foreach (object value in writeData)
			    {
				    if (value.GetType().Equals(typeof(List<UInt16>)))
					    results.AddRange(BitConverter.GetBytes(Utils.z_GetSecondsValue(value as List<UInt16>)));
				    else
					    results.AddRange(BitConverter.GetBytes(Convert.ToUInt32(value)));
			    }
			    break;
		    case "MF":
			    foreach (object value in writeData)
			    {
				    //Switch to IEEE 754 standard
				    byte[] floatBytes = BitConverter.GetBytes(Convert.ToSingle(value));
				    Array.Reverse(floatBytes, 0, 4);
				    Array.Reverse(floatBytes, 0, 2);
				    Array.Reverse(floatBytes, 2, 2);
				    results.AddRange(floatBytes);
			    }
			    break;
		    case "DW":
		    case "SDW":
		    case "XDW":
			    foreach (object value in writeData)
			    {
				    results.AddRange(BitConverter.GetBytes(Convert.ToUInt32(value)));
			    }
			    break;
	    }
	    return results;
    }

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

This site uses cookies. By clicking I accept, you agree to their use.