EPICS Controls Argonne National Laboratory

Experimental Physics and
Industrial Control System

1994  1995  1996  1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  2013  2014  2015  2016  2017  2018  2019  2020  2021  2022  <20232024  Index 1994  1995  1996  1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  2013  2014  2015  2016  2017  2018  2019  2020  2021  2022  <20232024 
<== Date ==> <== Thread ==>

Subject: StreamDevice+regex for escaping and unescaping HEX messages
From: Sebastian Eckert via Tech-talk <tech-talk at aps.anl.gov>
To: tech-talk at aps.anl.gov
Date: Fri, 22 Dec 2023 13:59:15 +0100
Dear StreamDevice+regex experts,

I am trying to communicate with a device with a communication protocol, where messages contain:

- 0x0D (Start of a message)

- a source and a destination byte (to assign replies to requests)

- 0x04 or 0x05 or 0x08 (read, write, data)

- address of the register to access (can be multiple bytes)

- data (can be multiple bytes, depending on data type, little endian)

- CRC16 xmodem checksum (without leading 0x0D)

- 0x0A

Before dressing the message with 0x0D and 0x0A, special characters need to be escaped by adding a 0x5E byte before the corresponding byte and increasing the decimal value of the byte by 64 (0x0A to 0x5E 0x4A, 0x0D to 0x5E 0x4D, 0x11 to 0x5E 0x51, 0x13 to 0x5E 0x53, 0x40 to 0x5E 0x80, 0x5E to 0x5E 0x9E). When a message is received, the message (without the leading 0x0D and 0x0A in the end) needs to be un-escaped in the same fashion.

An example for reading a gain value (0x88 0x13 0x00 0x00, signed 32bit integer, escaped: 0x88 0x5E 0x53 0x00 0x00, dec: 5000) is:

out: 0x0D 0xFF 0x03 0x04 0x01 0x03 0x07 0x02 0x74 0xA6 0x0A

in: 0x0D 0x03 0x42 0x08 0x01 0x88 0x5E 0x53 0x00 0x00 0x90 0xA1 0x0A

Here 0x01 0x03 0x07 0x02 is the address of the register we want to read. In the reply only the first byte of the register (0x01), which was read, is mentioned.

Below is the protocol file, in which I escape and unescape the messages with regular expressions. It works correct for sending commands to the device. The received messages are not un-escaped correctly. In the example above, it just replaces the 0x5E by 0x11. My guess is that the 0x5E (^) creates the biggest problems due to its distinct role in regex. The outcome of the byte transformation also drastically depends on the order in which the individual un-escape regex-expressions are applied. I would appreciate any idea of what could by the problem with the expressions.

Best regards!

Sebastian

---------proto file---------------------
Terminator = LF;

preo = 0x0D,0xFF,0x03;
prei = 0x0D,0x03,0x42;
get = 0x04;
write = 0x05;
gain = 0x01,0x03,0x07,0x02;


esco =    "%#/\x5E/\x5E\x9E/"
                    "%#/\x11/\x5E\x51/"
                    "%#/\x13/\x5E\x53/"
                    "%#/\x40/\x5E\x80/"
                    "%#/(?<!^)\x0D/\x5E\x4D/"
                    "%#/\x0A/\x5E\x4A/";

unesci =    "%#/\x5E\x4A/\x0A/"
                    "%#/\x5E\x51/\x11/"
                    "%#/\x5E\x80/\x40/"
                    "%#/\x5E\x4D/\x0D/"
                    "%#/\x5E\x53/\x13/"
                    "%#/\x5E\x9E/\x5E/";

getGain{
 out $preo $get $gain "%1.0<xmodem>" $esco;
 in $unesci $prei BS 0x01 "%#4r" "%1.0<xmodem>";
}

setGain{
 out $preo $write $gain "%#4.4r%1.0<xmodem>" $esco;
 in  $unesci $prei ETX $write 0x01 "%1.0<xmodem>";
 @init{
       out $preo $get $gain "%1.0<xmodem>" $esco;
       in $unesci $prei BS 0x01 "%#4r%1.0<xmodem>";
       }
}

----------db file---------------------
record (longin,"$(P):gain")
{
    field (DTYP, "stream")
    field (INP,  "@syncroStream.proto getGain $(PORT)")
    field (PINI,"YES")
}

record (longout,"$(P):gain_set")
{
    field (DTYP, "stream")
    field (OUT,  "@syncroStream.proto setGain $(PORT)")
}

--
Sebastian Eckert

Helmholtz-Zentrum Berlin für Materialien und Energie

BESSY II - PS-ISRR
Albert-Einstein-Str. 15
12489 Berlin
Germany

E-mail: sebastian.eckert at helmholtz-berlin.de

Tel.: +49 30 8062 14465


Navigate by Date:
Prev: Re: Steps for installing any EPICS Device Support module Eusebio Naif Al-Soliman Fuentes via Tech-talk
Next: Re: Steps for installing any EPICS Device Support module Ralph Lange via Tech-talk
Index: 1994  1995  1996  1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  2013  2014  2015  2016  2017  2018  2019  2020  2021  2022  <20232024 
Navigate by Thread:
Prev: IP520 not communcating with baud rates other than 9600bps Mrinal Bera via Tech-talk
Next: Re: StreamDevice+regex for escaping and unescaping HEX messages Zimoch Dirk via Tech-talk
Index: 1994  1995  1996  1997  1998  1999  2000  2001  2002  2003  2004  2005  2006  2007  2008  2009  2010  2011  2012  2013  2014  2015  2016  2017  2018  2019  2020  2021  2022  <20232024 
ANJ, 23 Dec 2023 Valid HTML 4.01! · Home · News · About · Base · Modules · Extensions · Distributions · Download ·
· Search · EPICS V4 · IRMIS · Talk · Bugs · Documents · Links · Licensing ·