### Start and Stop BLF Logging Source: https://context7.com/sy950915/tsmasterapi/llms.txt Use `tsapp_start_logging` to begin recording bus messages to a BLF file and `tsapp_stop_logging` to end the recording. Ensure the file path is absolute. ```python from TSMasterAPI import * # Specify BLF file path (requires absolute path) blf_path = "E:\\logs\\can_log.blf".encode("utf8") # Start logging result = tsapp_start_logging(blf_path) if result == 0: print("Start logging messages") else: print(f"Logging failed to start, error code: {result}") # ... run for a period to collect messages ... # Stop logging tsapp_stop_logging() print("Logging stopped") ``` -------------------------------- ### Initialize and Finalize TSMaster Library Source: https://context7.com/sy950915/tsmasterapi/llms.txt Required setup to initialize the library before any API calls and to release resources upon program termination. ```python from TSMasterAPI import * # 初始化 TSMaster 库 initialize_lib_tsmaster("TSMaster".encode("utf8")) # ... 执行其他操作 ... # 释放库资源 finalize_lib_tsmaster() ``` -------------------------------- ### Start Remaining Bus Simulation (RBS) Source: https://context7.com/sy950915/tsmasterapi/llms.txt Initiate the Remaining Bus Simulation (RBS) feature using `tscom_can_rbs_start`. RBS simulates ECU nodes by automatically sending messages and signals defined in a DBC file. Ensure a DBC file with network definitions is loaded before starting RBS. ```python from TSMasterAPI import * from ctypes import c_int32 # First load DBC file db_handle = c_int32(0) tsdb_load_can_db(b"C:/path/to/network.dbc", b"0,1", db_handle) # Connect to hardware tsapp_connect() tsfifo_enable_receive_fifo() # Start RBS function result = tscom_can_rbs_start() if result == 0: print("RBS function started successfully") else: print(f"RBS failed to start, error code: {result}") ``` -------------------------------- ### GET /api/fifo_receive Source: https://context7.com/sy950915/tsmasterapi/llms.txt Retrieves received CAN and CANFD messages from the internal FIFO buffer. ```APIDOC ## GET /api/fifo_receive ### Description Reads messages from the FIFO buffer. Requires enabling the FIFO receiver first. ### Method GET ### Parameters - **buffer** (Array) - Buffer to store received messages. - **count** (int) - Number of messages to read. - **channel** (int) - Channel index. - **read_type** (enum) - Filter type (TX, RX, or both). ``` -------------------------------- ### Send UDS Diagnostic Request and Get Response Source: https://context7.com/sy950915/tsmasterapi/llms.txt Use `tstp_can_request_and_get_response` to send a UDS request and wait for a response. Provide the diagnostic module handle, request data, request length, and buffers for the response data and its size. This function blocks until a response is received or a timeout occurs. ```python from TSMasterAPI import * from ctypes import c_uint8, c_int # Prepare request data (Service 22 Read VIN) request_data = (c_uint8 * 100)() request_data[0] = 0x22 # Read Data Service ID request_data[1] = 0xF1 # DID High byte request_data[2] = 0x90 # DID Low byte (F190 = VIN) # Prepare response buffer response_data = (c_uint8 * 1000)() response_size = c_int(1000) # Send request and get response result = tstp_can_request_and_get_response( uds_handle, # Diagnostic module handle request_data, # Request data 3, # Request data length response_data, # Response buffer response_size # Response data size ) if result == 0: print(f"Received response, length: {response_size.value} bytes") print("Response data: ", end="") for i in range(response_size.value): print(f"{response_data[i]:02X} ", end="") print() else: print(f"Diagnostic request failed, error code: {result}") ``` -------------------------------- ### Create and Write to BLF File Source: https://context7.com/sy950915/tsmasterapi/llms.txt Use `tslog_blf_write_start` to create a new BLF file, `tslog_blf_write_can` or `tslog_blf_write_canfd` to write messages, and `tslog_blf_write_end` to finalize and close the file. Ensure the output path is encoded as UTF-8. ```python from TSMasterAPI import * from ctypes import c_int32 # Create BLF file write_handle = c_int32(0) output_path = "E:\\logs\\output.blf".encode("utf8") result = tslog_blf_write_start(output_path, write_handle) if result == 0: print("BLF file created successfully") # Write CAN message can_msg = TLIBCAN() can_msg.FIdxChn = 0 can_msg.FIdentifier = 0x100 can_msg.FDLC = 8 can_msg.FTimeUs = 1000000 # 1 second timestamp for i in range(8): can_msg.FData[i] = i tslog_blf_write_can(write_handle, can_msg) # Write more messages... # Finish writing tslog_blf_write_end(write_handle) print("BLF file writing completed") ``` -------------------------------- ### 获取 API 错误描述 Source: https://context7.com/sy950915/tsmasterapi/llms.txt 将 API 返回的错误码转换为可读的文本描述,用于调试。 ```python from TSMasterAPI import * from ctypes import c_char_p # 执行某个操作 result = tsapp_connect() if result != 0: # 获取错误描述 error_desc = c_char_p() tsapp_get_error_description(result, error_desc) print(f"错误码: {result}") print(f"错误描述: {error_desc.value.decode('utf8') if error_desc.value else '未知错误'}") ``` -------------------------------- ### Map Hardware Channels Source: https://context7.com/sy950915/tsmasterapi/llms.txt Establishes the mapping between physical hardware channels and software logical channels. ```python from TSMasterAPI import * AppName = "TSMaster".encode("utf8") # 映射 CAN 通道 1 到 TC1016 硬件的第一个通道 result = tsapp_set_mapping_verbose( AppName, # 应用程序名称 TLIBApplicationChannelType.APP_CAN, # 通道类型:CAN CHANNEL_INDEX.CHN1, # 软件通道索引:通道1 "TC1016".encode("UTF8"), # 硬件设备名称 TLIBBusToolDeviceType.TS_USB_DEVICE, # 设备类型:USB设备 TLIB_TS_Device_Sub_Type.TC1016, # 设备子类型:TC1016 0, # 硬件索引 CHANNEL_INDEX.CHN1, # 硬件通道索引 True # 启用映射 ) if result == 0: print("通道映射成功") else: print("通道映射失败") ``` -------------------------------- ### Hardware Connection and Disconnection Source: https://context7.com/sy950915/tsmasterapi/llms.txt Establishes and terminates the connection with the hardware device. ```APIDOC ## Hardware Connection and Disconnection ### Description Establishes and terminates the connection with the hardware device. Connection must be preceded by channel count setting, mapping, and baudrate configuration. ### Method `tsapp_connect()` `tsapp_disconnect()` ### Parameters None for these functions. ### Request Example ```python from TSMasterAPI import * # Connect to hardware device if tsapp_connect() == 0: print("Hardware connection successful") # Enable FIFO reception after successful connection tsfifo_enable_receive_fifo() # ... perform message send/receive operations ... # Disconnect from hardware tsapp_disconnect() else: print("Hardware connection failed") ``` ### Response - **Return Value** (int) - 0 indicates success, non-zero indicates failure. ``` -------------------------------- ### Enumerate Hardware Devices Source: https://context7.com/sy950915/tsmasterapi/llms.txt Retrieves information about all connected TSMaster-compatible hardware devices. ```python from TSMasterAPI import * from ctypes import c_int32 # 枚举硬件设备 ACount = c_int32(0) result = tsapp_enumerate_hw_devices(ACount) if result == 0: print(f"发现 {ACount.value} 个硬件设备") # 获取每个设备的详细信息 hwInfo = TLIBHWInfo() for i in range(ACount.value): tsapp_get_hw_info_by_index(i, hwInfo) print(f"设备 {i}: 类型={hwInfo.FDeviceType}, " f"名称={hwInfo.FDeviceName.decode('utf8')}, " f"序列号={hwInfo.FSerialString.decode('utf8')}") ``` -------------------------------- ### Connect and Disconnect Hardware Source: https://context7.com/sy950915/tsmasterapi/llms.txt Manages the connection state of the configured hardware. Requires prior channel and baud rate configuration. ```python from TSMasterAPI import * # 连接硬件设备 if tsapp_connect() == 0: print("硬件连接成功") # 连接成功后启用 FIFO 接收 tsfifo_enable_receive_fifo() # ... 执行报文收发操作 ... # 断开硬件连接 tsapp_disconnect() else: print("硬件连接失败") ``` -------------------------------- ### Hardware Device Enumeration Source: https://context7.com/sy950915/tsmasterapi/llms.txt Enumerates available TSMaster compatible hardware devices and retrieves their information. ```APIDOC ## Hardware Device Enumeration ### Description Enumerates all available TSMaster compatible hardware devices and retrieves detailed information about each device. ### Method `tsapp_enumerate_hw_devices(count)` `tsapp_get_hw_info_by_index(index, hw_info)` ### Parameters #### Function: `tsapp_enumerate_hw_devices` ##### Path Parameters - **count** (c_int32) - Output - A ctypes integer to store the count of found hardware devices. #### Function: `tsapp_get_hw_info_by_index` ##### Path Parameters - **index** (int) - Required - The index of the hardware device to query. - **hw_info** (TLIBHWInfo) - Output - A TLIBHWInfo object to store the hardware information. ### Request Example ```python from TSMasterAPI import * from ctypes import c_int32 # Enumerate hardware devices ACount = c_int32(0) result = tsapp_enumerate_hw_devices(ACount) if result == 0: print(f"Found {ACount.value} hardware devices") # Get detailed information for each device hwInfo = TLIBHWInfo() for i in range(ACount.value): tsapp_get_hw_info_by_index(i, hwInfo) print(f"Device {i}: Type={hwInfo.FDeviceType}, " f"Name={hwInfo.FDeviceName.decode('utf8')}, " f"Serial={hwInfo.FSerialString.decode('utf8')}") ``` ### Response - **Return Value** (int) - 0 indicates success, non-zero indicates failure. - **hwInfo** (TLIBHWInfo) - Contains details like FDeviceType, FDeviceName, FSerialString. ``` -------------------------------- ### Set CAN and LIN Channel Counts Source: https://context7.com/sy950915/tsmasterapi/llms.txt Configures the number of available CAN and LIN channels. Must be called before hardware connection. ```python from TSMasterAPI import * # 设置 CAN 通道数量为 2 if tsapp_set_can_channel_count(2) == 0: print("CAN 通道设置成功") else: print("CAN 通道设置失败") # 设置 LIN 通道数量为 1 if tsapp_set_lin_channel_count(1) == 0: print("LIN 通道设置成功") else: print("LIN 通道设置失败") ``` -------------------------------- ### POST /api/load_dbc Source: https://context7.com/sy950915/tsmasterapi/llms.txt Loads a DBC database file into the TSMaster environment for signal parsing. ```APIDOC ## POST /api/load_dbc ### Description Loads a DBC file and associates it with specific channels. ### Method POST ### Parameters - **path** (string) - File path to the DBC. - **channels** (string) - Comma-separated channel indices. - **handle** (int) - Output variable for the database ID. ``` -------------------------------- ### Library Initialization and Finalization Source: https://context7.com/sy950915/tsmasterapi/llms.txt Initializes and finalizes the TSMaster library resources. Initialization must be called before any other API functions. ```APIDOC ## Library Initialization and Finalization ### Description Initializes and finalizes the TSMaster library resources. Initialization must be called before any other API functions. ### Method `initialize_lib_tsmaster(app_name)` `finalize_lib_tsmaster()` ### Parameters #### Function: `initialize_lib_tsmaster` ##### Path Parameters - **app_name** (bytes) - Required - The name of the application. ### Request Example ```python from TSMasterAPI import * # Initialize TSMaster library initialize_lib_tsmaster("TSMaster".encode("utf8")) # ... perform other operations ... # Release library resources finalize_lib_tsmaster() ``` ### Response No explicit response details provided for these functions, success is typically indicated by the absence of errors. ``` -------------------------------- ### Channel Count Settings Source: https://context7.com/sy950915/tsmasterapi/llms.txt Sets the number of CAN and LIN channels available for use. ```APIDOC ## Channel Count Settings ### Description Sets the number of CAN and LIN channels available for use. These functions must be called before hardware connection. ### Method `tsapp_set_can_channel_count(count)` `tsapp_set_lin_channel_count(count)` ### Parameters #### Function: `tsapp_set_can_channel_count` ##### Path Parameters - **count** (int) - Required - The number of CAN channels. #### Function: `tsapp_set_lin_channel_count` ##### Path Parameters - **count** (int) - Required - The number of LIN channels. ### Request Example ```python from TSMasterAPI import * # Set CAN channel count to 2 if tsapp_set_can_channel_count(2) == 0: print("CAN channel setup successful") else: print("CAN channel setup failed") # Set LIN channel count to 1 if tsapp_set_lin_channel_count(1) == 0: print("LIN channel setup successful") else: print("LIN channel setup failed") ``` ### Response - **Return Value** (int) - 0 indicates success, non-zero indicates failure. ``` -------------------------------- ### Define CAN Message Structure Source: https://context7.com/sy950915/tsmasterapi/llms.txt Demonstrates how to initialize and populate the TLIBCAN structure for message transmission. ```python from TSMasterAPI import * # 方式1:逐字段设置 msg = TLIBCAN() msg.FIdxChn = 0 # 通道0 msg.FIdentifier = 0x100 # 报文ID msg.FProperties = 0x05 # 扩展帧 + TX msg.FDLC = 8 # 数据长度8字节 # 设置数据 FData = [0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17] for i in range(len(FData)): msg.FData[i] = FData[i] # 方式2:构造函数初始化 msg2 = TLIBCAN( FIdxChn=0, FIdentifier=0x111, FProperties=1, FDLC=8, FData=[0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17] ) # 打印报文信息 print(msg2) # 输出格式化的报文信息 ``` -------------------------------- ### 定义 LIN 报文结构 Source: https://context7.com/sy950915/tsmasterapi/llms.txt 使用 TLIBLIN 结构体创建 LIN 报文,支持直接赋值或通过构造函数初始化。 ```python from TSMasterAPI import * # 创建 LIN 报文 lin_msg = TLIBLIN() lin_msg.FIdxChn = 0 # 通道0 lin_msg.FIdentifier = 0x10 # LIN ID (0-63) lin_msg.FDLC = 8 # 数据长度 lin_msg.FProperties = 1 # 发送方向 # 设置数据 for i in range(8): lin_msg.FData[i] = i * 0x11 # 使用构造函数 lin_msg2 = TLIBLIN( FIdxChn=0, FIdentifier=0x20, FDLC=8, FProperties=1, FData=[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08] ) # 打印报文信息 print(lin_msg2) ``` -------------------------------- ### 定义 FlexRay 报文结构 Source: https://context7.com/sy950915/tsmasterapi/llms.txt 使用 TLIBFlexRay 结构体定义 FlexRay 报文,包含 Slot ID、通道掩码及有效载荷等属性。 ```python from TSMasterAPI import * # 创建 FlexRay 报文 fr_msg = TLIBFlexRay() fr_msg.FIdxChn = 0 # 通道索引 fr_msg.FSlotId = 10 # Slot ID fr_msg.FChannelMask = 1 # 通道 A fr_msg.FActualPayloadLength = 32 # 实际数据长度 fr_msg.FCycleNumber = 1 # 周期号 # 设置数据 for i in range(32): fr_msg.FData[i] = i # 使用构造函数 fr_msg2 = TLIBFlexRay( FIdxChn=0, FSlotId=20, FChannelMask=1, FActualPayloadLength=16, FCycleNumber=0, FData=[0x10 + i for i in range(16)] ) # 打印报文信息 print(fr_msg2) ``` -------------------------------- ### Hardware Channel Mapping Source: https://context7.com/sy950915/tsmasterapi/llms.txt Maps physical hardware channels to software logical channels. ```APIDOC ## Hardware Channel Mapping ### Description Maps physical hardware channels to software logical channels, supporting various TSMaster hardware device types. ### Method `tsapp_set_mapping_verbose(app_name, channel_type, channel_index, device_name, device_type, device_sub_type, device_index, hw_channel_index, enable_mapping)` ### Parameters #### Path Parameters - **app_name** (bytes) - Required - The name of the application. - **channel_type** (TLIBApplicationChannelType) - Required - The type of channel (e.g., APP_CAN, APP_LIN). - **channel_index** (CHANNEL_INDEX) - Required - The software logical channel index (e.g., CHN1). - **device_name** (bytes) - Required - The name of the hardware device (e.g., b"TC1016"). - **device_type** (TLIBBusToolDeviceType) - Required - The type of the device (e.g., TS_USB_DEVICE). - **device_sub_type** (TLIB_TS_Device_Sub_Type) - Required - The sub-type of the device (e.g., TC1016). - **device_index** (int) - Required - The index of the hardware device. - **hw_channel_index** (CHANNEL_INDEX) - Required - The index of the hardware channel. - **enable_mapping** (bool) - Required - Whether to enable the mapping. ### Request Example ```python from TSMasterAPI import * AppName = "TSMaster".encode("utf8") # Map CAN channel 1 to the first channel of TC1016 hardware result = tsapp_set_mapping_verbose( AppName, TLIBApplicationChannelType.APP_CAN, # Channel type: CAN CHANNEL_INDEX.CHN1, # Software channel index: Channel 1 "TC1016".encode("UTF8"), # Hardware device name TLIBBusToolDeviceType.TS_USB_DEVICE, # Device type: USB device TLIB_TS_Device_Sub_Type.TC1016, # Device sub-type: TC1016 0, # Hardware index CHANNEL_INDEX.CHN1, # Hardware channel index True # Enable mapping ) if result == 0: print("Channel mapping successful") else: print("Channel mapping failed") ``` ### Response - **Return Value** (int) - 0 indicates success, non-zero indicates failure. ``` -------------------------------- ### 激活 RBS 网络、节点和报文 Source: https://context7.com/sy950915/tsmasterapi/llms.txt 通过名称激活指定的网络、节点或报文,以启动周期性发送。只有激活后的报文才会被仿真发送。 ```python from TSMasterAPI import * # 激活网络(通道0) result = tscom_can_rbs_activate_network_by_name( 0, # 通道索引 True, # 激活 b"CAN_FD_Powertrain", # 网络名称 False # 不包含子节点 ) print(f"网络激活: {result == 0}") # 激活节点 result = tscom_can_rbs_activate_node_by_name( 0, # 通道索引 True, # 激活 b"CAN_FD_Powertrain", # 网络名称 b"Engine", # 节点名称 False # 不包含子报文 ) print(f"节点激活: {result == 0}") # 激活指定报文 result = tscom_can_rbs_activate_message_by_name( 0, # 通道索引 True, # 激活 b"CAN_FD_Powertrain", # 网络名称 b"Engine", # 节点名称 b"GearBoxInfo" # 报文名称 ) print(f"报文激活: {result == 0}") ``` -------------------------------- ### Create UDS Diagnostic Module (CAN) Source: https://context7.com/sy950915/tsmasterapi/llms.txt Use `tsdiag_can_create` to set up a UDS diagnostic module over CAN. Specify channel, timeouts, IDs, and frame types. A successful creation returns a handle for subsequent diagnostic operations. ```python from TSMasterAPI import * from ctypes import c_int32, c_byte # Create UDS diagnostic module uds_handle = c_byte(0) result = tsdiag_can_create( uds_handle, # Diagnostic module handle CHANNEL_INDEX.CHN1, # CAN channel 0, # Timeout (0 uses default) 8, # Data length 0x7E0, # Physical Request ID True, # Request ID is extended frame 0x7E8, # Physical Response ID True, # Response ID is extended frame 0x7DF, # Functional Request ID True # Functional ID is extended frame ) if result == 0: print(f"UDS diagnostic module created successfully, handle: {uds_handle.value}") else: error_desc = c_char_p() tsapp_get_error_description(result, error_desc) print(f"Creation failed: {error_desc.value}") ``` -------------------------------- ### 读写 RBS 仿真信号值 Source: https://context7.com/sy950915/tsmasterapi/llms.txt 使用网络、节点、报文和信号名称定位并操作信号值。需要使用 ctypes 库处理数据类型。 ```python from TSMasterAPI import * from ctypes import c_double # 设置信号值 result = tscom_can_rbs_set_signal_value_by_element( 0, # 通道索引 "CAN_FD_Powertrain".encode("utf8"), # 网络名称 "Engine".encode("utf8"), # 节点名称 "GearBoxInfo".encode("utf8"), # 报文名称 "Gear".encode("utf8"), # 信号名称 c_double(3.0) # 信号值 ) if result == 0: print("信号值设置成功") # 读取信号值 signal_value = c_double(0) result = tscom_can_rbs_get_signal_value_by_element( 0, # 通道索引 "CAN_FD_Powertrain".encode("utf8"), # 网络名称 "Engine".encode("utf8"), # 节点名称 "GearBoxInfo".encode("utf8"), # 报文名称 "Gear".encode("utf8"), # 信号名称 signal_value # 接收信号值 ) if result == 0: print(f"当前信号值: {signal_value.value}") ``` -------------------------------- ### Configure CANFD Baud Rate Source: https://context7.com/sy950915/tsmasterapi/llms.txt Sets baud rate parameters for CANFD channels, including arbitration/data rates and controller modes. ```python from TSMasterAPI import * # 配置通道1的 CANFD 波特率 # 仲裁段:500kbps,数据段:2000kbps result = tsapp_configure_baudrate_canfd( CHANNEL_INDEX.CHN1, # 通道索引 500.0, # 仲裁段波特率 (kbps) 2000.0, # 数据段波特率 (kbps) TLIBCANFDControllerType.lfdtISOCAN, # 控制器类型:ISO CANFD TLIBCANFDControllerMode.lfdmNormal, # 控制器模式:正常模式 True # 安装120欧姆终端电阻 ) if result == 0: print("CANFD 波特率配置成功") else: print("CANFD 波特率配置失败") ``` -------------------------------- ### Load and Unload CAN DBC Database Files Source: https://context7.com/sy950915/tsmasterapi/llms.txt Loads CAN DBC database files using tsdb_load_can_db, specifying the file path, associated channels, and receiving a database handle. Unloads all loaded DBC files with tsdb_unload_can_dbs. ```python from TSMasterAPI import * from ctypes import c_int32 # 加载 DBC 文件 db_handle = c_int32(0) dbc_path = "C:/path/to/database.dbc".encode("utf8") result = tsdb_load_can_db(dbc_path, b"0,1", db_handle) if result == 0: print(f"DBC 加载成功, 句柄: {db_handle.value}") else: print(f"DBC 加载失败, 错误码: {result}") # ... 使用数据库进行信号解析 ... # 卸载所有 DBC 文件 if tsdb_unload_can_dbs() == 0: print("DBC 文件已全部卸载") ``` -------------------------------- ### Receive CAN/CANFD Messages via FIFO Source: https://context7.com/sy950915/tsmasterapi/llms.txt Enables FIFO reception with tsfifo_enable_receive_fifo and reads messages using tsfifo_receive_can_msgs and tsfifo_receive_canfd_msgs. Allows specifying channel and read type (receive/transmit/both). ```python from TSMasterAPI import * from ctypes import c_int32 # 启用 FIFO 接收 tsfifo_enable_receive_fifo() # 准备接收缓冲区 can_buffer = (TLIBCAN * 100)() # 100 条 CAN 报文缓冲 canfd_buffer = (TLIBCANFD * 100)() # 100 条 CANFD 报文缓冲 can_count = c_int32(100) canfd_count = c_int32(100) # 接收报文(通道0,收发报文都读取) result_can = tsfifo_receive_can_msgs(can_buffer, can_count, 0, READ_TX_RX_DEF.TX_RX_MESSAGES) result_canfd = tsfifo_receive_canfd_msgs(canfd_buffer, canfd_count, 0, READ_TX_RX_DEF.TX_RX_MESSAGES) # 处理接收到的 CAN 报文 print(f"接收到 {can_count.value} 条 CAN 报文") for i in range(can_count.value): msg = can_buffer[i] print(f"ID: 0x{msg.FIdentifier:X}, DLC: {msg.FDLC}, Data: {[hex(msg.FData[j]) for j in range(msg.FDLC)]}") # 处理接收到的 CANFD 报文 print(f"接收到 {canfd_count.value} 条 CANFD 报文") for i in range(canfd_count.value): msg = canfd_buffer[i] print(f"ID: 0x{msg.FIdentifier:X}, DLC: {msg.FDLC}") ``` -------------------------------- ### Set Up Cyclic Transmission for CAN/CANFD Messages Source: https://context7.com/sy950915/tsmasterapi/llms.txt Configures periodic automatic transmission of CAN and CANFD messages using tsapp_add_cyclic_msg_can and tsapp_add_cyclic_msg_canfd. Use tsapp_delete_cyclic_msg_can and tsapp_delete_cyclic_msg_canfd to stop these tasks. ```python from TSMasterAPI import * # 创建报文 msg = TLIBCAN(FIdentifier=0x100, FData=[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]) fdmsg = TLIBCANFD(FIdentifier=0x200, FFDProperties=1, FData=[0x10, 0x11, 0x12, 0x13]) # 添加周期发送任务,周期100毫秒 ret1 = tsapp_add_cyclic_msg_can(msg, 100) ret2 = tsapp_add_cyclic_msg_canfd(fdmsg, 100) if ret1 == 0 and ret2 == 0: print("周期发送任务添加成功") # ... 运行一段时间后停止周期发送 ... # 删除周期发送任务 tsapp_delete_cyclic_msg_can(msg) tsapp_delete_cyclic_msg_canfd(fdmsg) print("周期发送任务已停止") ``` -------------------------------- ### CANFD Baudrate Configuration Source: https://context7.com/sy950915/tsmasterapi/llms.txt Configures CANFD channel's arbitration and data baudrates, controller type, and mode. ```APIDOC ## CANFD Baudrate Configuration ### Description Configures CANFD channel's arbitration and data baudrates, controller type, and mode. ### Method `tsapp_configure_baudrate_canfd(channel_index, arbitration_baudrate_kbps, data_baudrate_kbps, controller_type, controller_mode, enable_termination)` ### Parameters #### Path Parameters - **channel_index** (CHANNEL_INDEX) - Required - The channel index (e.g., CHN1). - **arbitration_baudrate_kbps** (float) - Required - The arbitration segment baudrate in kbps. - **data_baudrate_kbps** (float) - Required - The data segment baudrate in kbps. - **controller_type** (TLIBCANFDControllerType) - Required - The controller type (e.g., lfdtISOCAN). - **controller_mode** (TLIBCANFDControllerMode) - Required - The controller mode (e.g., lfdmNormal). - **enable_termination** (bool) - Required - Whether to enable the 120 Ohm termination resistor. ### Request Example ```python from TSMasterAPI import * # Configure CANFD baudrate for channel 1 # Arbitration: 500kbps, Data: 2000kbps result = tsapp_configure_baudrate_canfd( CHANNEL_INDEX.CHN1, # Channel index 500.0, # Arbitration baudrate (kbps) 2000.0, # Data baudrate (kbps) TLIBCANFDControllerType.lfdtISOCAN, # Controller type: ISO CANFD TLIBCANFDControllerMode.lfdmNormal, # Controller mode: Normal mode True # Enable 120 Ohm termination resistor ) if result == 0: print("CANFD baudrate configuration successful") else: print("CANFD baudrate configuration failed") ``` ### Response - **Return Value** (int) - 0 indicates success, non-zero indicates failure. ``` -------------------------------- ### Read Messages from BLF File Source: https://context7.com/sy950915/tsmasterapi/llms.txt Open a BLF file using `tslog_blf_read_start`, read messages iteratively with `tslog_blf_read_object`, and close the file with `tslog_blf_read_end`. This function supports reading CAN, CANFD, and LIN messages. ```python from TSMasterAPI import * from ctypes import c_int32, c_ulong # Open BLF file blf_id = c_int32(0) msg_count = c_ulong(0) blf_path = "E:\\logs\\can_log.blf" result = tslog_blf_read_start(blf_path, blf_id, msg_count) if result == 0: print(f"BLF file opened successfully, {msg_count.value} messages in total") # Read messages real_count = c_ulong(0) msg_type = TSupportedObjType.sotUnknown can_msg = TLIBCAN() canfd_msg = TLIBCANFD() lin_msg = TLIBLIN() for i in range(msg_count.value): tslog_blf_read_object(blf_id, real_count, msg_type, can_msg, lin_msg, canfd_msg) if msg_type.value == TSupportedObjType.sotCAN.value: print(f"CAN: Time={can_msg.FTimeUs/1000000:.6f}s, " f"ID=0x{can_msg.FIdentifier:X}, DLC={can_msg.FDLC}") elif msg_type.value == TSupportedObjType.sotCANFD.value: print(f"CANFD: Time={canfd_msg.FTimeUs/1000000:.6f}s, " f"ID=0x{canfd_msg.FIdentifier:X}, DLC={canfd_msg.FDLC}") # Close file tslog_blf_read_end(blf_id) print("BLF file closed") ``` -------------------------------- ### POST /api/transmit_async Source: https://context7.com/sy950915/tsmasterapi/llms.txt Asynchronously transmits CAN and CANFD messages to the bus without blocking the main execution thread. ```APIDOC ## POST /api/transmit_async ### Description Sends CAN or CANFD messages to the bus asynchronously. Messages are queued in the background. ### Method POST ### Parameters - **msg** (TLIBCAN/TLIBCANFD) - The message object to transmit. ### Response - **result** (int) - Returns 0 if the message was successfully added to the queue. ``` -------------------------------- ### Register CAN Message Pre-Transmission Event Callback Source: https://context7.com/sy950915/tsmasterapi/llms.txt Registers a callback function for pre-transmission CAN events using tsapp_register_pretx_event_can. The callback receives a pointer to the CAN message and can modify its content before sending. ```python from TSMasterAPI import * from ctypes import c_int32 # 定义回调函数 def on_can_event(obj, acan): """CAN 报文回调处理函数""" # 检查特定 ID 的报文 if acan.contents.FIdentifier == 0x111 and acan.contents.FIdxChn == 0: # 修改报文数据 acan.contents.FData[0] += 1 print(f"处理报文 ID=0x{acan.contents.FIdentifier:X}") # 创建回调函数包装器 on_can_callback = TCANQueueEvent_Win32(on_can_event) obj = c_int32(0) # 注册回调事件 result = tsapp_register_pretx_event_can(obj, on_can_callback) if result == 0: print("回调事件注册成功") else: print("回调事件注册失败") ``` -------------------------------- ### POST /api/register_event Source: https://context7.com/sy950915/tsmasterapi/llms.txt Registers callback functions for handling CAN events such as pre-transmission modifications. ```APIDOC ## POST /api/register_event ### Description Registers a callback function to be triggered on specific CAN events. ### Method POST ### Parameters - **obj** (int) - Context object. - **callback** (function) - The function to execute on event. ``` -------------------------------- ### Transmit CAN and CANFD Messages Asynchronously Source: https://context7.com/sy950915/tsmasterapi/llms.txt Asynchronously transmits CAN and CANFD messages to the bus using tsapp_transmit_can_async and tsapp_transmit_canfd_async. Returns 0 if the message is successfully queued for transmission. ```python from TSMasterAPI import * # 创建 CAN 报文 can_msg = TLIBCAN(FIdentifier=0x100, FData=[0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08]) # 创建 CANFD 报文 canfd_msg = TLIBCANFD(FIdentifier=0x200, FFDProperties=1, FDLC=9, FData=[0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B]) # 发送报文 for i in range(10): result_can = tsapp_transmit_can_async(can_msg) result_canfd = tsapp_transmit_canfd_async(canfd_msg) if result_can == 0 and result_canfd == 0: print("报文发送成功") else: print(f"发送失败: CAN={result_can}, CANFD={result_canfd}") ``` -------------------------------- ### CAN Message Structure Source: https://context7.com/sy950915/tsmasterapi/llms.txt Defines the data structure for CAN messages used in sending and receiving. ```APIDOC ## CAN Message Structure ### Description Defines the data structure for CAN messages, used for both sending and receiving. ### Structure `TLIBCAN` ### Fields - **FIdxChn** (int) - Channel index. - **FIdentifier** (int) - Message identifier. - **FProperties** (int) - Property flags (e.g., frame type, direction). - **FDLC** (int) - Data Length Code (DLC). - **FData** (list of int) - Data payload (up to 8 bytes). - **FTimeStamp** (float) - Timestamp of the message. ### Request Example ```python from TSMasterAPI import * # Method 1: Field by field assignment msg = TLIBCAN() msg.FIdxChn = 0 # Channel 0 msg.FIdentifier = 0x100 # Message ID msg.FProperties = 0x05 # Extended frame + TX msg.FDLC = 8 # Data length 8 bytes # Set data FData = [0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17] for i in range(len(FData)): msg.FData[i] = FData[i] # Method 2: Initialization via constructor msg2 = TLIBCAN( FIdxChn=0, FIdentifier=0x111, FProperties=1, FDLC=8, FData=[0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17] ) # Print message information print(msg2) # Outputs formatted message information ``` ### Response No explicit response details provided for message structure definition, as it's a data structure definition. ``` -------------------------------- ### POST /api/cyclic_msg Source: https://context7.com/sy950915/tsmasterapi/llms.txt Configures periodic transmission for CAN and CANFD messages with a specified cycle time. ```APIDOC ## POST /api/cyclic_msg ### Description Adds or removes cyclic transmission tasks for CAN/CANFD messages. ### Method POST ### Parameters - **msg** (TLIBCAN/TLIBCANFD) - The message object. - **cycle_time** (int) - Transmission period in milliseconds. ### Response - **result** (int) - Returns 0 on success. ``` -------------------------------- ### Define CANFD Message Structure Source: https://context7.com/sy950915/tsmasterapi/llms.txt Defines the TLIBCANFD structure for CANFD messages, supporting up to 64 bytes of data payload and FD properties like EDL, BRS, and ESI. DLC values map to specific byte lengths. ```python from TSMasterAPI import * # 创建 CANFD 报文 fdmsg = TLIBCANFD() fdmsg.FIdxChn = 0 # 通道0 fdmsg.FIdentifier = 0x101 # 报文ID fdmsg.FProperties = 0x05 # 扩展帧 fdmsg.FFDProperties = 0x03 # EDL=1, BRS=1 (FD帧,启用波特率切换) fdmsg.FDLC = 9 # DLC=9 对应 12 字节数据 # 设置 12 字节数据 FData = [0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B] for i in range(len(FData)): fdmsg.FData[i] = FData[i] # 使用构造函数创建 fdmsg2 = TLIBCANFD( FIdxChn=0, FIdentifier=0x200, FProperties=1, FFDProperties=1, FDLC=15, # 64字节 FData=[i for i in range(64)] ) ``` -------------------------------- ### CANFD Message Structure Source: https://context7.com/sy950915/tsmasterapi/llms.txt Defines the TLIBCANFD structure for handling CANFD frames with up to 64 bytes of data and specific FD properties. ```APIDOC ## CANFD Message Structure ### Description The TLIBCANFD structure supports CANFD frames. It includes FFDProperties for FD-specific flags (EDL, BRS, ESI) and supports data payloads up to 64 bytes. ### Data Structure - **FIdxChn** (int) - Channel index - **FIdentifier** (int) - Message ID - **FProperties** (int) - Frame properties (e.g., extended frame) - **FFDProperties** (int) - FD properties (EDL, BRS) - **FDLC** (int) - Data length code (0-15) - **FData** (array) - Data payload ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.