发送消息
最后修改:
该函数通过诊断协议发送消息。适配器的发送队列每个通道的单个队列可容纳 50 条消息,所有队列共有 64 KB 的可用内存。当队列已满或全部可用内存耗尽时,发送队列将暂停接收消息。
long PassThruWriteMsg(unsigned long ChannelID, PASSTHRU_MSG *pMsg, unsigned long *pNumMsgs, unsigned long Timeout)
PassThruStartMsgFilter 设置 Flow Control 过滤器。否则函数将返回错误 ERR_NO_FLOW_CONTROL。
PassThruConnect 命令获得。ERR_TIMEOUT。如果超时为 0,则消息被放入发送队列而不等待物理发送,函数立即返回。在这种情况下不会产生 ERR_TIMEOUT 错误。| 代码 | 说明 | 可能的原因与解决方法 |
|---|---|---|
| STATUS_NOERROR | 函数执行成功 | — |
| ERR_DEVICE_NOT_CONNECTED | 未与适配器建立连接 |
|
| ERR_INVALID_CHANNEL_ID | 指定了不存在的通道标识符 ChannelID |
|
| ERR_NULL_PARAMETER | 未指定指针 pMsg 或 pNumMsgs |
|
| ERR_TIMEOUT | 在规定时间内未能发送全部消息 |
|
| ERR_INVALID_MSG | pMsg 中的消息结构不正确 |
|
| ERR_MSG_PROTOCOL_ID | 消息中的协议与通道协议不一致 |
|
| ERR_NO_FLOW_CONTROL | 对于 ISO 15765 协议未设置 Flow Control 过滤器 |
|
| ERR_BUFFER_FULL | 发送队列已满 |
|
| ERR_FAILED | 库或适配器内部错误 |
|
#include "j2534_dll.hpp"
// ChannelID 此前已通过 PassThruConnect 获得
unsigned long ChannelID;
PASSTHRU_MSG Msg;
unsigned long NumMsgs = 1;
unsigned long Timeout = 200;
// 构建 ISO 15765 消息(请求 SID 0x22, PID 0xF190)
Msg.ProtocolID = ISO15765;
Msg.TxFlags = ISO15765_FRAME_PAD;
Msg.Data[0] = 0x00;
Msg.Data[1] = 0x00;
Msg.Data[2] = 0x07;
Msg.Data[3] = 0xDF;
Msg.Data[4] = 0x22;
Msg.Data[5] = 0xF1;
Msg.Data[6] = 0x90;
Msg.DataSize = 7;
long ret = PassThruWriteMsg(ChannelID, &Msg, &NumMsgs, Timeout);
if (ret != STATUS_NOERROR) {
char error[256];
PassThruGetLastError(error);
// 错误处理
}
// channelID 此前已通过 ptConnect 获得
val msg = PassThruMsg(
protocolID = ISO15765,
dataSize = 7,
txFlags = ISO15765_FRAME_PAD,
// 请求 VIN(SID 0x22, PID 0xF190)
data = byteArrayOf(0x00, 0x00, 0x07, 0xDF.toByte(), 0x22, 0xF1.toByte(), 0x90.toByte())
)
val messages = arrayOf(msg)
val timeout = 200 // ms
val result = j2534.ptWriteMsgs(channelID, messages, timeout)
if (result.status == STATUS_NOERROR) {
Log.i("J2534", "已发送消息数: ${result.numMsgs}")
} else {
Log.e("J2534", "发送错误: ${result.status}")
}
# channel_id 此前已通过 PassThruConnect 获得
msg = PassThruMsg()
msg.ProtocolID = ISO15765
msg.TxFlags = ISO15765_FRAME_PAD
msg.Data = bytes([0x00, 0x00, 0x07, 0xDF, 0x22, 0xF1, 0x90])
msg.DataSize = 7
num_msgs = ctypes.c_ulong(1)
timeout = 200 # ms
ret = j2534.PassThruWriteMsg(channel_id, ctypes.byref(msg), ctypes.byref(num_msgs), timeout)
if ret == 0: # STATUS_NOERROR
print(f"已发送消息数: {num_msgs.value}")
else:
print(f"发送错误: {ret}")
// channelId 此前已通过 PassThruConnect 获得
var msg = new PassThruMsg {
ProtocolID = ISO15765,
TxFlags = ISO15765_FRAME_PAD,
Data = new byte[] { 0x00, 0x00, 0x07, 0xDF, 0x22, 0xF1, 0x90 },
DataSize = 7
};
uint numMsgs = 1;
uint timeout = 200; // ms
int ret = J2534.PassThruWriteMsg(channelId, ref msg, ref numMsgs, timeout);
if (ret == 0) {
Console.WriteLine($"已发送消息数: {numMsgs}");
} else {
Console.WriteLine($"发送错误: {ret}");
}