修复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()
# 使用控制传输发送数据到设备
# bmRequestType = 0x40: 主机到设备, 请求类型为 Class, 接口
# bRequest = 0x00: 请求代码(需要参考设备手册)
# wValue, wIndex, data 或 wLength = 参数(需要参考设备手册)
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)
# 使用控制传输从设备读取数据
bmRequestType = 0xC0 # 设备到主机
bRequest = 0x01 # 请求代码(需要参考设备手册)
wValue = 0x0000
wIndex = 0x0000
length = 64 # 读取的字节数
response = dev.ctrl_transfer(bmRequestType, bRequest, wValue, wIndex, length)
print("控制传输读取的数据:", response)
# 释放设备
usb.util.dispose_resources(dev)
0530
# 假设我们构造一个非常基础的广播数据包,这里仅作为示例
# 实际应用中需要根据具体的协议栈和目的进行详细设计
data = b'\x01' # 帧控制字段,这里假设是最简化的表示,实际应用中会更复杂
data += b'\xFF\xFE' # 广播PAN ID
data += b'\xFF\xFF\xFF\xFF\xFF\xFF' # 广播地址
data += b'\x01\x02\x03\x04' # 源地址等,具体取决于你的设计
try:
# 发送数据
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()
# 发送报文函数
def send_packet(packet):
endpoint = dev[0][(0,0)][1]
dev.write(endpoint.bEndpointAddress, packet)
# 接收报文函数
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("开关已关闭")
# 接收来自网关的状态修改命令
response = receive_packet()
if response:
if response[0] == 0x01:
print("接收到网关命令:打开开关")
elif response[0] == 0x00:
print("接收到网关命令:关闭开关")
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
# 写数据到设备
data = b'\x01\x02\x03\x04'
ep_out.write(data)
# 读取设备返回的数据
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