Location>code7788 >text

Modbus ASCII Get Data

Popularity:484 ℃/2024-08-27 17:04:44

Reading data according to Galaxy HLT protocols

1. Content of the agreement

8.1: Introduction to communication protocols
8.1.5 Communication Settings
This protocol uses asynchronous serial communication with 1 start bit, 8 data bits, 2 stop bits, and no parity.
This protocol uses an asynchronous serial communication method with 1 start bit, 8 data bits, 2 stop bits, and no parity data communication format, in which the data bits are 8 bits, with the first bit being the highest bit (MSB) and the eighth bit being the lowest bit (LSB).
The first bit is the highest bit (MSB) and the eighth bit is the lowest bit (LSB).
8.1.6 Data format
The communication uses direct access to the data in the S7-200 V memory, and all parameters are placed in the S7-200 V memory. The data types in the S7-200 are BYTE (byte), INT (signed integer), WORD (unsigned integer), DWORD (unsigned long integer), LONG (signed long integer), FLOAT (floating point), etc. The format of the data in the S7-200 V memory is BYTE (byte).
(floating-point number) format, bit operation by byte mode operation, a byte 8 bits. The length of BYTE is 1 byte; the length of INT and WT is 1 byte.
BYTE length of 1 byte; INT and WORD for 2 bytes, first high then low; DWORD and LONG for 4 bytes, first high then low; floating-point numbers for 4 bytes, first high then low; floating-point numbers for 4 bytes.
DWORD and LONG are 4 bytes, high first, then low; floating point numbers are 4 bytes. Detailed information can be found in the Siemens SIMATIC S7-200 Programmable Controller System Manual.
System Manual of Siemens SIMATIC S7-200 Programmable Control System.
8.1.7 Read Parameter Command
Command Format: >MMVDUUUUr
MM: Machine No. (machine number).
MM: machine number (should be converted to hexadecimal data during programming)
VD: Read command
UUUU: S7200 internal V memory address (to be converted to hexadecimal data during programming)
UUUU: S7200 internal V memory address (converted to hexadecimal data during programming)
r: Carriage return character

Response data format: <MMVDUUUUUaabbccddee...r
<MMVDUUUUU: response command header
aabbccddee: 128 bytes starting from the specified V-unit, every 2 characters represent one byte.
r: carriage return character

8.1.8 PLC address description

Parameter name V address unit data format Data description
operational state 904 int 0 Stop 1 Run 2 Pause
Temperature measurement 804 float Unit: °C
Humidity measurement 860 float Unit: RH%

2. Use the serial port assistant debugging

  1. Serial Assistant Setup
serial port parameter parameter value
baud 9600
data bit 8
stop bit 2
check digit not have
flow control not have
  1. Debugging Screenshots
    img
    Command Description To add a carriage return to the command, otherwise the PLC will not recognize the command.
>00VD0388 :Obtaining Device Operational Status Return results: <00VD0388000000010000000200040000000000000000000000000000000000000000000000000000000000000000000000000000000000FA03B6000000FA0000FDDA0000
>00VD0324 :Getting Temperature Measurements Return results:<00VD032441EA980041EABDD541F6580041FBE0000000000044864400447EBA00000000000000000000000000000000000000000041C80000000000000000000000000000
>00VD035C :Getting humidity measurements Return results:<00VD035C00000000000000000000000001000400000000000000000000000000000000000000000000000000000000000000000100000002000400000000000000000000

According to the result returned by the command, parse the data to get the temperature measurement value and humidity measurement value.

1. Since the return result is in hexadecimal, you need to state the hexadecimal.

2. Temperature measurement value and humidity measurement value in the return result of the location were: 41EA9800 and 00000000, need to convert these two values to float type.
3. The running status in the return result is: 0000 (4 bits after the intercept command), converted to an integer value of 0.

4. The temperature measurement value is 41EA9800 (8 bits after the intercept command), which is converted to floating point number: 29.324219.

5. The humidity measurement value is 00000000 (8 bits after the intercept command), which is converted to a floating point number: 0.

# Use SerialPort to send commands to receive data

  1. Initialize serial port information and open the serial port
        private void OpenCom()
        {

            // Create the SerialPort object
            serialPort = new SerialPort((), 9600, , 8, );

             = 1000; // Serial port read timeout
             = 1000; // Serial port write timeout
            // Open the serial port
            ();
        }
  1. Send commands and receive data
Focus: converting a string to an array of ASCII bytes byte[] asciiBytes = (request); Data cannot be received correctly if it is not sent through an ASCII byte array.

img

public string SendOrderToPLC(string request)
        {
            string txt = ;
            try
            {
                OpenCom();
                 
                // Convert strings toASCIIcode byte array
                byte[] asciiBytes = (request);

                // Send Byte Array
                (asciiBytes, 0, );
                //Waiting for data reception to complete Waiting time can be small with small amount of data,Long waiting time for large amount of data
                (1000);

                ASCIIEncoding ascii = new ASCIIEncoding();
                byte[] readBuffer = new byte[];
                (readBuffer, 0, );

                if ( > 14)
                {
                    txt = (readBuffer);

                    // Close the serial port
                    ();
                    return txt;
                }
                else
                {
                    (new Action(() =>
                    {
                         = "No response received";
                    }));
                    ();
                    return txt;
                }
            }
            catch (Exception ex)
            {
                txt = null;
                (new Action(() =>
                {
                     = ;
                }));
                return txt;
            }
        }
  1. Processing data
     Processing device operation status data
    //Obtain the running status of the device, which is an integer, so take 4 bits in hexadecimal system; exclude the previous <00VD0388 command.
    /*0 Stop 1 Run 2 Pause
     */
    var stringSub = (9, 4); var st = HexToData = HexToData; var st = HexToData
    var st = HexToDecimal(stringSub);

    Processing temperature data
    /* Temperature is taken as a floating point number so hexadecimal takes 8 bits; exclude the previous < 00VD0324 command Unit: °C
      */
    var stringSub = (9, 8); var st = HexToData = HexToData; var st = HexToData
    var st = HexToDecimal(stringSub);


    /* Humidity is taken as a floating point number, so hexadecimal takes 8 bits; exclude the previous < 00VD035C command Unit: RH%
      */
    var stringSub = (9, 8);
    var st = HexToDecimal(stringSub);

4. Data conversion methods

        /// <summary>
        /// commander-in-chief (military)16Converting Progressive Strings to Floating Point Numbers
        /// </summary>
        /// <param name="hex"></param>
        /// <returns></returns>
        static double HexToDecimal(string hex)
        {
            uint intValue = Convert.ToUInt32(hex, 16);

            // commander-in-chief (military)整数值转换为单精度浮点数
            float floatValue = ((intValue), 0);

            // output result
            ("Hex: " + hex);
            ("Float: " + floatValue);
            return floatValue;
        }