|
|
|
|
@ -0,0 +1,499 @@
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
# 基礎功能的 import
|
|
|
|
|
import asyncio
|
|
|
|
|
import serial_asyncio
|
|
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
import sys
|
|
|
|
|
import serial
|
|
|
|
|
import signal
|
|
|
|
|
from enum import Enum, auto
|
|
|
|
|
|
|
|
|
|
# # XBee 模組
|
|
|
|
|
# from xbee.frame import APIFrame
|
|
|
|
|
|
|
|
|
|
# 自定義的 import
|
|
|
|
|
from .utils import setup_logger
|
|
|
|
|
|
|
|
|
|
# ====================== 分割線 =====================
|
|
|
|
|
|
|
|
|
|
logger = setup_logger(os.path.basename(__file__))
|
|
|
|
|
|
|
|
|
|
# ====================== 分割線 =====================
|
|
|
|
|
class XBeeFrameHandler:
|
|
|
|
|
"""XBee API Frame 處理器"""
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def parse_at_command_response(frame: bytes) -> dict:
|
|
|
|
|
"""解析 AT Command Response (0x88)"""
|
|
|
|
|
if len(frame) < 8:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
frame_type = frame[3]
|
|
|
|
|
if frame_type != 0x88:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
frame_id = frame[4]
|
|
|
|
|
at_command = frame[5:7]
|
|
|
|
|
status = frame[7]
|
|
|
|
|
data = frame[8:] if len(frame) > 8 else b''
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
'frame_id': frame_id,
|
|
|
|
|
'command': at_command,
|
|
|
|
|
'status': status,
|
|
|
|
|
'data': data,
|
|
|
|
|
'is_ok': status == 0x00
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def parse_receive_packet(frame: bytes) -> dict:
|
|
|
|
|
# """解析 RX Packet (0x90) - 未來擴展用"""
|
|
|
|
|
# if len(frame) < 15 or frame[3] != 0x90:
|
|
|
|
|
# return None
|
|
|
|
|
|
|
|
|
|
# return {
|
|
|
|
|
# 'source_addr': frame[4:12],
|
|
|
|
|
# 'reserved': frame[12:14],
|
|
|
|
|
# 'options': frame[14],
|
|
|
|
|
# 'data': frame[15:-1]
|
|
|
|
|
# }
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def encapsulate_data(data: bytes, dest_addr64: bytes, frame_id=0x01) -> bytes:
|
|
|
|
|
"""
|
|
|
|
|
將數據封裝為 XBee API 傳輸幀
|
|
|
|
|
|
|
|
|
|
使用 XBee API 格式封裝數據:
|
|
|
|
|
- 傳輸請求幀 (0x10)
|
|
|
|
|
- 使用廣播地址
|
|
|
|
|
- 添加適當的頭部和校驗和
|
|
|
|
|
"""
|
|
|
|
|
frame_type = 0x10
|
|
|
|
|
dest_addr16 = b'\xFF\xFE'
|
|
|
|
|
broadcast_radius = 0x00
|
|
|
|
|
options = 0x00
|
|
|
|
|
|
|
|
|
|
frame = struct.pack(">B", frame_type) + struct.pack(">B", frame_id)
|
|
|
|
|
frame += dest_addr64 + dest_addr16
|
|
|
|
|
frame += struct.pack(">BB", broadcast_radius, options) + data
|
|
|
|
|
checksum = 0xFF - (sum(frame) & 0xFF)
|
|
|
|
|
return b'\x7E' + struct.pack(">H", len(frame)) + frame + struct.pack("B", checksum)
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def decapsulate_data(data: bytes):
|
|
|
|
|
# 這裡可以根據需要進行數據解封裝
|
|
|
|
|
|
|
|
|
|
# XBee API 幀格式:
|
|
|
|
|
# 起始分隔符(1字節) + 長度(2字節) + API標識符(1字節) + 數據 + 校驗和(1字節)
|
|
|
|
|
|
|
|
|
|
# 檢查幀起始符 (0x7E)
|
|
|
|
|
if not data or len(data) < 5 or data[0] != 0x7E:
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
# 獲取數據長度 (不包括校驗和)
|
|
|
|
|
# length = (data[1] << 8) + data[2]
|
|
|
|
|
length = (data[1] << 8) | data[2]
|
|
|
|
|
|
|
|
|
|
# 檢查幀完整性
|
|
|
|
|
if len(data) < length + 4: # 起始符 + 長度(2字節) + 數據 + 校驗和
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
# 提取API標識符和數據
|
|
|
|
|
frame_type = data[3]
|
|
|
|
|
# frame_data = data[4:4+length-1] # 減1是因為API標識符已經算在長度中
|
|
|
|
|
|
|
|
|
|
# 根據不同的幀類型進行處理
|
|
|
|
|
if frame_type == 0x90: # 例如,這是"接收數據包"類型
|
|
|
|
|
rf_data_start = 3 + 12
|
|
|
|
|
return data[rf_data_start:3 + length]
|
|
|
|
|
else:
|
|
|
|
|
return None
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ATCommandHandler:
|
|
|
|
|
"""AT 指令回應處理器"""
|
|
|
|
|
|
|
|
|
|
def __init__(self, serial_port: str):
|
|
|
|
|
self.serial_port = serial_port
|
|
|
|
|
self.handlers = {
|
|
|
|
|
b'DB': self._handle_rssi,
|
|
|
|
|
b'SH': self._handle_serial_high,
|
|
|
|
|
b'SL': self._handle_serial_low,
|
|
|
|
|
# 可擴展其他 AT 指令
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
def handle_response(self, response: dict):
|
|
|
|
|
"""根據 AT 指令類型分派處理"""
|
|
|
|
|
if not response or not response['is_ok']:
|
|
|
|
|
if response:
|
|
|
|
|
print(f"[{self.serial_port}] AT {response['command'].decode()} 失敗,狀態碼: {response['status']}")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
command = response['command']
|
|
|
|
|
handler = self.handlers.get(command)
|
|
|
|
|
|
|
|
|
|
if handler:
|
|
|
|
|
handler(response['data'])
|
|
|
|
|
else:
|
|
|
|
|
print(f"[{self.serial_port}] 未處理的 AT 指令: {command.decode()}")
|
|
|
|
|
|
|
|
|
|
def _handle_rssi(self, data: bytes):
|
|
|
|
|
"""處理 DB (RSSI) 回應"""
|
|
|
|
|
if not data:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
rssi_value = data[0]
|
|
|
|
|
now = time.time()
|
|
|
|
|
|
|
|
|
|
# 檢查是否最近有收到 MAVLink
|
|
|
|
|
last_mavlink_time = serial_last_mavlink_time.get(self.serial_port, 0)
|
|
|
|
|
if now - last_mavlink_time > 0.5:
|
|
|
|
|
print(f"[{self.serial_port}] 超過 0.5 秒未接收 MAVLink,RSSI = -{rssi_value} dBm 已忽略")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# 取得對應的 sysid
|
|
|
|
|
sysid = serial_to_sysid.get(self.serial_port)
|
|
|
|
|
if sysid is None:
|
|
|
|
|
print(f"[{self.serial_port}] 找不到 sysid 對應,RSSI = -{rssi_value} dBm,已忽略")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# 記錄 RSSI
|
|
|
|
|
rssi_history[sysid].append(-rssi_value)
|
|
|
|
|
time_history[sysid].append(now)
|
|
|
|
|
# print(f"[SYSID:{sysid}] RSSI = -{rssi_value} dBm")
|
|
|
|
|
|
|
|
|
|
def _handle_serial_high(self, data: bytes):
|
|
|
|
|
"""處理 SH (Serial Number High) - 範例"""
|
|
|
|
|
if len(data) >= 4:
|
|
|
|
|
serial_high = int.from_bytes(data[:4], 'big')
|
|
|
|
|
print(f"[{self.serial_port}] Serial High: 0x{serial_high:08X}")
|
|
|
|
|
|
|
|
|
|
def _handle_serial_low(self, data: bytes):
|
|
|
|
|
"""處理 SL (Serial Number Low) - 範例"""
|
|
|
|
|
if len(data) >= 4:
|
|
|
|
|
serial_low = int.from_bytes(data[:4], 'big')
|
|
|
|
|
print(f"[{self.serial_port}] Serial Low: 0x{serial_low:08X}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SerialHandler(asyncio.Protocol): # asyncio.Protocol 用於處理 Serial 收發
|
|
|
|
|
def __init__(self, udp_handler, serial_port_str):
|
|
|
|
|
self.udp_handler = udp_handler # UDP 的傳輸把手
|
|
|
|
|
self.serial_port_str = serial_port_str
|
|
|
|
|
self.at_handler = ATCommandHandler(serial_port)
|
|
|
|
|
|
|
|
|
|
self.buffer = bytearray() # 用於緩存接收到的資料
|
|
|
|
|
self.transport = None # Serial 自己的傳輸物件
|
|
|
|
|
# self.first_data = True # 標記是否為第一次收到資料
|
|
|
|
|
# self.has_processed = False # 測試模式用 處理數據旗標 # debug
|
|
|
|
|
|
|
|
|
|
def connection_made(self, transport):
|
|
|
|
|
self.transport = transport
|
|
|
|
|
if hasattr(self.udp_handler, 'set_serial_handler'):
|
|
|
|
|
self.udp_handler.set_serial_handler(self)
|
|
|
|
|
logger.info(f"Serial port {self.serial_port_str} connected.")
|
|
|
|
|
|
|
|
|
|
# Serial 收到資料的處理過程
|
|
|
|
|
def data_received(self, data):
|
|
|
|
|
# 1. 把收到的資料加入緩衝區
|
|
|
|
|
self.buffer.extend(data)
|
|
|
|
|
|
|
|
|
|
# 2. 需要完整的 header 才能解析
|
|
|
|
|
while len(self.buffer) >= 3:
|
|
|
|
|
# 3. 瞄準 XBee API Frame (0x7E 開頭的封包)
|
|
|
|
|
if self.buffer[0] != 0x7E:
|
|
|
|
|
self.buffer.pop(0) # 如果不是就丟掉
|
|
|
|
|
continue
|
|
|
|
|
|
|
|
|
|
# 4. 讀取 payload 長度
|
|
|
|
|
length = (self.buffer[1] << 8) | self.buffer[2]
|
|
|
|
|
full_length = 3 + length + 1
|
|
|
|
|
|
|
|
|
|
# 5. 等待完整封包
|
|
|
|
|
if len(self.buffer) < full_length:
|
|
|
|
|
break
|
|
|
|
|
|
|
|
|
|
# 6. 提取完整 frame 並從緩衝區移除
|
|
|
|
|
frame_payload = self.buffer[:full_length]
|
|
|
|
|
del self.buffer[:full_length]
|
|
|
|
|
|
|
|
|
|
# 7. 判斷 frame 類型
|
|
|
|
|
frame_type = frame[3]
|
|
|
|
|
|
|
|
|
|
if frame_type == 0x88:
|
|
|
|
|
# 處理 AT Command 回應
|
|
|
|
|
# response = XBeeFrameHandler.parse_at_command_response(frame)
|
|
|
|
|
# self.at_handler.handle_response(response)
|
|
|
|
|
pass # debug
|
|
|
|
|
|
|
|
|
|
elif frame_type == 0x90:
|
|
|
|
|
# Receive Packet (RX) payload 先解碼
|
|
|
|
|
processed_data = XBeeFrameHandler.decapsulate_data(bytes(frame_payload))
|
|
|
|
|
# 轉換失敗就捨棄了
|
|
|
|
|
if processed_data is None:
|
|
|
|
|
break
|
|
|
|
|
# 再透過 UDP 送出
|
|
|
|
|
self.udp_handler.transport.sendto(processed_data, (self.udp_handler.LOCAL_HOST_IP, self.udp_handler.target_port))
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
# 其他類型的 frame 未來可擴展處理 現在忽略
|
|
|
|
|
logger.warning(f"[{self.serial_port_str}] Undefined frame type: 0x{frame_type:02X}")
|
|
|
|
|
|
|
|
|
|
# # RSSI
|
|
|
|
|
# if frame[3] == 0x88 and frame[5:7] == b'DB': # frame[3] == 0x88 AT -> API 封包
|
|
|
|
|
# # frame[5:7] == b'DB' -> API 封包的DB參數
|
|
|
|
|
# status = frame[7] #
|
|
|
|
|
# if status == 0x00 and len(frame) > 8: # status == 0x00 -> 這個封包是有效封包
|
|
|
|
|
# rssi_value = frame[8]
|
|
|
|
|
# now = time.time()
|
|
|
|
|
|
|
|
|
|
# # === 優化 1:僅信任最近 0.5 秒內有接收 MAVLink 的 port
|
|
|
|
|
# last_time = serial_last_mavlink_time.get(self.serial_port, 0)
|
|
|
|
|
# if now - last_time <= 0.5:
|
|
|
|
|
# sysid = serial_to_sysid.get(self.serial_port, None)
|
|
|
|
|
# if sysid is not None:
|
|
|
|
|
# rssi_history[sysid].append(-rssi_value)
|
|
|
|
|
# time_history[sysid].append(now)
|
|
|
|
|
# # print(f"[SYSID:{sysid}] RSSI = -{rssi_value} dBm")
|
|
|
|
|
# else:
|
|
|
|
|
# print(f"[{self.serial_port}] 找不到 sysid 對應,RSSI = -{rssi_value} dBm,已忽略")
|
|
|
|
|
# else:
|
|
|
|
|
# print(f"[{self.serial_port}] 超過 0.5 秒未接收 MAVLink,RSSI = -{rssi_value} dBm 已忽略")
|
|
|
|
|
# else:
|
|
|
|
|
# print(f"[{self.serial_port}] DB 指令失敗,狀態碼: {status}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# def write_to_serial(self, data):
|
|
|
|
|
# # 在資料透過 Serial 發送之前進行處理
|
|
|
|
|
# processed_data = self.encapsulate_data(data)
|
|
|
|
|
|
|
|
|
|
# # 處理失敗就不發送
|
|
|
|
|
# if processed_data not None:
|
|
|
|
|
# self.transport.write(processed_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class UDPHandler(asyncio.DatagramProtocol): # asyncio.DatagramProtocol 用於處理 UDP 收發
|
|
|
|
|
|
|
|
|
|
LOCAL_HOST_IP = '127.0.0.1' # 只送給本地端IP
|
|
|
|
|
|
|
|
|
|
def __init__(self, target_port):
|
|
|
|
|
self.target_port = target_port # 目標 UDP 端口
|
|
|
|
|
|
|
|
|
|
self.serial_handler = None # Serial 的傳輸物件
|
|
|
|
|
self.transport = None # UDP 自己的傳輸物件
|
|
|
|
|
self.remote_addr = None # 儲存動態獲取的遠程地址 # debug
|
|
|
|
|
# self.has_processed = False # 測試模式用 處理數據旗標 # debug
|
|
|
|
|
|
|
|
|
|
def connection_made(self, transport):
|
|
|
|
|
self.transport = transport
|
|
|
|
|
print("UDP transport ready. Waiting for serial data before sending...")
|
|
|
|
|
|
|
|
|
|
def set_serial_handler(self, serial_handler):
|
|
|
|
|
self.serial_handler = serial_handler
|
|
|
|
|
|
|
|
|
|
# UDP 收到資料的處理過程
|
|
|
|
|
def datagram_received(self, data, addr):
|
|
|
|
|
# 儲存對方的地址(這樣就能向同一個來源回傳數據)
|
|
|
|
|
# self.remote_addr = addr
|
|
|
|
|
# print(f"Received UDP data from {addr}, setting as remote address")
|
|
|
|
|
|
|
|
|
|
processed_data = XBeeFrameHandler.encapsulate_data(data)
|
|
|
|
|
|
|
|
|
|
if self.serial_handler:
|
|
|
|
|
self.serial_handler.transport.write(processed_data)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# def send_udp(self, data):
|
|
|
|
|
# # 藉由 UDP 發送資料出去
|
|
|
|
|
|
|
|
|
|
# # 在透過 UDP 發送數據之前進行解封裝
|
|
|
|
|
# decoded_data = self.decapsulate_data(data)
|
|
|
|
|
# if decoded_data is None:
|
|
|
|
|
# return
|
|
|
|
|
|
|
|
|
|
# if self.transport:
|
|
|
|
|
# self.transport.sendto(decoded_data, (self.LOCAL_HOST_IP, self.target_port))
|
|
|
|
|
|
|
|
|
|
#==================================================================
|
|
|
|
|
|
|
|
|
|
class SerialReceiverType(Enum):
|
|
|
|
|
"""連接類型"""
|
|
|
|
|
TELEMETRY = auto()
|
|
|
|
|
XBEEAPI = auto()
|
|
|
|
|
OTHER = auto()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class serial_manager:
|
|
|
|
|
|
|
|
|
|
class serial_object:
|
|
|
|
|
def __init__(self, serial_port, baudrate, target_port, receiver_type: SerialReceiverType):
|
|
|
|
|
self.serial_port = serial_port # /dev/ttyUSB or COM3 ...etc
|
|
|
|
|
self.baudrate = baudrate
|
|
|
|
|
self.receiver_type = receiver_type
|
|
|
|
|
self.target_port = target_port # 指向的 UPD 端口
|
|
|
|
|
|
|
|
|
|
self.transport = None
|
|
|
|
|
self.protocol = None
|
|
|
|
|
self.udp_handler = None
|
|
|
|
|
self.serial_handler = None
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
self.thread = None
|
|
|
|
|
self.loop = None
|
|
|
|
|
self.running = False
|
|
|
|
|
self.serial_count = 0
|
|
|
|
|
self.serial_objects = {} # serial id num : serial object
|
|
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
|
self.loop = None
|
|
|
|
|
self.thread = None
|
|
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
|
|
|
|
|
|
if self.running:
|
|
|
|
|
logger.warning("serial_manager already running")
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
self.running = True
|
|
|
|
|
|
|
|
|
|
# 啟動獨立線程 命名為 SerialManager
|
|
|
|
|
self.thread = threading.Thread(
|
|
|
|
|
target=self._run_event_loop,
|
|
|
|
|
name="SerialManager"
|
|
|
|
|
)
|
|
|
|
|
self.thread.daemon = False # 不設為 daemon,確保正確關閉
|
|
|
|
|
self.thread.start()
|
|
|
|
|
|
|
|
|
|
# 等待 _run_event_loop 建立事件循環的物件 self.loop
|
|
|
|
|
start_timeout = 2.0
|
|
|
|
|
start_time = time.time()
|
|
|
|
|
while not self.loop and time.time() - start_time < start_timeout:
|
|
|
|
|
time.sleep(0.1)
|
|
|
|
|
|
|
|
|
|
# 檢查另一個執行緒有沒有成功建立事件循環物件 self.loop
|
|
|
|
|
if self.loop:
|
|
|
|
|
logger.info("serial_manager thread started <-")
|
|
|
|
|
return True
|
|
|
|
|
else:
|
|
|
|
|
logger.error("serial_manager failed to start")
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
def shutdown(self):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def _run_event_loop(self):
|
|
|
|
|
"""在獨立線程中運行 asyncio 事件循環"""
|
|
|
|
|
self.loop = asyncio.new_event_loop()
|
|
|
|
|
asyncio.set_event_loop(self.loop)
|
|
|
|
|
|
|
|
|
|
# # 為每個 serial_object 建立連接
|
|
|
|
|
# for serial_obj in self.serial_objects:
|
|
|
|
|
# coro = serial_asyncio.create_serial_connection(
|
|
|
|
|
# self.loop,
|
|
|
|
|
# lambda: SerialProtocol(serial_obj.receiver_type),
|
|
|
|
|
# serial_obj.serial_port,
|
|
|
|
|
# baudrate=serial_obj.baudrate
|
|
|
|
|
# )
|
|
|
|
|
# transport, protocol = self.loop.run_until_complete(coro)
|
|
|
|
|
# serial_obj.transport = transport
|
|
|
|
|
# serial_obj.protocol = protocol
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
self.loop.run_forever()
|
|
|
|
|
finally:
|
|
|
|
|
self.loop.close()
|
|
|
|
|
|
|
|
|
|
def create_serial_link(self, serial_port, baudrate, target_port, receiver_type: SerialReceiverType):
|
|
|
|
|
|
|
|
|
|
if self.loop is None:
|
|
|
|
|
logger.error("Event loop not running, cannot create serial link")
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
# 檢查 serial port 有效
|
|
|
|
|
self.check_serial_port(serial_port)
|
|
|
|
|
|
|
|
|
|
serial_obj = self.serial_object(serial_port, baudrate, target_port, receiver_type)
|
|
|
|
|
|
|
|
|
|
# 建立 UDP 處理器 並指定目標端口位置
|
|
|
|
|
serial_obj.udp_handler = UDPHandler(target_port)
|
|
|
|
|
# 建立 UDP 傳輸,不指定接收端口(自己),讓系統自動分配
|
|
|
|
|
try:
|
|
|
|
|
serial_obj.transport, serial_obj.protocol = await self.loop.create_datagram_endpoint(
|
|
|
|
|
lambda: serial_obj.udp_handler,
|
|
|
|
|
local_addr=('0.0.0.0', 0) # 使用端口 0 讓系統自動分配可用端口
|
|
|
|
|
)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Cannot Create UDP Endpoint: {str(e)}")
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
# 建立 Serial 傳輸,將 UDP 處理器傳給它
|
|
|
|
|
try:
|
|
|
|
|
serial_obj.serial_handler = SerialHandler(serial_obj.udp_handler)
|
|
|
|
|
|
|
|
|
|
_, serial_transport = await serial_asyncio.create_serial_connection(
|
|
|
|
|
self.loop, lambda: serial_obj.serial_handler, serial_port, baudrate=baudrate
|
|
|
|
|
)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Cannot Create Serial Connection: {str(e)}")
|
|
|
|
|
serial_obj.transport.close()
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
# self.serial_objects.append(serial_obj)
|
|
|
|
|
self.serial_objects[serial_count+1] = serial_obj
|
|
|
|
|
serial_count += 1
|
|
|
|
|
|
|
|
|
|
async def _async_create_serial_link(self, serial_port, baudrate, target_port):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def remove_serial_link(serial_id):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
async def _async_remove_serial_link(self, serial_id):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def check_serial_port(serial_port):
|
|
|
|
|
"""檢查串口是否存在與可用"""
|
|
|
|
|
# 檢查設備是否存在
|
|
|
|
|
if not os.path.exists(serial_port):
|
|
|
|
|
logger.error(f"Serial Device {serial_port} Not Found")
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
# 檢查是否有權限訪問設備
|
|
|
|
|
try:
|
|
|
|
|
os.access(serial_port, os.R_OK | os.W_OK)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Cannot Access Serial Device {serial_port}: {str(e)}")
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
# 檢查是否被占用
|
|
|
|
|
try:
|
|
|
|
|
# 嘗試打開串口
|
|
|
|
|
ser = serial.Serial(serial_port, SERIAL_BAUDRATE)
|
|
|
|
|
ser.close() # 打開成功後立即關閉
|
|
|
|
|
return True
|
|
|
|
|
except serial.SerialException as e:
|
|
|
|
|
logger.error(f"Serial Device {serial_port} is Occupied or Inaccessible: {str(e)}")
|
|
|
|
|
return False
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"Unknown Error: {str(e)}")
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __main__ == '__main__':
|
|
|
|
|
sm = serial_manager()
|
|
|
|
|
sm.start()
|
|
|
|
|
|
|
|
|
|
SERIAL_PORT = '/dev/ttyUSB0' # 手動指定
|
|
|
|
|
SERIAL_BAUDRATE = 115200
|
|
|
|
|
UDP_REMOTE_IP = '127.0.0.1'
|
|
|
|
|
UDP_REMOTE_PORT = 14560
|
|
|
|
|
sm.create_serial_link(SERIAL_PORT, SERIAL_BAUDRATE, UDP_REMOTE_IP, SerialReceiverType.XBEEAPI)
|