修復json
echo '{key1: value1, key2: value2}' | sed 's/\([a-zA-Z0-9_]\+\)\s*:/\"\1\":/g' | sed 's/: \([^,}]\+\)/: "\1"/g'
echo '{key1: value1, key2: {key3: value3, key4: [value4, value5]}, key5: [value6, {key6: value7}]}' | \
sed -E 's/([a-zA-Z0-9_]+):/\1":/g; s/: ([^,}\]:\[]+)/: "\1"/g'
import usb.core
import usb.util
# 查找 CC2531EMK 設備(通常供應商ID和產品ID為0x0451:0x16a8)
dev = usb.core.find(idVendor=0x0451, idProduct=0x16a8)
if dev is None:
? ? raise ValueError('設備未找到')
# 使設備脫離內核驅動程序
if dev.is_kernel_driver_active(0):
? ? dev.detach_kernel_driver(0)
# 設置設備配置
dev.set_configuration()
# 使用控制傳輸發(fā)送數(shù)據(jù)到設備
# bmRequestType = 0x40: 主機到設備, 請求類型為 Class, 接口
# bRequest = 0x00: 請求代碼(需要參考設備手冊)
# wValue, wIndex, data 或 wLength = 參數(shù)(需要參考設備手冊)
bmRequestType = 0x40
bRequest = 0x00
wValue = 0x0000
wIndex = 0x0000
data = b'\x01\x02\x03\x04'
response = dev.ctrl_transfer(bmRequestType, bRequest, wValue, wIndex, data)
print("控制傳輸響應:", response)
# 使用控制傳輸從設備讀取數(shù)據(jù)
bmRequestType = 0xC0? # 設備到主機
bRequest = 0x01? ? ? # 請求代碼(需要參考設備手冊)
wValue = 0x0000
wIndex = 0x0000
length = 64? ? ? ? ? # 讀取的字節(jié)數(shù)
response = dev.ctrl_transfer(bmRequestType, bRequest, wValue, wIndex, length)
print("控制傳輸讀取的數(shù)據(jù):", response)
# 釋放設備
usb.util.dispose_resources(dev)
0530
# 假設我們構造一個非?;A的廣播數(shù)據(jù)包,這里僅作為示例
# 實際應用中需要根據(jù)具體的協(xié)議棧和目的進行詳細設計
data = b'\x01' # 幀控制字段,這里假設是最簡化的表示,實際應用中會更復雜
data += b'\xFF\xFE' # 廣播PAN ID
data += b'\xFF\xFF\xFF\xFF\xFF\xFF' # 廣播地址
data += b'\x01\x02\x03\x04' # 源地址等,具體取決于你的設計
try:
? ? # 發(fā)送數(shù)據(jù)
? ? dev.write(ep_out, data, timeout=1000)
? ? print("Data Sent")
except usb.core.USBError as e:
? ? print(f"Error: {e}")
0527
import serial
import time
def init_serial(port, baudrate=115200):
? ? ser = serial.Serial(port, baudrate)
? ? if ser.is_open:
? ? ? ? print(f"Opened {ser.name} successfully")
? ? else:
? ? ? ? print(f"Failed to open {ser.name}")
? ? return ser
def send_command(ser, command):
? ? ser.write(command.encode())
? ? time.sleep(0.1)
? ? response = ser.read_all()
? ? print(f"Received: {response}")
def main():
? ? # 修改為你的串口端口
? ? port = '/dev/ttyUSB0'
? ? ser = init_serial(port)
? ? try:
? ? ? ? while True:
? ? ? ? ? ? command = input("Enter command to send to CC2531: ")
? ? ? ? ? ? if command.lower() == 'exit':
? ? ? ? ? ? ? ? break
? ? ? ? ? ? send_command(ser, command)
? ? finally:
? ? ? ? ser.close()
? ? ? ? print("Serial port closed")
if __name__ == "__main__":
? ? main()
0529
import usb.core
import usb.util
import time
# 初始化USB設備
dev = usb.core.find(idVendor=0x0451, idProduct=0x16ae)
if dev is None:
? ? raise ValueError('設備未找到')
# 設置設備配置
dev.set_configuration()
# 發(fā)送報文函數(shù)
def send_packet(packet):
? ? endpoint = dev[0][(0,0)][1]
? ? dev.write(endpoint.bEndpointAddress, packet)
# 接收報文函數(shù)
def receive_packet():
? ? endpoint = dev[0][(0,0)][0]
? ? try:
? ? ? ? data = dev.read(endpoint.bEndpointAddress, endpoint.wMaxPacketSize, timeout=100)
? ? ? ? return data
? ? except usb.core.USBError:
? ? ? ? return None
# 模擬Zigbee開關
def simulate_switch():
? ? while True:
? ? ? ? command = input("輸入 'on' 打開開關 或 'off' 關閉開關:")
? ? ? ? if command == 'on':
? ? ? ? ? ? zigbee_packet = [0x01]? # 開關打開報文
? ? ? ? ? ? send_packet(zigbee_packet)
? ? ? ? ? ? print("開關已打開")
? ? ? ? elif command == 'off':
? ? ? ? ? ? zigbee_packet = [0x00]? # 開關關閉報文
? ? ? ? ? ? send_packet(zigbee_packet)
? ? ? ? ? ? print("開關已關閉")
? ? ? ?
? ? ? ? # 接收來自網(wǎng)關的狀態(tài)修改命令
? ? ? ? response = receive_packet()
? ? ? ? if response:
? ? ? ? ? ? if response[0] == 0x01:
? ? ? ? ? ? ? ? print("接收到網(wǎng)關命令:打開開關")
? ? ? ? ? ? elif response[0] == 0x00:
? ? ? ? ? ? ? ? print("接收到網(wǎng)關命令:關閉開關")
if __name__ == "__main__":
? ? simulate_switch()
import usb.core
import usb.util
# 查找 CC2531EMK 設備(通常供應商ID和產品ID為0x0451:0x16a8)
dev = usb.core.find(idVendor=0x0451, idProduct=0x16a8)
if dev is None:
? ? raise ValueError('設備未找到')
# 使設備脫離內核驅動程序
if dev.is_kernel_driver_active(0):
? ? dev.detach_kernel_driver(0)
# 設置設備配置
dev.set_configuration()
# 獲取設備的第一個接口
cfg = dev.get_active_configuration()
interface_number = cfg[(0, 0)].bInterfaceNumber
intf = usb.util.find_descriptor(cfg, bInterfaceNumber=interface_number)
# 尋找輸入和輸出端點
ep_out = usb.util.find_descriptor(intf, custom_match=lambda e: usb.util.endpoint_direction(e.bEndpointAddress) == usb.util.ENDPOINT_OUT)
ep_in = usb.util.find_descriptor(intf, custom_match=lambda e: usb.util.endpoint_direction(e.bEndpointAddress) == usb.util.ENDPOINT_IN)
# 確保我們找到了端點
assert ep_out is not None
assert ep_in is not None
# 寫數(shù)據(jù)到設備
data = b'\x01\x02\x03\x04'
ep_out.write(data)
# 讀取設備返回的數(shù)據(jù)
response = ep_in.read(64)
print("Response:", response)
# 釋放設備
usb.util.release_interface(dev, interface_number)
dev.attach_kernel_driver(interface_number)
import usb.core
import usb.util
# 查找 CC2531EMK 設備(通常供應商ID和產品ID為0x0451:0x16a8)
dev = usb.core.find(idVendor=0x0451, idProduct=0x16a8)
if dev is None:
? ? raise ValueError('設備未找到')
# 使設備脫離內核驅動程序
if dev.is_kernel_driver_active(0):
? ? dev.detach_kernel_driver(0)
# 設置設備配置
dev.set_configuration()
# 枚舉所有配置
for cfg in dev:
? ? print(f'配置值: {cfg.bConfigurationValue}')
? ? for intf in cfg:
? ? ? ? print(f'接口編號: {intf.bInterfaceNumber}, 備用設置: {intf.bAlternateSetting}')
? ? ? ? for ep in intf:
? ? ? ? ? ? direction = 'IN' if usb.util.endpoint_direction(ep.bEndpointAddress) == usb.util.ENDPOINT_IN else 'OUT'
? ? ? ? ? ? print(f'? 端點地址: {ep.bEndpointAddress}, 方向: {direction}, 類型: {ep.bmAttributes & usb.ENDPOINT_TYPE_MASK}')
# 釋放設備
usb.util.dispose_resources(dev)
https://software-dl.ti.com/simplelink/esd/simplelink_cc13x2_26x2_sdk/3.10.01.11/exports/docs/zigbee/doxygen/zigbee/html/index.html