Skip to content

Native TCP Alarm

Native TCP uses a binary frame format for integration, suitable for embedded or industrial control scenarios where HTTP is not convenient.

This chapter describes how to trigger audio-visual alarms on the device through the Native TCP protocol. Although it uses TCP for integration, it actually leverages the Modbus TCP protocol.

This chapter only provides direct demonstrations and does not elaborate on the Modbus TCP protocol. Please search for the Modbus TCP protocol on your own to understand the underlying implementation.

When should you use Native TCP Alarm?

  • When replacing other manufacturers’ devices with this device
  • When the original device uses the TCP protocol
  • When you do not want to modify the original device’s integration method

In summary, in situations where the original integration method is not changed, you only need to modify the data being sent to directly interface with our company’s devices.

A relatively simple integration method. Simply configure the broadcast template and its corresponding ID in the interface, then call the corresponding ID to broadcast.

This method corresponds to Command Template in Modbus TCP.

Prerequisites

  • Enable the Modbus TCP service switch and configure the port number
  • Configure the command list in Modbus TCP. For detailed description of this feature, please refer to Command Template

Invocation Demo

To invoke template 0x01 from the command template, send the following bytes:

0x00 0x01 0x00 0x00 0x00 0x06 0x01 0x06 0x00 0x63 0x00 0x01

The last byte is the template number to be invoked. This method can invoke templates in the range 0x01 to 0xff.

Code Example

The following is a Python script that simulates sending this TCP request. You need to pre-configure template with command number 1 in the Modbus TCP configuration page.

def templateModbusTcp():
'''
Write single register to trigger template alarm (template must be pre-configured)
Response: MBAP header + function code + register address H register address L + register value H register value L
:return:
'''
# Write single register, address 100 (1-based), instruction uses 0-based, so 0x63; command ID = 1
return bytes.fromhex("0001 0000 0006 0106 0063 0001")
# Write single register, address 100, command ID = 2
# return bytes.fromhex("0001 0000 0006 0106 0063 0002")
# Target IP and port
ip = "192.168.0.66"
port = 502
# Create a socket object
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
# raw_data = sendModbusTCP("123 test message - sent at 2024-01-16 14:23:04")
raw_data = templateModbusTcp()
# Establish connection
s.connect((ip, port))
# Send data
s.sendall(bytes(raw_data))
# Receive response data (optional)
response = s.recv(1024)
print("Received:", response.hex())
# Since it's uncertain whether the server returns data, the receive part is commented out
print("Data sent successfully.")

Response Data

0001 0000 0006 0106 0063 0001

A powerful broadcasting method that allows customizing light styles, colors, and broadcast content, but constructing data is relatively complex, implemented using multiple register writes.

This method corresponds to Modbus TCP - Custom Audio-Visual Broadcast

Prerequisites

  • Enable the Modbus TCP service switch and configure the port number

Invocation Demo

Invoke using the following audio-visual alarm parameters

ParameterValue
led_style15
color[“#8A2BE2”]
led_flag[0.03,1]
play_time0
tone0
tts_speed0
textThis is a TCP call demo

Constructing Data

In the data table below, bytes 1 to 13 are the Modbus TCP protocol write multiple registers packet header. 14 to 79 are the specific audio-visual broadcast content. For related descriptions, see Modbus TCP - Address Table

ByteDescriptionExample Value
1-4Header’0x00’, ‘0x01’, ‘0x00’, ‘0x00’
5-6Packet Length
Number of bytes from the next byte to the end
’0x00’, ‘0x49’
7-8Fixed Value’0x01’, ‘0x10’
9-10Fixed Value’0x00’, ‘0x64’
11-12Number of Registers to Write
Audio-Visual Broadcast Content Data Bytes / 2
In this example, content is bytes 14~79
Total 66 bytes
66 / 2 = 33
Hexadecimal: 0x21
’0x00’, ‘0x21’
13Number of Bytes to Write
Number of registers to write * 2
’0x42’
14-15Led style’0x00’, ‘0x0f’,
16-31led_color’0x00’, ‘0x8a’, ‘0x2b’, ‘0xe2’,
‘0x00’, ‘0x00’,‘0x00’, ‘0x00’,
‘0x00’, ‘0x00’, ‘0x00’, ‘0x00’,
‘0x00’, ‘0x00’, ‘0x00’, ‘0x00’
32-33play_time Broadcast Duration’0x00’, ‘0x00’
34-35tone Ringtone Number’0x00’, ‘0x00’
36-37tts_speed TTS Speed’0x00’, ‘0x00’
38-39led_style Style Parameters’0x03’, ‘0x01’
40-43Reserved Bytes’0x00’, ‘0x00’, ‘0x00’, ‘0x00’
44-47Text Content - “这“‘0x00’, ‘0xe8’, ‘0xbf’, ‘0x99’
48-51Text Content - “是“‘0x00’, ‘0xe6’, ‘0x98’, ‘0xaf’
52-55Text Content - “T“‘0x00’, ‘0x00’, ‘0x00’, ‘0x54’
56-59Text Content - “C“‘0x00’, ‘0x00’, ‘0x00’, ‘0x43’
60-63Text Content - “P“‘0x00’, ‘0x00’, ‘0x00’, ‘0x50
64-67Text Content - “调“‘0x00’, ‘0xe8’, ‘0xb0’, ‘0x83’,
68-71Text Content - “用“‘0x00’, ‘0xe7’, ‘0x94’, ‘0xa8’,
72-75Text Content - “演“‘0x00’, ‘0xe6’, ‘0xbc’, ‘0x94’
76-79Text Content - “示“‘0x00’, ‘0xe7’, ‘0xa4’, ‘0xba’

Combined Data

[‘0x00’, ‘0x01’, ‘0x00’, ‘0x00’, ‘0x00’, ‘0x49’, ‘0x01’, ‘0x10’, ‘0x00’, ‘0x64’, ‘0x00’, ‘0x21’, ‘0x42’, ‘0x00’, ‘0x0f’, ‘0x00’, ‘0x8a’, ‘0x2b’, ‘0xe2’, ‘0x00’, ‘0x00’, ‘0x00’, ‘0x00’, ‘0x00’, ‘0x00’, ‘0x00’, ‘0x00’, ‘0x00’, ‘0x00’, ‘0x00’, ‘0x00’, ‘0x00’, ‘0x00’, ‘0x00’, ‘0x00’, ‘0x00’, ‘0x00’, ‘0x03’, ‘0x01’, ‘0x00’, ‘0x00’, ‘0x00’, ‘0x00’, ‘0x00’, ‘0xe8’, ‘0xbf’, ‘0x99’, ‘0x00’, ‘0xe6’, ‘0x98’, ‘0xaf’, ‘0x00’, ‘0x00’, ‘0x00’, ‘0x54’, ‘0x00’, ‘0x00’, ‘0x00’, ‘0x43’, ‘0x00’, ‘0x00’, ‘0x00’, ‘0x50’, ‘0x00’, ‘0xe8’, ‘0xb0’, ‘0x83’, ‘0x00’, ‘0xe7’, ‘0x94’, ‘0xa8’, ‘0x00’, ‘0xe6’, ‘0xbc’, ‘0x94’, ‘0x00’, ‘0xe7’, ‘0xa4’, ‘0xba’]

Python Code Example

import socket
import time
write_multi_reg_header = [0x0001, 0x0000, 0x0000, 0x0110]
'''
MBAP header format:
Example Length (byte) Description
0x0001 2 Can always be 1; the response will echo this value
0x0000 2 Fixed value
0x0006 2 Byte count from the next byte to the end
0x01 1 Fixed value, use 1
0x10 1 Function code, see Modbus TCP specification
'''
def wordList2byteList(l):
'''
Convert a list of 16-bit words to a list of single bytes
:param l:
:return:
'''
byte_list = []
for word in l:
byte_list.append(word >> 8)
byte_list.append(word & 0xff)
return byte_list
def createTcpPkg(start_addr, reg_number, data:list):
'''
Create a write-multiple-registers package
Response: MBAP header + function code + start address H start address L + register count H register count L (12 bytes total)
:param start_addr:
:param reg_number: 16-bit value, e.g. 0x000f
:param addr: list of 16-bit values
:return:
'''
# Packet header
pkg = write_multi_reg_header
# Update header length: byte count of request data + 7
pkg[2] = len(data)*2 + 7
# Write multiple registers: start address (2 bytes)
pkg += [start_addr, reg_number]
# Convert previous data to a byte list
byte_list = wordList2byteList(pkg)
# Byte count to write (1 byte)
byte_list.append(len(data) * 2 & 0xff)
byte_list += wordList2byteList(data)
return byte_list
def encodeStr(s):
'''
Encode string to register values
:param s:
:return:
'''
raw = []
for bit in s:
bit = int.from_bytes(bit.encode('utf-8'), 'big')
word = [0] * 2
word[0] = (0xffff0000 & bit) >> 16
word[1] = 0xffff & bit
raw += word
return raw
# Custom command demo
def sendModbusTCP(tts):
'''
Send custom command via TCP
:param tts:
:return:
'''
data_arr = []
led_color = [0] * 8
# LED style
led_style = 15
# LED color
led_color[0] = 0x8A
led_color[1] = 0x2be2
play_time = 0
tone = 0
tts_speed = 0
led_flag = 0x0301
empty = [0] * 2
tts_str = encodeStr(tts)
data_arr += [led_style] + led_color + [play_time] + [tone] + [tts_speed] + [led_flag] + empty + tts_str
print('Broadcast Payload:')
print([f"0x{x:04x}" for x in data_arr])
print('Payload length:', len(data_arr))
raw_data = createTcpPkg(100, len(data_arr), data_arr)
print('Encapsulated raw data:')
print([f"0x{x:02x}" for x in raw_data])
print(len(raw_data))
return raw_data
def templateModbusTcp():
'''
Write single register to trigger template alarm (template must be pre-configured)
Response: MBAP header + function code + register address H register address L + register value H register value L
:return:
'''
# Write single register, address 100 (1-based), instruction uses 0-based, so 0x63; command ID = 1
return bytes.fromhex("0001 0000 0006 0106 0063 0001")
# Write single register, address 100, command ID = 2
# return bytes.fromhex("0001 0000 0006 0106 0063 0002")
# Target IP and port
ip = "192.168.0.216"
port = 502
# Create a socket object
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
raw_data = sendModbusTCP("This is a TCP call demo")
# raw_data = templateModbusTcp()
# Establish connection
s.connect((ip, port))
# Send data
s.sendall(bytes(raw_data))
# Receive response data (optional)
response = s.recv(1024)
print("Received:", response.hex())
# Since it's uncertain whether the server returns data, the receive part is commented out
print("Data sent successfully.")

Response Data

The last two bytes represent the number of registers written. In this example, it is 0x0021; the rest remains unchanged.

0x0001 0x0000 0x0006 0x0110 0x0064 0x0021