python-can-hirain-test 0.1.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
demo/Test_Canfd.py ADDED
@@ -0,0 +1,112 @@
1
+ import can
2
+ import time
3
+ from can_hirain.config import TestBaseVCI_ChlMode, TestBaseVCI_SAMPLEPOINT # 导入枚举值
4
+ from can_hirain.hrcan import get_hr_hw_devices_info # 导入获取全部设备信息函数
5
+
6
+
7
+ def multi_channel():
8
+
9
+ # 初始化通道0(发送端)和通道1(接收端)
10
+ with can.Bus(interface="hrcan", channel=0, # 激活通道0为CANFD通道
11
+ configs = [{
12
+ 'resistance': 1, # 启用终端电阻
13
+ 'mode' : TestBaseVCI_ChlMode.CANFD, # 设置CANFD通道
14
+ 'listenFlag' : 0, # 0-正常状态,1-通道为仅监听状态,不给ACK响应
15
+ 'abrBaudrate': 500_000,
16
+ 'dbrBaudrate': 2000_000,
17
+ 'abrSamplePoint': TestBaseVCI_SAMPLEPOINT.SAMPLE_80,
18
+ 'dbrSamplePoint': TestBaseVCI_SAMPLEPOINT.SAMPLE_80,}]) as bus_tx, \
19
+ can.Bus(interface="hrcan", channel=1, # 激活通道1为CANFD通道
20
+ configs=[{
21
+ 'resistance': 1, # 启用终端电阻
22
+ 'mode': TestBaseVCI_ChlMode.CANFD, # 设置CANFD通道
23
+ 'listenFlag' : 0, # 0-正常状态,1-通道为仅监听状态,不给ACK响应
24
+ 'abrBaudrate': 500_000,
25
+ 'dbrBaudrate': 2000_000,
26
+ 'abrSamplePoint': TestBaseVCI_SAMPLEPOINT.SAMPLE_80,
27
+ 'dbrSamplePoint': TestBaseVCI_SAMPLEPOINT.SAMPLE_80}]) as bus_rx:
28
+ # with can.Bus(
29
+ # interface="hrcan",
30
+ # channel=0,
31
+ # rate_baudrate=500,
32
+ # data_baudrate=2000,
33
+ # enable_120hm=True,
34
+ # is_fd=True,
35
+ # is_include_tx=True,
36
+ # hwserial=b"HRA000020000" # 使用设备SN作为硬件序列号
37
+ # ) as bus_tx,\
38
+ # can.Bus(
39
+ # interface="hrcan",
40
+ # channel=1,
41
+ # rate_baudrate=500,
42
+ # data_baudrate=2000,
43
+ # enable_120hm=True,
44
+ # is_fd=True,
45
+ # is_include_tx=True,
46
+ # hwserial=b"HRA000020000" # 使用设备SN作为硬件序列号
47
+ # ) as bus_rx:
48
+
49
+ # # 配置接收端监听器(打印+日志)
50
+ # listeners_rx = [can.Printer(), can.Logger("rx_channel.asc")]
51
+ # notifier_rx = can.Notifier(bus_rx, listeners_rx)
52
+ # time.sleep(0.1) # 确保监听器就绪
53
+ #
54
+ # # 配置发送端监听器(打印+日志)
55
+ # listeners_tx = [can.Printer(), can.Logger("tx_channel.asc")]
56
+ # notifier_tx = can.Notifier(bus_tx, listeners_tx)
57
+ # time.sleep(0.1) # 确保监听器就绪
58
+
59
+ # 配置总计监听器(日志)(不配置Printer以降低IO资源占用)
60
+ listeners = [can.Printer(), can.Logger("rxtx_channel_test.blf")]
61
+ notifier = can.Notifier([bus_tx,bus_rx], listeners)
62
+ time.sleep(0.1) # 确保监听器就绪
63
+
64
+ # 发送测试消息(从通道0发送到通道1)
65
+ test_msg_1 = can.Message(
66
+ arbitration_id=0x123,
67
+ data=[0xAA, 0xBB, 0xCC],
68
+ dlc=3,
69
+ is_extended_id=True,
70
+ is_fd=True,
71
+ bitrate_switch=True
72
+ )
73
+
74
+ # 发送测试消息(从通道0发送到通道1)
75
+ test_msg_2 = can.Message(
76
+ arbitration_id=0x1FFFFFFE,
77
+ data=[i for i in range(8)],
78
+ dlc=8,
79
+ is_extended_id=True,
80
+ is_fd=True,
81
+ bitrate_switch=True
82
+ )
83
+ print(f"发送消息: ID=0x{test_msg_1.arbitration_id:X}, Data={','.join(str(_) for _ in test_msg_1.data)}")
84
+ bus_tx.send(test_msg_1)
85
+ print(f"发送消息: ID=0x{test_msg_2.arbitration_id:X}, Data={','.join(str(_) for _ in test_msg_2.data)}")
86
+ bus_tx.send(test_msg_2)
87
+
88
+ time.sleep(2)
89
+ # canfd_id = [0x85, 0xE0, 0x100, 0x103, 0x122, 0x123, 0x124, 0x125, 0x162, 0x165, 0x1A5, 0x218, 0x220, 0x228, 0x256, 0x26D, 0x288, 0x292, 0x2A1, 0x2A5, 0x2A7, 0x2AC, 0x2E0, 0x2F7, 0x360, 0x380, 0x3D0, 0x3F1, 0x405, 0x744, 0x783, 0x795, 0x7C6, 0x7DF, 0x7E2, 0x1F0, 0x1F1, 0x1F2, 0x283, 0x284]
90
+ canfd_id = [0x7E2, 0x1F0, 0x1F1, 0x1F2, 0x283, 0x284]
91
+ # test_msg_2.arbitration_id = 0x1FFFEEEE
92
+ num = 0
93
+ test_msg_2.is_extended_id = False
94
+ for i in canfd_id:
95
+ test_msg_2.arbitration_id = i
96
+ bus_tx.send_periodic(test_msg_2, 0.01)
97
+ print(f"发送周期消息: ID=0x{test_msg_2.arbitration_id:X}")
98
+ num += 1
99
+ print(f"共有{num}条周期报文")
100
+
101
+ print("等待100s...")
102
+ time.sleep(100)
103
+
104
+ bus_tx.stop_all_periodic_tasks()
105
+ time.sleep(10)
106
+ notifier.stop()
107
+
108
+ if __name__ == "__main__":
109
+ devices = get_hr_hw_devices_info()
110
+ print(devices)
111
+ time.sleep(2)
112
+ multi_channel()
demo/Test_can.py ADDED
@@ -0,0 +1,81 @@
1
+ import can
2
+ import time
3
+ from can_hirain.config import *
4
+
5
+ def multi_channel():
6
+
7
+ # 初始化通道0(发送端)和通道1(接收端)
8
+ with can.Bus(interface="hrcan", channel=0, # 激活通道0为CANFD通道
9
+ configs = [{
10
+ 'resistance': 1, # 启用终端电阻
11
+ 'mode' : TestBaseVCI_ChlMode.CAN, # 设置CAN通道
12
+ 'listenFlag' : 0, # 0-正常状态,1-通道为仅监听状态,不给ACK响应
13
+ 'baudrate': 500_000,
14
+ 'samplePoint': TestBaseVCI_SAMPLEPOINT.SAMPLE_75}]) as bus_tx, \
15
+ can.Bus(interface="hrcan", channel=1, # 激活通道1为CANFD通道
16
+ configs=[{
17
+ 'resistance': 1, # 启用终端电阻
18
+ 'mode': TestBaseVCI_ChlMode.CAN, # 设置CAN通道
19
+ 'listenFlag' : 0, # 0-正常状态,1-通道为仅监听状态,不给ACK响应
20
+ 'baudrate': 500_000,
21
+ 'samplePoint': TestBaseVCI_SAMPLEPOINT.SAMPLE_75}]) as bus_rx:
22
+
23
+ time.sleep(0.1) # 确保监听器就绪
24
+
25
+ # 发送测试消息(从通道0发送到通道1)
26
+ test_msg_1 = can.Message(
27
+ arbitration_id=0x123,
28
+ dlc=3,
29
+ data=[0xAA, 0xBB, 0xCC],
30
+ is_extended_id=False,
31
+ is_fd=False,
32
+ bitrate_switch=True
33
+ )
34
+
35
+ # 发送测试消息(从通道0发送到通道1)
36
+ test_msg_2 = can.Message(
37
+ arbitration_id=0x1FFFFFFE,
38
+ data=[i for i in range(8)],
39
+ dlc=8,
40
+ is_extended_id=True,
41
+ is_fd=False,
42
+ bitrate_switch=True
43
+ )
44
+
45
+ print(f"发送消息: ID=0x{test_msg_1.arbitration_id:X}, Data={','.join(str(_) for _ in test_msg_1.data)}")
46
+ bus_tx.send(test_msg_1)
47
+ print(f"发送消息: ID=0x{test_msg_2.arbitration_id:X}, Data={','.join(str(_) for _ in test_msg_2.data)}")
48
+ bus_tx.send(test_msg_2)
49
+ # time.sleep(2)
50
+
51
+ # ==== 接收阶段1:接收单次发送的消息 ====
52
+ received_count = 0
53
+ start_time = time.time()
54
+ while time.time() - start_time < 2 and received_count <=2 :
55
+ msg = bus_rx.recv(timeout=0.5) # 设置接收超时
56
+ if msg is not None:
57
+ received_count += 1
58
+ print(f"接收到消息[{received_count}]: ID=0x{msg.arbitration_id:X}, DLC={msg.dlc}, Data={','.join(str(_) for _ in msg.data)}")
59
+
60
+
61
+ # ==== 周期消息处理 ====
62
+ bus_tx.send_periodic(test_msg_2, 0.1)
63
+ print(f"开始周期发送: ID=0x{test_msg_2.arbitration_id:X} 周期: 100ms")
64
+
65
+ # ==== 接收阶段2:接收周期消息 ====
66
+ period_start = time.time()
67
+ period_msgs = []
68
+ while time.time() - period_start < 5:
69
+ msg = bus_rx.recv(timeout=0.1)
70
+ if msg is not None:
71
+ period_msgs.append(msg)
72
+ # 实时打印接收到的消息摘要
73
+ print(f"[周期消息] ID=0x{msg.arbitration_id:X} chl={msg.channel} Dir={msg.is_rx} DLC={msg.dlc} TimeStamp={msg.timestamp}")
74
+
75
+
76
+ # 停止周期发送并关闭
77
+ bus_rx.stop_all_periodic_tasks()
78
+ print(f"已停止周期发送, 共接收到 {len(period_msgs)} 条周期消息")
79
+
80
+ if __name__ == "__main__":
81
+ multi_channel()
demo/Test_diag.py ADDED
@@ -0,0 +1,87 @@
1
+ import can
2
+ import time
3
+ from can_hirain.config import TestBaseVCI_ChlMode, TestBaseVCI_SAMPLEPOINT, N_Parameter # 导入枚举值
4
+ from can_hirain.hrcan import get_hr_hw_devices_info # 导入获取全部设备信息函数
5
+
6
+
7
+ def multi_channel():
8
+
9
+ # 初始化通道0(发送端)和通道1(接收端)
10
+ # with can.Bus(interface="hrcan", channel=0, # 激活通道0为CANFD通道
11
+ # configs = [{
12
+ # 'resistance': 1, # 启用终端电阻
13
+ # 'mode' : TestBaseVCI_ChlMode.CANFD, # 设置CANFD通道
14
+ # 'listenFlag' : 0, # 0-正常状态,1-通道为仅监听状态,不给ACK响应
15
+ # 'abrBaudrate': 500_000,
16
+ # 'dbrBaudrate': 2000_000,
17
+ # 'abrSamplePoint': TestBaseVCI_SAMPLEPOINT.SAMPLE_80,
18
+ # 'dbrSamplePoint': TestBaseVCI_SAMPLEPOINT.SAMPLE_80,}]) as bus_tx, \
19
+ # can.Bus(interface="hrcan", channel=4, # 激活通道1为CANFD通道
20
+ # configs=[{
21
+ # 'resistance': 1, # 启用终端电阻
22
+ # 'mode': TestBaseVCI_ChlMode.CANFD, # 设置CANFD通道
23
+ # 'listenFlag' : 0, # 0-正常状态,1-通道为仅监听状态,不给ACK响应
24
+ # 'abrBaudrate': 500_000,
25
+ # 'dbrBaudrate': 2000_000,
26
+ # 'abrSamplePoint': TestBaseVCI_SAMPLEPOINT.SAMPLE_80,
27
+ # 'dbrSamplePoint': TestBaseVCI_SAMPLEPOINT.SAMPLE_80}]) as bus_rx:
28
+ with can.Bus(
29
+ interface="hrcan",
30
+ channel=0,
31
+ rate_baudrate=500,
32
+ data_baudrate=2000,
33
+ enable_120hm=True,
34
+ is_fd=True,
35
+ is_include_tx=True,
36
+ hwserial=b"HR3210700241", # 使用设备SN作为硬件序列号
37
+ # filters=[{"min":0x600, "max": 0x7FF, "extend": False}] # 仅保留诊断报文
38
+ ) as bus_tx,\
39
+ can.Bus(
40
+ interface="hrcan",
41
+ channel=1,
42
+ rate_baudrate=500,
43
+ data_baudrate=2000,
44
+ enable_120hm=True,
45
+ is_fd=True,
46
+ is_include_tx=True,
47
+ hwserial=b"HR3210700241" # 使用设备SN作为硬件序列号
48
+ ) as bus_rx:
49
+
50
+ # 配置接收端监听器(打印+日志)
51
+ # listeners_rx = [can.Printer(), can.Logger("rx_channel.asc")]
52
+ # notifier_rx = can.Notifier(bus_rx, listeners_rx)
53
+ # time.sleep(0.1) # 确保监听器就绪
54
+
55
+ # 配置发送端监听器(打印+日志)
56
+ listeners_tx = [can.Printer(), can.Logger("tx_channel.asc")]
57
+ notifier_tx = can.Notifier(bus_tx, listeners_tx)
58
+ time.sleep(0.1) # 确保监听器就绪
59
+
60
+ n_rst = bus_tx.can_set_diagnostic_config(ReqId=0x7B6, FuncReqID=0x7DF, RespId=0x7BE, DataPadding = 0xCC)
61
+ if n_rst == 0:
62
+ print(f"诊断配置下发成功")
63
+ else:
64
+ print(f"诊断配置下发失败,错误码{n_rst}")
65
+
66
+ # 配置启动填充位
67
+ n_rst = bus_tx.can_set_param_15765_2(True, Parameter=N_Parameter.N_Opt)
68
+ if n_rst == 0:
69
+ print(f"诊断填充位配置下发成功")
70
+ else:
71
+ print(f"诊断填充位配置下发失败,错误码{n_rst}")
72
+
73
+ n_rst, resp = bus_tx.can_forward_15765_2(ReqId=0x7B6, DataLen=3, data=(0x22, 0xF1, 0x92), WaitResp = True)
74
+ if n_rst == 0:
75
+ print(f"诊断成功, 响应数据{resp}")
76
+ else:
77
+ print(f"诊断失败,错误码{n_rst}")
78
+
79
+ time.sleep(10)
80
+
81
+
82
+ import threading
83
+ if __name__ == "__main__":
84
+ devices = get_hr_hw_devices_info()
85
+ print(devices)
86
+ time.sleep(2)
87
+ multi_channel()
demo/Test_env.py ADDED
@@ -0,0 +1,2 @@
1
+ import importlib.metadata as m;
2
+ print([e for e in m.entry_points(group='can.interface') if e.name=='hrcan'])
@@ -0,0 +1,83 @@
1
+ Metadata-Version: 2.4
2
+ Name: python-can-hirain-test
3
+ Version: 0.1.1
4
+ Summary: python-can interface plugin for Hirain
5
+ Author-email: Jingwei Hirain <yalong.ding@hirain.com>
6
+ License: MIT
7
+ Keywords: python-can,CAN,Hirain
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Operating System :: OS Independent
10
+ Requires-Python: >=3.7
11
+ Description-Content-Type: text/markdown
12
+ License-File: LICENSE
13
+ Requires-Dist: python-can>=4.0.0
14
+ Dynamic: license-file
15
+
16
+ # python-can-hirain
17
+
18
+ Python CAN 接口适配器,用于通过 **python-can** 库操作 **HIRAIN TestBaseVCI** 硬件设备。
19
+
20
+ ## 安装
21
+
22
+ 使用 pip 安装:
23
+
24
+ ```bash
25
+ pip install python-can-hirain
26
+ ```
27
+
28
+ 安装后,`hrcan` 接口会自动注册到 python-can 中,无需手动复制文件或修改 `__init__.py`。
29
+
30
+ ## 快速开始
31
+
32
+ ### CAN 通信示例
33
+
34
+ ```python
35
+ import can
36
+ import time
37
+
38
+ # 初始化 CAN 通道
39
+ with can.Bus(interface="hrcan", channel=0, # 激活通道0为CANFD通道
40
+ configs = [{
41
+ 'resistance': 1, # 启用终端电阻
42
+ 'mode' : TestBaseVCI_ChlMode.CAN, # 设置CAN通道
43
+ 'listenFlag' : 0, # 0-正常状态,1-通道为仅监听状态,不给ACK响应
44
+ 'baudrate': 500_000,
45
+ 'samplePoint': TestBaseVCI_SAMPLEPOINT.SAMPLE_75}]) as bus:
46
+ pass
47
+ # 初始化 CANFD 通道
48
+ with can.Bus(interface="hrcan", channel=0, # 激活通道0为CANFD通道
49
+ configs = [{
50
+ 'resistance': 1, # 启用终端电阻
51
+ 'mode' : TestBaseVCI_ChlMode.CANFD, # 设置CANFD通道
52
+ 'listenFlag' : 0, # 0-正常状态,1-通道为仅监听状态,不给ACK响应
53
+ 'abrBaudrate': 500_000,
54
+ 'dbrBaudrate': 2000_000,
55
+ 'abrSamplePoint': TestBaseVCI_SAMPLEPOINT.SAMPLE_80,
56
+ 'dbrSamplePoint': TestBaseVCI_SAMPLEPOINT.SAMPLE_80,}]) as bus:
57
+ pass
58
+ ```
59
+
60
+
61
+ ## 环境要求
62
+
63
+ - **操作系统**: Windows 10/11, Ubuntu 20/22
64
+ - **硬件驱动**: TestBaseVCI 驱动程序 2.4.1+
65
+ - **Python**: 64 位 Python 3.7+
66
+ - **依赖库**: python-can
67
+
68
+ ## 更多示例
69
+
70
+ 查看 `demo/` 目录获取完整示例:
71
+ - `Test_can.py` - 经典 CAN 操作示例
72
+ - `Test_Canfd.py` - CAN FD 操作示例
73
+ - `Test_diag.py` - 诊断功能示例
74
+
75
+ ## 注意事项
76
+
77
+ - 确保 TestBaseVCI 驱动程序已正确安装
78
+ - Linux 系统上使用 sudo 时,可能需要添加 `-E` 参数保留环境变量
79
+ - 使用前请确认设备能被系统识别
80
+
81
+ ## 支持
82
+
83
+ 有关详细配置参数和高级用法,请参考 python-can 官方文档和 HIRAIN 二次开发SDK手册。
@@ -0,0 +1,14 @@
1
+ can_hirain/BasicStructure.py,sha256=mxKqAv7OOPC4faRSgUaNFS7_mxdNuF6nobS7LYNE4Lo,59440
2
+ can_hirain/TestBaseVCI.py,sha256=l81JPbnlHmgBl1gZVKMTQhVDEAfl9h0mOH3LglMKNBo,35484
3
+ can_hirain/__init__.py,sha256=jgtBbPhWvHOT7cuiNaKeuGLGU2_5Sb_BUCqCwSysqFs,53
4
+ can_hirain/hrcan.py,sha256=cBRlkzFSzLrfjdjylf6qwQS9kZYhi2hjaLu60yJcUus,28243
5
+ demo/Test_Canfd.py,sha256=RS2yC9Q_hKQh8Xp4_8_qiRfu1doQrkRAG5hXouenH80,4884
6
+ demo/Test_can.py,sha256=XMnJ4kPwxoauELB1jObQbbkTVHJZC4CtxkwdUlB3wfA,3445
7
+ demo/Test_diag.py,sha256=SqI0vYlrg7Fho6HEhB1twFSeAdb615WGZDzSSYFjWy8,3751
8
+ demo/Test_env.py,sha256=0zn0fDvvIQvs4hi7lKIFklgsFoAU26pE31RLphzb8xM,110
9
+ python_can_hirain_test-0.1.1.dist-info/licenses/LICENSE,sha256=5fhq4o78k6EnY_DJPRU-Yz_4J_2cSDF4Bp_RXcnxE3Y,1299
10
+ python_can_hirain_test-0.1.1.dist-info/METADATA,sha256=4_WT9h8huwgyB3PJNRGBSJv99PS7Pju9S_ORTMVk_7g,2578
11
+ python_can_hirain_test-0.1.1.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
12
+ python_can_hirain_test-0.1.1.dist-info/entry_points.txt,sha256=n23mUs8tYXL-P6523j0rPoU1Ja3ew6RycevbBY4IqbU,50
13
+ python_can_hirain_test-0.1.1.dist-info/top_level.txt,sha256=dRUhv5FHQAfYpdFLEvu9YG5cxGqM-DnoTzgssJqRVHs,16
14
+ python_can_hirain_test-0.1.1.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (82.0.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,2 @@
1
+ [can.interface]
2
+ hrcan = can_hirain.hrcan:HRCANBus
@@ -0,0 +1,26 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 JINGWEI HIRAIN
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ Third-party libraries imported in the code are subject to the proprietary rights
16
+ notices of their respective authors. Users must comply with the copyright
17
+ requirements specified by the authors of such third-party libraries.
18
+
19
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
+ SOFTWARE.
26
+
@@ -0,0 +1,2 @@
1
+ can_hirain
2
+ demo