# Modbus Query

## Modbus query command

```
[[Modbus query 1],[Modbus query 2],.....]
```

## Things required to make Modbus query

* Slave ID
* Function code
* Starting address
* How many registers/coils want to be read
* Data type
* Index

## Modbus query format

\[ Slave ID, Function code, Starting address, No. of registers/coils want to read, Data type, Index]

## How to choose function code

It depends on starting address of the register or coil.

| Parameter              | Register / Coil Address | Function Code |
| ---------------------- | ----------------------- | ------------- |
| Coil status            | 00001 on words          | 1             |
| Input Coil status      | 10001 on words          | 2             |
| Holding register       | 40001 on words          | 3             |
| Input holding register | 30001 on words          | 4             |

## How to choose starting address & number of registers

<figure><img src="https://1313992628-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FdTvSz7jpW1e3wAaLdma4%2Fuploads%2FCmOtNKvON8AHq7mTxvdd%2Fregister.jpg?alt=media&#x26;token=d430bd05-0904-4084-83cf-7093c89f6990" alt=""><figcaption></figcaption></figure>

* For eg: if we want to read the parameters like `Watts Total`, `Watts R phase`, `Watts Y phase`, `Watts B phase`, and `VAR Total` from the Elmeasure `EN8400` Energy meter then its address is from 40101 to 40110. so now the starting address would be 100 & no. of registers want to read would be 10.
* `NOTE: The number of register would be 2 in order to read single parameter of data type float, UINT32, INT32 & for rest it would be 1.`

## How to choose the data type

* Supported data types & formats are:

  | Datatypes case no | Datatype   | Remark                                                                                                                                                                            |
  | ----------------- | ---------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
  | 0                 | Float ABCD |                                                                                                                                                                                   |
  | 1                 | Float DCBA |                                                                                                                                                                                   |
  | 2                 | Float BADC |                                                                                                                                                                                   |
  | 3                 | Float CDAB | Generally, the datatype mentioned in the datasheet will be without data format\[A, B, C, D sequence], for example, `datatype is float`. In that case, use this format\[default].  |
  | 4                 | UINT ABCD  |                                                                                                                                                                                   |
  | 5                 | UNIT DCBA  |                                                                                                                                                                                   |
  | 6                 | UINT BADC  |                                                                                                                                                                                   |
  | 7                 | UINT CDAB  | Generally, the datatype mentioned in the datasheet will be without data format\[A, B, C, D sequence], for example, `datatype is UINT32`. In that case, use this format\[default]. |
  | 8                 | INT ABCD   |                                                                                                                                                                                   |
  | 9                 | INT DCBA   |                                                                                                                                                                                   |
  | 10                | INT BADC   |                                                                                                                                                                                   |
  | 11                | INT CDAB   | Generally, the datatype mentioned in the datasheet will be without data format\[A, B, C, D sequence], for example, `datatype is INT32`. In that case, use this format\[default].  |
  | 12                | INT AB     | Generally, the datatype mentioned in the datasheet will be without data format\[A, B sequence], for example, `datatype is INT16`. In that case, use this format\[default].        |
  | 13                | INT BA     |                                                                                                                                                                                   |
  | 14                | UINT AB    | Generally, the datatype mentioned in the datasheet will be without data format\[A, B sequence], for example, `datatype is UINT16`. In that case, use this format\[default].       |
  | 15                | UINT BA    |                                                                                                                                                                                   |
* In case of Coil status or input Coil status data type is not mandatory, we can choose default data type 0.
* `Note: In some exceptional cases even after selecting the [default] data format, we will observe that we are not receiving data from the slave in that scenario we have to do a trial & error by selecting a different data format until we will receive the data`.

## Index

Index number is nothing but a number of queries you have? The first query index is 0.

### Example Queries

#### Schneider meter EM6436

UART: `{"action":"modbus", "mb_mode":"1", "baud":"4800", "data_bits":"8", "parity":"EVEN", "stop_bits":"1", "mb_interval":"2000", "mb_timeout":"1000"}`

`[[1,3,3000,20,3,0], [1,3,3030,20,3,1], [1,3,3060,20,3,2], [1,3,3090,20,3,3], [1,3,3120,4,3,4], [1,3,3150,4,3,5]]`
