Support for string variable in MODBUS TCP Adapter
From Datonis
Use Case:
Read the ISBN barcode as string from the MODBUS using Datonis Edge Modbus Adapter and send it to Datonis. ISBN barcode is of 13 character string and these 13 characters are stored in 7 registers of Modbus having base address as 1.
e.g. ISBN barcode string is "9781400067909" and its data(in decimal format) in 7 registers of MODBUS are 14647 14385 13360 12336 13879 14640 14592 respectively.
Solution:
There are following steps through which you can achieve above use case:
- First create the four read tags tag_chars_1_4, tag_chars_5_8, tag_chars_9_12 and tag_chars_13 inside MODBUS config json 'thing_configs -> source_tags -> read_tags'. These will store the 1st to 4th character, 5th to 8th character, 9th to 12th character and 13th character of ISBN barcode respectively.
- Set the read_count for first three read tags as 2(to read 4 characters of barcode) and for last read tag as 1(to read 2 characters of barcode).
- Set the proper ref_addr for each read tags as shown in below sample MODBUS config json. Here it is assumed that base address for ISBN barcode string in MODBUS is 1.
- Set the val_type as long for each read tags.
- Set the other required config in read tags if required.
- Now create a derived tag barcode inside thing_configs -> derived_tags. Set the expression_js as shown in sample MODBUS config json. This expression has a toStr function which convert the input long to string of given count length. Expression toStr(tag_chars_1_4,4) will return the 4 character string which is "9781" in our case. Similarly toStr(tag_chars_5_8,4) and toStr(tag_chars_9_12,4) will return "4000" and "6790". Expression toStr(tag_chars_13_14,2).substring(0,1) will return "9". Now concatenate the all 4 string to make the full ISBN barcode.
- Now map this derived barcode with datonis metric inside thing_configs -> metric_mappings.
Sample MODBUS config json:
{
"adapter_type": "com.altizon.gateway.modbus.ModbusAdapter", "tag_suffixes": [], "thing_configs": [{ "thing_key": "a8629c99cc", "name": "Test Job", "description": "Job Description", "bi_directional": true, "config_mode": "read_write", "send_interval_ms": 5000, "scan_interval_ms": 1000, "heartbeat_interval_ms": 120000, "source_tags": [{ "protocol_connection": { "protocol": "TCP", "ip_address": "127.0.0.1", "port": 8502 }, "read_tags": [{ "tag_id": "tag_chars_1_4", "tag_type": "InputRegister", "val_type": "long", "device_id": 1, "read_count": 2, "ref_addr": 1, "endian": true }, { "tag_id": "tag_chars_5_8", "tag_type": "InputRegister", "val_type": "long", "device_id": 1, "read_count": 2, "ref_addr": 3, "endian": true }, { "tag_id": "tag_chars_9_12", "tag_type": "InputRegister", "val_type": "long", "device_id": 1, "read_count": 2, "ref_addr": 5, "endian": true }, { "tag_id": "tag_chars_13_14", "tag_type": "InputRegister", "val_type": "long", "device_id": 1, "read_count": 1, "ref_addr": 7, "endian": true } ], "write_tags": [] }], "ml_tags": [], "derived_tags": [{ "tag_id": "barcode", "expression_js": "toStr=function(/*long*/long, count) {var str=; for ( var index = 0; index < count; index ++ ) {var byte = long & 0xff;str = String.fromCharCode(byte)+str;long = (long - byte) / 256 ;} return str}; toStr(tag_chars_1_4,4)+toStr(tag_chars_5_8,4)+toStr(tag_chars_9_12,4)+toStr(tag_chars_13_14,2).substring(0,1)" }], "monitor_tag_ids": [], "scan_tag_ids": [], "metric_mappings": [{ "metric_id": "barcode", "tag_id": "barcode", "type": "string" }] }]
}