อ้างอิง API
ทุก export ของ
@smongrot/sdkและsmongrot_sdk: ชนิด ค่าเริ่มต้น และตัวอย่างเรียกสั้น ๆ
เปิด session ด้วย connect อย่าสร้าง Session เอง (ใน Dart คอนสตรักเตอร์เป็น private) ชื่อตรงกันทั้งสองภาษา ยกเว้นแถวด้านล่าง
| JavaScript | Dart | |
|---|---|---|
| แพ็กเกจ | @smongrot/sdk |
smongrot_sdk |
| ข้อผิดพลาด | SmongrotError extends Error |
SmongrotException implements Exception |
live() |
async generator | Stream<LiveFrame> |
LiveFrame.at |
มิลลิวินาทีตั้งแต่ epoch (number) |
DateTime |
หยุด live() |
break หรือ signal: AbortSignal |
break, ยกเลิก subscription หรือ await stream.first |
DTC status |
status: 'beta' และ beta: true |
getter beta เป็น true ไม่มีฟิลด์ status |
| แก้จังหวะเวลา | อ็อบเจกต์บางฟิลด์ ผสานเข้า DEFAULT_TIMING |
อินสแตนซ์ Timing(...) |
| ค่าคงที่ฐาน API | DEFAULT_API_BASE |
defaultApiBase |
| สเปกที่ฝังมา | SPEC |
spec |
| reply ของม้านั่ง | BENCH_SESSION |
benchSession |
| ปิดตัวช่วย HTTP | ไม่มี | api.close() |
ไม่มี fetch ตอนเริ่ม |
throw NO_FETCH |
ไม่มีกรณีนี้ (http เป็น dependency) |
core.* ใน JavaScript คือฟังก์ชันชุดเดียวกับที่ Dart export ที่ระดับบน (import 'package:smongrot_sdk/smongrot_sdk.dart')
connect
connect(transport: Transport, opts?: ConnectOptions): Promise<Session>
Future<Session> connect(Transport transport, {
bool matchV2 = false,
String? makerHint,
FutureOr<ModelInfo?> Function(String ecmId)? lookupModel,
Timing? timing,
Sleep? sleep,
DateTime Function()? now,
})
ปลุก ECU ส่ง StartComm รอ reply ของ liveness ตัวใดก็ได้ แล้วส่ง StartDiag คืนตอน state === 'ready'
Throw ECU_SILENT หลัง wakeTries (ค่าเริ่มต้น 4) Throw NO_WAKE เมื่อ transport ของ JS ไม่มีทั้ง wake และ setBreak
const s = await connect(transport, { makerHint: 'keihin' });
final s = await connect(transport, makerHint: 'keihin');
ConnectOptions / อาร์กิวเมนต์แบบชื่อ
| ชื่อ | ชนิด | ค่าเริ่มต้น | |
|---|---|---|---|
matchV2 |
boolean |
false |
ใช้กติกา match_v2 ในโปรไฟล์ที่สเปก live มี ปล่อยปิดไว้ frame ที่เข้าได้เฉพาะกติกานั้นจะตั้งฟิลด์ใน v2_provisional เป็น provisional |
makerHint |
'keihin' | 'shindengen' |
ไม่ตั้ง | ใช้กับตัวถอดรหัสข้อผิดพลาด และกับ detect().mismatch ไม่ได้บังคับโปรไฟล์ live |
lookupModel |
(ecmId: string) => ModelInfo | null |
ไม่ตั้ง | ไม่บังคับ session ไม่เปิดซ็อกเก็ตเอง throw ในฮุกกลายเป็น model: null |
timing |
Timing บางฟิลด์ (JS) / Timing (Dart) |
ค่าด้านล่าง | ทับจังหวะตอนเปิด session และการโพล |
sleep |
JS (ms: number) => Promise<void> / Dart Future<void> Function(Duration) |
ตัวจับเวลาจริง | เทสส่งฟังก์ชันว่าง |
now |
JS () => number / Dart DateTime Function() |
Date.now / DateTime.now |
นาฬิกาสำหรับ timeout และ LiveFrame.at |
Session
state เป็น 'idle' | 'waking' | 'ready' | 'live' | 'closed' อ่านได้ อย่ากำหนดเอง
identify, detect, live และ dtc.read ต้องอยู่ใน 'ready' ไม่งั้น BAD_STATE
identify() → Identity
ส่ง 72 07 72 00 00 05 10 สูงสุด 3 ครั้ง ห่างกัน 300 ms ถ้าไม่ได้ใช้ ECM-ID ที่เก็บจาก StartDiag จากนั้นถ้าตั้ง lookupModel และมี id จะเรียกครั้งเดียว ผลถูกจำบน session
const id = await s.identify();
id.ecmId; // "0104780D01" | null
id.source; // "id_frame" | "start_diag" | null
id.model; // ModelInfo | null
final id = await s.identify();
id.ecmId;
id.model?.model;
Identity
| ฟิลด์ | ชนิด | |
|---|---|---|
ecmId |
string | null |
เลขฐานสิบหกตัวใหญ่ 10 ตัว หรือ null |
ecmIdBytes |
number[] | null / List<int>? |
5 ไบต์เดียวกัน หรือ null |
source |
'id_frame' | 'start_diag' | null |
frame ไหนให้ ecmId |
model |
ModelInfo | null |
จาก lookupModel ไม่งั้น null |
ModelInfo
| ฟิลด์ | ชนิด | |
|---|---|---|
model |
string? |
ชื่อที่คนขี่ใช้ จาก API อาจเป็นภาษาไทย และไม่มีปี |
maker |
'keihin' | 'shindengen'? |
|
partCode |
string? |
รหัสอะไหล่เต็มเมื่อเข้ากับ NNNNN-… (ใช้กติกาผู้ผลิตของ DTC) |
ecuCode |
string? |
รหัสอะไหล่เต็มตามที่ API ส่ง เช่น 38770-K03-H01 |
liveProfile |
string? |
คำแนะนำ มักเป็น null live() ตรวจโปรไฟล์เอง |
ecmId |
string? |
id ที่ API ตอบ |
verified |
boolean |
true ถ้า ECM-ID นี้ถูกยืนยันบนกล่องจริง modelInfoFromApi ตั้ง false เมื่อ API ไม่ได้ส่ง true คอนสตรักเตอร์ของ Dart ค่าเริ่มต้นเป็น false |
detect() → Detected | null
Probe 0x17 แล้ว 0x13 แล้ว 0x11 null แปลว่าไม่มีตาราง live ตอบ
const d = await s.detect();
d?.profile; // "t17.sub8"
d?.method; // "probed" | "generic"
d?.mismatch; // boolean
Detected
| ฟิลด์ | ชนิด | |
|---|---|---|
profile |
string |
id ของโปรไฟล์ |
method |
'probed' | 'generic' |
|
mismatch |
boolean |
มีทั้ง maker hint และผู้ผลิตของโปรไฟล์ และทั้งสองไม่ตรงกัน ไม่เป็นจริงบนโปรไฟล์ generic |
live(options?) → frames
อ่านตารางหลักหนึ่งครั้งต่อเฟรม แล้วตารางรองที่ถึงรอบมากสุดหนึ่งตาราง ไม่จบจนกว่าคุณจะหยุด การเรียกครั้งแรกยังรัน identify() (ใช้ค่าที่จำไว้ถ้าเรียกไปแล้ว)
live(o?: { profile?: string; signal?: AbortSignal; onFrame?: (f: LiveFrame) => void }): AsyncGenerator<LiveFrame>
Stream<LiveFrame> live({String? profile, void Function(LiveFrame f)? onFrame})
| ตัวเลือก | ค่าเริ่มต้น | |
|---|---|---|
profile |
ตรวจเอง | id ที่รู้จักจะข้ามการตรวจ id ที่ไม่รู้จัก throw BAD_PROFILE |
signal |
ไม่มี | เฉพาะ JS เมื่อ abort ลูปจบโดยไม่ throw |
onFrame |
ไม่มี | ถูกเรียกด้วยอ็อบเจกต์เดียวกับที่ yield ก่อน yield |
Throw LIVE_UNSUPPORTED ถ้าตรวจไม่เจออะไร Throw BAD_PROFILE เมื่อ id ไม่รู้จัก หลังลูป state กลับเป็น 'ready' เว้นแต่คุณ close() ไปแล้ว
for await (const f of s.live()) {
const rpm = f.fields.rpm;
if (rpm?.status === 'ok') console.log(rpm.value);
break;
}
await for (final f in s.live()) {
final rpm = f.fields['rpm']!;
if (rpm.ok) print(rpm.value);
break;
}
LiveFrame
| ฟิลด์ | JS | Dart | |
|---|---|---|---|
at |
number (ms) |
DateTime |
นาฬิกาจาก now |
profile |
string |
string |
id ของโปรไฟล์ |
primaryOk |
boolean |
boolean |
false → ฟิลด์ของตารางหลักเป็น absent ในเฟรมนี้ ไม่ใช่ค่าของเฟรมก่อน |
match |
boolean |
boolean |
กติกา match ของโปรไฟล์ยังเข้ากับข้อมูลที่สะสมมา |
engine |
'running' | 'off' | 'unknown' |
String (getter) |
off = หัวฉีด 0 ms และองศาจุดระเบิด 0° ตอนเครื่องดับ รอบกับอุณหภูมิไม่ใช่ค่าจริง (กล่อง Keihin ขึ้นราว 17,500 รอบ และ −30 °C) ควรซ่อน unknown = อ่านสองฟิลด์นี้ไม่ได้ ใช้ core.engineState(fields) / engineState(fields) ได้ด้วย |
fields |
Record<string, Reading> |
Map<String, Reading> |
ทุกฟิลด์ของโปรไฟล์ ทุกเฟรม |
Reading
| ฟิลด์ | ชนิด | |
|---|---|---|
status |
'ok' | 'absent' | 'invalid' |
absent คือไม่มีค่า ไม่ใช่ 0 |
value |
number? / double? |
มีก็ต่อเมื่อ ok |
prov |
'bench' | 'field' | 'reference' | 'provisional' |
ไม่ใช่ status |
source |
string? |
id ของตารางที่ชนะ เฉพาะตอน ok |
Dart: reading.ok คือ status == 'ok'
dtc.read(options?) → DtcResult
dtc.read(o?: { maker?: string }): Promise<DtcResult>
Future<DtcResult> read({String? maker})
อ่านสามครั้ง 72 05 74 01/02/03 ห่างกัน 60 ms maker ทับ hint เฉพาะการเรียกนี้ ต้องอยู่ใน ready
const dtc = await s.dtc.read();
dtc.beta; // true
dtc.outcome; // "faults" | "clean" | "unreadable" | "failed"
DtcResult
| ฟิลด์ | ชนิด | |
|---|---|---|
beta |
true |
เสมอ |
status |
'beta' |
เฉพาะ JS |
outcome |
'faults' | 'clean' | 'unreadable' | 'failed' |
ดู แนวคิด |
reason |
'no_reply' | 'maker_unknown' | 'bad_payload' | null |
เหตุที่ไม่มีโค้ด maker_unknown = กล่องตอบมีข้อมูล แต่ไม่รู้ว่ายี่ห้อไหน เลยไม่ถอดโค้ด ใส่ API key (smongrotApi) หรือ dtc.read({ maker: 'keihin' }) ถ้ารู้ยี่ห้อกล่อง เป็น null เมื่อ faults หรือ clean |
maker |
string | null |
hint ที่ใช้ หรือ null |
codes |
DtcCode[] |
ว่าง เว้นแต่ outcome เป็น faults |
replies |
{ sub: number, hex: string }[] |
ทั้งสาม sub hex เป็นตัวใหญ่คั่นด้วยช่องว่าง "" ถ้าเงียบ Dart เป็นเรคคอร์ด ({int sub, String hex}) |
DtcCode
| ฟิลด์ | ชนิด | |
|---|---|---|
label |
string |
รูปแบบรหัสกระพริบของ Honda เช่น 7-1 |
meaning |
string | null |
ชื่อเซ็นเซอร์ภาษาอังกฤษ และต่อท้าย , signal too low / , signal too high สำหรับรหัส 1, 7, 8, 9, 54 เมื่อ sub เป็น 1 หรือ 2 เป็น null ถ้ารหัสไม่อยู่ในตาราง |
critical |
boolean |
true สำหรับรหัส 12, 33, 52 |
benchConfirmed |
boolean |
true เฉพาะ 7-1 และ 8-1 |
close() → Promise<void>
ตั้ง state เป็น 'closed' และปิด transport ถ้ามี close เรียกจาก finally ได้ เรียกอะไรที่คุยกับ ECU หลังจากนั้นจะ throw CLOSED
await s.close();
Timing
interface Timing {
wakeLowMs: number; wakeIdleMs: number; wakeTries: number;
commandQuietMs: number; commandTimeoutMs: number;
tableQuietMs: number; tableTimeoutMs: number;
ecmIdTries: number; ecmIdGapMs: number; dtcGapMs: number;
liveTickUnitMs: number; liveTargetTickMs: number; liveMinGapMs: number;
}
const DEFAULT_TIMING: Readonly<Timing>;
class Timing {
Timing({int? wakeLowMs, int? wakeIdleMs, int wakeTries = 4, /* same fields */});
}
| ฟิลด์ | ค่าเริ่มต้น |
|---|---|
wakeLowMs |
70 |
wakeIdleMs |
130 |
wakeTries |
4 |
commandQuietMs |
120 |
commandTimeoutMs |
2000 |
tableQuietMs |
120 |
tableTimeoutMs |
500 |
ecmIdTries |
3 |
ecmIdGapMs |
300 |
dtcGapMs |
60 |
liveTickUnitMs |
300 |
liveTargetTickMs |
150 |
liveMinGapMs |
60 |
Dart ไม่มีค่าคงที่ DEFAULT_TIMING Timing() ใส่ค่าเหล่านี้ Timing(wakeTries: 6) ทับหนึ่งฟิลด์
await connect(transport, { timing: { wakeTries: 6 } });
await connect(transport, timing: Timing(wakeTries: 6));
Transport
ทำให้เองเมื่อจะเพิ่มสาย พอร์ตเป็น 10400 8N1 อยู่แล้ว session ตัด echo ให้
interface Transport {
write(bytes: Uint8Array): Promise<void>;
read(timeoutMs: number): Promise<Uint8Array | null>;
flush?(): Promise<void>;
wake?(lowMs: number): Promise<void>;
setBreak?(on: boolean): Promise<void>;
close?(): Promise<void>;
}
abstract class Transport {
Future<void> write(Uint8List bytes);
Future<Uint8List?> read(Duration timeout);
Future<void> flush(); // default: no-op
bool get hasAtomicWake; // default false
Future<void> wake(Duration low); // used only when hasAtomicWake
Future<void> setBreak(bool on); // fallback wake
Future<void> close();
}
| สมาชิก | |
|---|---|
write |
ไบต์ดิบ รวม checksum |
read |
chunk ถัดไปที่รับมา หรือ null เมื่อ timeout JS รับมิลลิวินาที Dart รับ Duration |
flush |
ทิ้งไบต์ที่รับค้าง ถูกเรียกก่อน request ทุกครั้ง |
wake |
ดึงสายลง LOW ตามเวลาที่ให้ จับเวลาบน adapter ทางที่ควรใช้ |
setBreak |
true ดึงลง LOW, false ปล่อย ทางสำรองเมื่อไม่มี wake |
close |
ปล่อยพอร์ต |
Dart เรียก wake ก็ต่อเมื่อ hasAtomicWake เป็นจริง setBreak ค่าเริ่มต้น throw UnsupportedError ไม่ใช่ NO_WAKE JS throw NO_WAKE เมื่อไม่มีทั้งคู่
ChunkQueue
คิว FIFO ที่ adapter ใช้ push(chunk) ไม่สนใจ chunk ว่าง read คืน chunk ถัดไปหรือ null เมื่อครบเวลา clear() ทิ้งของที่ค้าง close() ทำให้การอ่านถัดไปคืน null
const q = new ChunkQueue();
q.push([0x02, 0x04]);
await q.read(50);
final q = ChunkQueue();
q.push([0x02, 0x04]);
await q.read(const Duration(milliseconds: 50));
ข้อผิดพลาด
class SmongrotError extends Error { code: string }
class SmongrotException implements Exception { final String code; final String message; }
Session: ECU_SILENT, BAD_STATE, LIVE_UNSUPPORTED, BAD_PROFILE, CLOSED, NO_WAKE, FRAME_NOT_ALLOWED (SDK ไม่ยอมส่งเฟรมที่อยู่นอกรายการอ่านอย่างเดียว), RESPONSE_TOO_LARGE (อุปกรณ์ส่งข้อมูลเกิน 4 KB ในคำตอบเดียว) Adapter: BAD_PORT, PORT_OPEN, WRITE_FAILED ตัวช่วย API: NO_API_KEY และ NO_FETCH ของ JS รหัส API_* ไปที่ onError อย่างเดียว สาเหตุและวิธีแก้: แก้ปัญหา
try { await connect(transport); }
catch (e) { if (e instanceof SmongrotError && e.code === 'ECU_SILENT') console.error(e.message); }
try { await connect(transport); }
on SmongrotException catch (e) { if (e.code == 'ECU_SILENT') print(e.message); }
API ชื่อรุ่น
smongrotApi / SmongrotApi
smongrotApi(opts: {
apiKey: string;
baseUrl?: string; // default DEFAULT_API_BASE
fetch?: typeof fetch;
timeoutMs?: number; // default 5000
onError?: (e: SmongrotError) => void;
}): { baseUrl: string; lookupModel(ecmId: string): Promise<ModelInfo | null> }
class SmongrotApi {
SmongrotApi({
required String apiKey,
String baseUrl = defaultApiBase,
http.Client? client,
Duration timeout = const Duration(seconds: 5),
void Function(SmongrotException e)? onError,
});
final String baseUrl;
Future<ModelInfo?> lookupModel(String ecmId);
void close();
}
SmongrotApi smongrotApi({required String apiKey, String baseUrl = defaultApiBase, http.Client? client,
Duration timeout = const Duration(seconds: 5), void Function(SmongrotException e)? onError});
DEFAULT_API_BASE / defaultApiBase คือ https://api.smongrot.com/v1 เครื่องหมาย / ท้ายถูกตัดออก
lookupModel ไม่ throw 404 (ไม่รู้จัก ECU) คืน null และไม่เรียก onError ความล้มเหลวอื่นเรียก onError แล้วคืน null id ต้องเป็นเลขฐานสิบหก 10 ตัว ไม่งั้น onError ได้ API_BAD_INPUT
Throw NO_API_KEY เมื่อ apiKey ว่าง JS ยัง throw NO_FETCH เมื่อไม่มี fetch
const api = smongrotApi({ apiKey: process.env.SMONGROT_API_KEY, onError: (e) => console.warn(e.code) });
const s = await connect(transport, { lookupModel: api.lookupModel });
final api = SmongrotApi(apiKey: myKey, onError: (e) => print(e.code));
final s = await connect(transport, lookupModel: api.lookupModel);
api.close();
modelInfoFromApi(data) → ModelInfo | null
แปลงอ็อบเจกต์ data ของ API (ecm_id, maker, model, ecu_code, live_profile, verified) คืน null เมื่อ model, ecu_code และ maker ใช้ไม่ได้สักตัว maker ถูกทำเป็นตัวเล็ก และเก็บไว้ก็ต่อเมื่อเป็น keihin หรือ shindengen
modelInfoFromApi({ ecm_id: '0101880D01', maker: 'Keihin', model: 'เวฟ 110i', ecu_code: '38770-K03-H01', verified: true });
modelInfoFromApi({'ecm_id': '0101880D01', 'maker': 'Keihin', 'model': 'เวฟ 110i', 'ecu_code': '38770-K03-H01', 'verified': true});
Adapter สาย (JavaScript)
openWebSerial(port) → Promise<Transport>
openWebSerial(port: SerialPort): Promise<Transport>
เปิดพอร์ต Web Serial ที่ 10400 8N1 ไม่มี flow control port มาจาก navigator.serial.requestPort() และต้องยังไม่ถูกเปิด wake คือ setBreak นาน 70 ms close() ยกเลิก reader แล้วปิดพอร์ต ความล้มเหลวของเบราว์เซอร์ reject ด้วย DOMException ไม่ใช่ SmongrotError
const s = await connect(await openWebSerial(await navigator.serial.requestPort()));
fromSerialPort(port, opts?) → Promise<Transport>
fromSerialPort(port: SerialPort, o?: { wake?: (lowMs: number) => Promise<void>; closeStream?: boolean }): Promise<Transport>
ห่อ SerialPort ของ serialport เปิดให้ถ้ายังไม่เปิด Reject BAD_PORT เมื่อค่าไม่ใช่ SerialPort, baud ถูกตั้งและไม่ใช่ 10400 หรือเฟรมไม่ใช่ 8N1 wake คือ port.set({ brk }) นาน 70 ms เว้นแต่ส่ง wake closeStream ค่าเริ่มต้นคือปิดพอร์ตตอน close()
serialport เป็น optional peer ติดตั้งเอง แพ็กเกจนี้ไม่ import มัน
import { SerialPort } from 'serialport';
const port = new SerialPort({ path: '/dev/cu.usbserial-XXXX', baudRate: 10400, autoOpen: false });
const s = await connect(await fromSerialPort(port));
fromDuplex(stream, opts?) → Transport
fromDuplex(stream: Duplex, o?: {
setBreak?: (on: boolean) => Promise<void>;
wake?: (lowMs: number) => Promise<void>;
closeStream?: boolean; // default true (stream.destroy() on close)
}): Transport
Duplex ของ Node ที่เป็น 10400 8N1 อยู่แล้ว ไม่ตรวจ baud ถ้าไม่มี wake หรือ setBreak connect จะ throw NO_WAKE write reject ด้วยข้อผิดพลาดของสตรีม ไม่ใช่ SmongrotError
const t = fromDuplex(stream, { setBreak: (on) => new Promise((ok, no) => port.set({ brk: on }, (e) => e ? no(e) : ok())) });
Adapter สาย (Dart)
เผยแพร่แยก เพื่อให้ core เป็น Dart ล้วน ทั้งคู่ตั้ง 10400 8N1 และไม่มี flow control
LibSerialPortTransport (smongrot_sdk_libserialport)
เดสก์ท็อป Windows, macOS, Linux ผ่าน flutter_libserialport re-export SerialPort
| สมาชิก | |
|---|---|
LibSerialPortTransport.open(name) |
เปิดอ่านเขียน ตั้ง 10400 8N1 Throw PORT_OPEN |
LibSerialPortTransport.fromOpenPort(port) |
ห่อพอร์ตที่เปิดไว้แล้ว ตั้ง 10400 8N1 |
LibSerialPortTransport.configureKLine(port) |
baud 10400, 8 data bits, ไม่มี parity, 1 stop bit, ไม่มี flow control |
setBreak(on) |
break ของระบบปฏิบัติการ Throw NO_WAKE ถ้าไดรเวอร์ปฏิเสธ |
write |
throw WRITE_FAILED ถ้าไบต์ไม่ถูกยอมรับครบ (timeout การเขียน 1000 ms) |
wake เป็น break 70 ms ไม่มี hasAtomicWake
import 'package:smongrot_sdk_libserialport/smongrot_sdk_libserialport.dart';
print(SerialPort.availablePorts);
final s = await connect(LibSerialPortTransport.open('/dev/cu.usbserial-XXXX'));
UsbSerialTransport (smongrot_sdk_usb_serial)
Android USB-OTG ผ่าน usb_serial re-export UsbSerial, UsbPort, UsbDevice
static Future<UsbSerialTransport> open(UsbPort port, {Future<void> Function(Duration low)? atomicWake})
Throw PORT_OPEN ถ้า port.open() คืน false ตั้ง DTR และ RTS ให้ต่ำ hasAtomicWake เป็นจริง: wake เรียก atomicWake เมื่อคุณส่งมา ไม่งั้นใช้ emulatedWake (LOW ประมาณ 45 ms)
import 'package:smongrot_sdk_usb_serial/smongrot_sdk_usb_serial.dart';
final devices = await UsbSerial.listDevices();
final port = await devices.first.create();
final s = await connect(await UsbSerialTransport.open(port!));
emulatedWake / emulatedWakeBaud / kLineBaud
อยู่ใน core ของ Dart สำหรับคนที่เขียน adapter เอง
kLineBaud |
10400 |
emulatedWakeBaud(lowMs) |
round(9000 / lowMs) บีบไว้ที่ 200..600 ที่ 70 ms ได้ 129 แล้วถูกบีบเป็น 200 |
emulatedWake(lowMs, {setBaud, write, sleep}) |
สลับไป baud นั้น เขียน 0x00 หนึ่งไบต์ รอ ceil(10000 / baud) + 60 ms แล้วกลับ 10400 ไดรเวอร์ที่ปฏิเสธ baud ต่ำจะข้ามพัลส์ สายถูกคืนเป็น 10400 เสมอ |
await emulatedWake(
70,
setBaud: (baud) async { /* switch your chip, including back to 10400 */ },
write: (bytes) async { /* write these bytes */ },
);
setBaud คือ Future<void> Function(int baud) write คือ Future<void> Function(Uint8List bytes) sleep ค่าเริ่มต้นคือ Future.delayed
ตัวแทนสำหรับเทส
FakeEcuTransport
ECU ที่ตอบตามสคริปต์ read ไม่รอตัวจับเวลา: ได้ chunk ถัดไป หรือ null ทันที ส่ง sleep: async () => {} (JS) หรือ sleep: (_) async {} (Dart) ให้ connect เพื่อไม่ให้เทสรอ 70 ms กับ 130 ms
new FakeEcuTransport(o?: {
replies?: Record<string, string | null | ((tx: number[]) => string | null)>;
raw?: Record<string, string>;
bench?: boolean; // default true
echo?: boolean; // default true
chunkSize?: number; // default 0 = one chunk
})
FakeEcuTransport({
Map<String, String?> replies = const {},
Map<String, String> raw = const {},
bool bench = true,
bool echo = true,
int chunkSize = 0,
})
replies |
hex ของคำขอ → hex ของ reply ที่ส่งหลัง echo null คือเงียบ ค่าของ JS เป็นฟังก์ชันของไบต์ tx ได้ ค่าของ Dart เป็นสตริงอย่างเดียว |
raw |
hex ของคำขอ → สิ่งที่อ่านกลับตรง ๆ ไม่ผ่านกติกา echo |
bench |
โหลด BENCH_SESSION / benchSession (StartComm, liveness, StartDiag, ECM-ID) identify() จะได้ 0104780D01 |
writes |
hex ของทุกเฟรมที่ถูกเขียน |
badChecksumWrites |
ส่วนย่อยที่ checksum ผิด |
wakes |
JS: number[] ของ low-ms Dart: List<Duration> |
ตัวปลอมที่มีแค่ bench ไม่มีตาราง live live() throw LIVE_UNSUPPORTED จนกว่า replies จะมีหนึ่งตาราง reply ที่ควิกสตาร์ตใช้ถอด rpm ได้ 3566
const s = await connect(new FakeEcuTransport(), { sleep: async () => {} });
console.log((await s.identify()).ecmId); // 0104780D01
final s = await connect(FakeEcuTransport(), sleep: (_) async {});
print((await s.identify()).ecmId);
SPEC / spec
สเปก level 1 ที่ฝังมา: kline, live, dtc ข้อความที่เป็นเกณฑ์คือ JSON ที่เผยแพร่คู่กับเอกสารนี้ ใน JavaScript SPEC ก็คือ core.SPEC
SPEC.kline.physical.baud; // 10400
spec['kline']['physical']['baud']; // 10400
ตัวช่วยโปรโตคอล (core.* / ระดับบนของ Dart)
ฟังก์ชันบริสุทธิ์ ไม่มี I/O กับรถจริงให้ใช้ connect ใช้ชุดนี้เมื่อจะทดสอบเฟรม หรือสร้างเครื่องมือบนกติกาเดียวกัน
fromHex(s) → bytes
'72 07 72 17 00 0F' → [0x72, 0x07, 0x72, 0x17, 0x00, 0x0F] มีแต่ช่องว่าง → []
core.fromHex('72 05 71 00 18');
fromHex('72 05 71 00 18');
toHex(bytes) → string
ตัวใหญ่ คั่นด้วยช่องว่าง toHex([0x72, 0x07]) → '72 07'
checksum(body) → number
(0x100 − (sum mod 256)) mod 256 มาสก์เหลือ 8 บิต
core.checksum(core.fromHex('72 07 72 17 00 0F')); // 0xEF
isValidFrame(frame) → boolean
จริงเมื่อเฟรมไม่ว่าง และผลรวมของไบต์เป็น 0 mod 256
validReplyLength(raw, p) → number | null
ความยาว L ของเฟรมที่ถูกต้องซึ่งเริ่มที่ p หรือ null L = raw[p+1], L >= 4, เฟรมอยู่ในบัฟเฟอร์ และผลรวมของไบต์เป็น 0 mod 256
peelEcho(raw, tx) → { echo, distorted, reply }
ตัด echo ของการอ่านกลับหนึ่งครั้ง คืน echo, บอกว่า echo เพี้ยนหรือไม่ และไบต์ของ reply
core.peelEcho(core.fromHex('72 05 00 F0 99 02 04 00 FA'), core.fromHex('72 05 00 F0 99'));
// { echo: true, distorted: false, reply: [0x02, 0x04, 0x00, 0xFA] }
ecmIdFrom(raw) → string | null
เลขฐานสิบหกตัวใหญ่ 10 ตัวจาก reply หรือ null เลือก 02 0B 72 00 00 ก่อน ไม่งั้น 02 0F 71 00
extractData(reply, request, headerLen, dataLen) → bytes | null
ไบต์ข้อมูลของเฟรม 02 L <service> <table> อันสุดท้ายที่ checksum ถูก เป็น null เมื่อไม่มีเฟรม หรือสั้นกว่า dataLen
normalizeEcmId(s) → string
ตัดช่องว่างแล้วทำเป็นตัวใหญ่ '01 04780d01' → '0104780D01'
profiles() / profiles
JS: core.profiles() คืนอาเรย์ของโปรไฟล์ Dart: profiles เป็น getter ของ List<Profile> (Profile คือ Map<String, dynamic>)
profileById(id) → profile | null
อ็อบเจกต์โปรไฟล์ หรือ null JS: core.profileById('t17.sub8')
tableDef(profile, id) / primaryTable(profile)
เฉพาะ Dart อ็อบเจกต์ตารางในโปรไฟล์ และตารางหลัก JS อ่าน profile.tables[id] กับ profile.tables[profile.primary] จากอ็อบเจกต์ของ profileById
profileMatches(profile, data, matchV2?) → boolean
data คือ { tableId: bytes } JS: core.profileMatches(p, data, false) Dart: profileMatches(p, data, matchV2: false)
decodeLive(profile, data, { ecmId?, matchV2? }) → fields
รูปร่างเดียวกับ LiveFrame.fields
const p = core.profileById('generic.t17');
core.decodeLive(p, { t17: core.fromHex('0D EE 00 00 FF FF FF FF FF FF 00 00 00 00 00') });
makerForPartCode(partCode) → 'keihin' | 'shindengen' | null
38770-… → keihin 30400-… → shindengen ที่เหลือ รวม null → null
core.makerForPartCode('38770-K03-H01'); // "keihin"
probeTables() → { table, def }[]
ลำดับการ probe table คือไบต์ 23, 19, 17 (0x17, 0x13, 0x11) def คืออ็อบเจกต์ตารางหลักของโปรไฟล์นั้น (request, reply_header_len, data_len)
chooseProfile(table, data, { makerHint?, matchV2? }) → detection | null
JS คืน { profile, method, mismatch } โดย profile เป็นอ็อบเจกต์โปรไฟล์ หรือ null Dart คืน Detected? โดย profile เป็นสตริง id session.detect() คืน id ทั้งสองภาษา table คือไบต์ (23, 19 หรือ 17) data คือไบต์ของตารางหลัก
const c = core.chooseProfile(0x17, bytes, { makerHint: 'keihin' });
c?.profile.id;
final c = chooseProfile(0x17, bytes, makerHint: 'keihin');
c?.profile; // the id
dtcPayloadOf(reply, sub?) → bytes | null
payload ของเฟรม 02 … 74 ที่ถูกต้องอันสุดท้าย ถ้าส่ง sub ต้องตรงกับไบต์ sub ของ reply
decodeDtcPayload(payload) → string[] | null
ป้าย หรือ null เมื่อ payload อ่านไม่ได้ ลิสต์ว่างแปลว่า payload นี้ไม่มีรหัส
decodeDtcReplies(replies, subs?) → string[] | null
ยูเนียนของป้าย หรือ null ถ้า reply ใดอ่านไม่ได้ เมื่อส่ง subs ต้องมีหนึ่งค่าต่อหนึ่ง reply
describeDtc(label) → meaning | null
JS คืน { meaning, critical, benchConfirmed } หรือ null เมื่อรหัสไม่อยู่ในตาราง Dart คืน DtcCode ที่ meaning เป็น null ในกรณีนั้น (ป้ายยังอยู่)
core.describeDtc('7-1');
// { meaning: "engine temperature sensor, signal too low", critical: false, benchConfirmed: true }
describeDtc('7-1').meaning;
ถัดไป: ระบุตัวกล่องและ API key