Native TCP Alarm
Native TCP uses a binary frame format for integration, suitable for embedded or industrial control scenarios where HTTP is not convenient.
Important Notes
Section titled "Important Notes"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.
Command Template Broadcast
Section titled "Command Template Broadcast"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 portip = "192.168.0.66"port = 502
# Create a socket objectwith 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
Custom Audio-Visual Broadcast
Section titled "Custom Audio-Visual Broadcast"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
| Parameter | Value |
|---|---|
| led_style | 15 |
| color | [“#8A2BE2”] |
| led_flag | [0.03,1] |
| play_time | 0 |
| tone | 0 |
| tts_speed | 0 |
| text | This 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
| Byte | Description | Example Value |
|---|---|---|
| 1-4 | Header | ’0x00’, ‘0x01’, ‘0x00’, ‘0x00’ |
| 5-6 | Packet Length Number of bytes from the next byte to the end | ’0x00’, ‘0x49’ |
| 7-8 | Fixed Value | ’0x01’, ‘0x10’ |
| 9-10 | Fixed Value | ’0x00’, ‘0x64’ |
| 11-12 | Number of Registers to WriteAudio-Visual Broadcast Content Data Bytes / 2In this example, content is bytes 14~79Total 66 bytes66 / 2 = 33Hexadecimal: 0x21 | ’0x00’, ‘0x21’ |
| 13 | Number of Bytes to Write Number of registers to write * 2 | ’0x42’ |
| 14-15 | Led style | ’0x00’, ‘0x0f’, |
| 16-31 | led_color | ’0x00’, ‘0x8a’, ‘0x2b’, ‘0xe2’, ‘0x00’, ‘0x00’,‘0x00’, ‘0x00’, ‘0x00’, ‘0x00’, ‘0x00’, ‘0x00’, ‘0x00’, ‘0x00’, ‘0x00’, ‘0x00’ |
| 32-33 | play_time Broadcast Duration | ’0x00’, ‘0x00’ |
| 34-35 | tone Ringtone Number | ’0x00’, ‘0x00’ |
| 36-37 | tts_speed TTS Speed | ’0x00’, ‘0x00’ |
| 38-39 | led_style Style Parameters | ’0x03’, ‘0x01’ |
| 40-43 | Reserved Bytes | ’0x00’, ‘0x00’, ‘0x00’, ‘0x00’ |
| 44-47 | Text Content - “这“ | ‘0x00’, ‘0xe8’, ‘0xbf’, ‘0x99’ |
| 48-51 | Text Content - “是“ | ‘0x00’, ‘0xe6’, ‘0x98’, ‘0xaf’ |
| 52-55 | Text Content - “T“ | ‘0x00’, ‘0x00’, ‘0x00’, ‘0x54’ |
| 56-59 | Text Content - “C“ | ‘0x00’, ‘0x00’, ‘0x00’, ‘0x43’ |
| 60-63 | Text Content - “P“ | ‘0x00’, ‘0x00’, ‘0x00’, ‘0x50 |
| 64-67 | Text Content - “调“ | ‘0x00’, ‘0xe8’, ‘0xb0’, ‘0x83’, |
| 68-71 | Text Content - “用“ | ‘0x00’, ‘0xe7’, ‘0x94’, ‘0xa8’, |
| 72-75 | Text Content - “演“ | ‘0x00’, ‘0xe6’, ‘0xbc’, ‘0x94’ |
| 76-79 | Text 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 socketimport time
write_multi_reg_header = [0x0001, 0x0000, 0x0000, 0x0110]
'''MBAP header format:Example Length (byte) Description0x0001 2 Can always be 1; the response will echo this value0x0000 2 Fixed value0x0006 2 Byte count from the next byte to the end0x01 1 Fixed value, use 10x10 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 demodef 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 portip = "192.168.0.216"port = 502
# Create a socket objectwith 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