logic questions for UDP messages

Discussion in 'C-Bus Wiser 2 Controller' started by znelbok, Aug 27, 2017.

  1. znelbok

    znelbok

    Joined:
    Aug 3, 2004
    Messages:
    1,151
    Likes Received:
    17
    I am trying to program a Wiser 2, I'm new ot the logic engine but I am getting through it OK - its not that hard and there are a ot of good examples.

    I a trying to connect to a fire place that accepts UDP connections.

    So far I am still just building the logic code up but I get this warning when I compile

    Warning W006 at line 371:15 - Function or Procedure not supported for this Project type

    Thats referring to the line with the WriteUPDSocket

    WriteUDPSocket(EsceaIP, EsceaUDPPort, EsceaCmd);

    So what does it actually mean - not supported for this project type. I dont recall anything about project types. Does it mean the Wiser2 cant send UPD packets?

    Assuming that I can get around this my next problem is that the WriteUPDSocket command is a string. I need to send hex codes that are not strings.

    An example is 0x473100000000000000000000003146

    You can see there is a "G" at the start and an "F" at the end. Second byte is a "1" and is the second last byte - but how do I sent the 0x00's (a null). Its not a character - Is IntToHexString something I should be looking at?

    Thanks
     
    znelbok, Aug 27, 2017
    #1
  2. znelbok

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,524
    Likes Received:
    173
    Location:
    Adelaide, Australia
    I don't have a Wiser 2 but the Wiser 1 only works with TCP connection , not UDP. I assume the Wiser 2 is the same (The help only mentions Colour Touchscreen for UDP).

    As for the string, does the device require raw binary bytes or are they encoded as 2 ascii hex characters (i.e is the string printable?). Can you post the documentation that describes the protocol?
     
    Ashley, Aug 27, 2017
    #2
  3. znelbok

    znelbok

    Joined:
    Aug 3, 2004
    Messages:
    1,151
    Likes Received:
    17
    Thanks Ashley

    The protocol is not with me right now but it just gives the hex value I indicated which looks like they are all 2 pair ascii hex - I believe I could send a "G1....1F" string but I am not sure how to handle the nul ascii character (00) in a string

    I will have to look for a different way to do it - although I find it strange that the CT only will do UDP.
     
    znelbok, Aug 27, 2017
    #3
  4. znelbok

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,524
    Likes Received:
    173
    Location:
    Adelaide, Australia
    I thought the same thing when I found this with the Wiser 1. UDP is a much simpler protocol than TCP. But there you go...

    If the strings are static you can insert a single null byte using the # coding

    So 0x473100000000000000000000003146 would become

    myString := 'G1'#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0'1F'

    If you need to create them dynamically, you can go

    myString[3] := #0; to put a null byte in position 3

    or
    i: integer;
    for i := 3 to 24 do myString := #0; to do them all

    Note that this would be an unusual protocol to mix ascii characters with nulls, so a copy of the protocol document would be useful so I don't lead you astray :)
     
    Last edited by a moderator: Aug 28, 2017
    Ashley, Aug 28, 2017
    #4
  5. znelbok

    znelbok

    Joined:
    Aug 3, 2004
    Messages:
    1,151
    Likes Received:
    17
    So how do I sum text

    The CRC is the sum of bytes. So where the first is a G (47) and the next is a command - say 9 (39) and then a lot of nul's (00), the CRC is the sum of all those values in this case 80.
     
    znelbok, Aug 29, 2017
    #5
  6. znelbok

    rhamer

    Joined:
    Aug 3, 2004
    Messages:
    673
    Likes Received:
    3
    Location:
    Melbourne, Australia
    Just as a side comment, if you only need to send a few commands, you could hand calculate the checksums and just hard code them into the string.
    This saves you writing a whole checksum calculation routine.

    Rohan
     
    rhamer, Aug 30, 2017
    #6
  7. znelbok

    znelbok

    Joined:
    Aug 3, 2004
    Messages:
    1,151
    Likes Received:
    17
    Yes, that's the easy way out - but I learn nothing from it. That's how I will start for testing etc if I don't work it out.
     
    znelbok, Aug 30, 2017
    #7
  8. znelbok

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,524
    Likes Received:
    173
    Location:
    Adelaide, Australia
    Here is a procedure to add a checksum to the end of a string. You need to put it in the procedures section of the Advanced area:

    Code:
    procedure Checksum(var aString: string);
    var
      i, cs: integer;
    begin
      cs := ord(aString[1]);
      for i := 2 to length(aString) do cs := cs + ord(aString[i]);
      setLength(aString, length(aString)+1);
      aString[length(aString)] := chr(cs and $FF);
    end;
    To use it just call it with a string you have already created. e.g.

    Code:
    myString: string;
    myString := 'G1'#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0#0'1 F';
    checksum(myString);
    What it does:

    Intialize the checksum with the first value. Ord converts a character to an integer (Ord = ordinal value)
    It then steps over the string one character at a time adding each character into the checksum.
    Then it increases the length of the string by one character and puts the checksum truncated to 8 bits in the last position.
     
    Ashley, Aug 30, 2017
    #8
  9. znelbok

    znelbok

    Joined:
    Aug 3, 2004
    Messages:
    1,151
    Likes Received:
    17
    great - ord was what I was looking for.

    Couple of questions just to make sure I am going the right way.

    Variables - can they only be defined int eh Global Variables section.

    If I have a variable that is an internal variable (not set by a group on the network) how do I test the logic by changing its value. off line testing). There are no graphics. Is there a way to change values or do I need to create a screen with some simple buttons to do what I want.
     
    znelbok, Aug 30, 2017
    #9
  10. znelbok

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,524
    Likes Received:
    173
    Location:
    Adelaide, Australia
    Variable used in a Module have to be defined in the Globals section. You can have local variable in functions and procedures, but their values are lost when the function/procedure returns.

    For testing in PICED you can use the Write or WriteLn procedure to print a value in the Output Window at the bottom of the logic screen. (For Wiser you need to select TOOLS/LOGIC OPTIONS/ALLOW USE OF ALL FUNCTIONS FOR TESTING). Unfortunately this is only useful if you use the RUN ONCE option or write the value each scan as the output window is cleared at the start of scan (must put in a bug report for this one day :) ). The logic help file has a section on debugging which gives examples.

    You can also use the LogMessage function to write a string to the log file which I find more useful.

    Finally you have your option which is to create a dummy page to interact with your logic for testing. This is a bit more work but good for complex logic. I generally just create a few temporary user systemio variables and then assign my internal values to them in the logic and use a simple widget to display them. You can use the logic HasChanged functions to test when a user systemio has been modified by the user.
     
    Last edited by a moderator: Aug 31, 2017
    Ashley, Aug 31, 2017
    #10
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.