| 12345678910111213141516171819202122232425262728293031323334353637 |
- # rblidar.py
- import ctypes
- import logging
- # 加载C库
- rblidar_lib = ctypes.CDLL('./rb_lidar.so') # 确保路径正确
- # 定义回调函数类型
- CALLBACK_TYPE = ctypes.CFUNCTYPE(None, ctypes.c_char_p, ctypes.c_int)
- # 回调函数
- def my_callback(data, length):
- # 将接收到的字节数据处理(例如打印、存储等)
- logging.info("Received length: %d", length)
- class RBLidar:
- def __init__(self, ip: str, port: int):
- # 初始化日志记录
- logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s: %(message)s')
- # 创建回调函数
- self.callback = CALLBACK_TYPE(my_callback)
- # 创建C库中的RBLidar实例
- self.lidar = rblidar_lib.rblidar_create(ip.encode('utf-8'), port, self.callback)
- def __del__(self):
- # 清理资源
- rblidar_lib.rblidar_destroy(self.lidar)
- # 示例使用
- if __name__ == "__main__":
- lidar = RBLidar("192.168.8.1", 2368) # 替换为实际的IP和端口
- try:
- while True:
- pass # 持续运行以接收数据
- except KeyboardInterrupt:
- logging.info("Stopping...")
|