What exactly to send this device?

Possibilities of the “ECHO-R” flow meter for operation under the MODBUS protocol

  1. Address: from 1 to 247.

  2. Mode: RTU or ASCII. Note. In ASCII mode, in a message addressed to Akron, the interval between adjacent characters must be at least 0.02 s.

  3. Speed, bps: 1200, 2400, 4800, 9600, 19200.

  4. Format.

4.1. Check bit: a) parity; b) odd; c) not used.

4.2. Number of stop bits: 1 or 2 (without using the check bit - only 2).

  1. Perceived commands:

03 - request for the transfer of archival data, or the current values ​​of speed or flow rate, or device settings (in accordance with the specified addresses - see Section 6);

08 00 - request to execute the diagnostic function Return Query Data.

enter image description here

first come across. quite an example. I write(new byte[1]{0x1f},0,1) something like # 232 or write(new byte[1]{0x1f},0,1) to other devices

    1 answer 1

    You expose the settings in the device and accordingly are the same in your program for the connection (for example, mode: Modbus RTU, Speed ​​9600, Check Bit - not used, 1 stop bit). Next, I advise you to use the function of forming the parcel, something like this:

     public static byte[] ReadHoldingRegister(int id, int startAddress, int length) { byte[] data = new byte[8]; byte High, Low; data[0] = Convert.ToByte(id); data[1] = Convert.ToByte(3); byte[] _adr = BitConverter.GetBytes(startAddress-1); data[2] = _adr[1]; data[3] = _adr[0]; byte[] _length = BitConverter.GetBytes(length); data[4] = _length[1]; data[5] = _length[0]; myCRC(data, 6, out High, out Low); data[6] = Low; data[7] = High; return data; } 

    To calculate the checksum, you can use for example the following function:

     public static void myCRC(byte[] message, int length, out byte CRCHigh, out byte CRCLow) { ushort CRCFull = 0xFFFF; for (int i = 0; i < length; i++) { CRCFull = (ushort)(CRCFull ^ message[i]); for (int j = 0; j < 8; j++) { if ((CRCFull & 0x0001) == 0) CRCFull = (ushort)(CRCFull >> 1); else { CRCFull = (ushort)((CRCFull >> 1) ^ 0xA001); } } } CRCHigh = (byte)((CRCFull >> 8) & 0xFF); CRCLow = (byte)(CRCFull & 0xFF); } 

    And then in the program code, calculate the package and send it via the COM port:

     _serialPort.Write(ReadHoldingRegister(123,21,1),0,8); 

    Where: 123 - device number; 21 - the first register (highlighted in the picture); 1 is the length to be counted. Ie, if you write 2, then in the reply package you will also receive 22 regstr (If you believe the screenshot, these are the index codes of the archives of hourly and daily values).