@@ -24,6 +24,19 @@ interface CANFrame {
24
24
ts : number
25
25
}
26
26
27
+ let txCnt = 0
28
+ const pendingBaseCmds = new Map <
29
+ number ,
30
+ {
31
+ resolve : ( value : number ) => void
32
+ reject : ( reason : CanError ) => void
33
+ msgType : CanMsgType
34
+ id : number
35
+ data : Buffer
36
+ extra ?: { database ?: string ; name ?: string }
37
+ }
38
+ > ( )
39
+
27
40
export class TOOMOSS_CAN extends CanBase {
28
41
// 添加静态设备管理Map
29
42
static deviceHandles = new Map < number , {
@@ -38,23 +51,16 @@ export class TOOMOSS_CAN extends CanBase {
38
51
deviceIndex : number
39
52
closed = false
40
53
cnt = 0
54
+
41
55
id : string
42
56
log : CanLOG
43
57
canConfig : any
44
58
canfdConfig : any
45
59
startTime = getTsUs ( )
46
60
tsOffset : number | undefined
61
+
47
62
private readAbort = new AbortController ( )
48
- pendingBaseCmds = new Map <
49
- string ,
50
- {
51
- resolve : ( value : number ) => void
52
- reject : ( reason : CanError ) => void
53
- msgType : CanMsgType
54
- data : Buffer
55
- extra ?: { database ?: string ; name ?: string }
56
- } [ ]
57
- > ( )
63
+
58
64
59
65
rejectBaseMap = new Map <
60
66
number ,
@@ -64,7 +70,7 @@ export class TOOMOSS_CAN extends CanBase {
64
70
}
65
71
> ( )
66
72
67
- constructor ( info : CanBaseInfo , enableRes = false ) {
73
+ constructor ( info : CanBaseInfo , enableRes = false ) {
68
74
super ( )
69
75
this . id = info . id
70
76
this . info = info
@@ -154,7 +160,7 @@ export class TOOMOSS_CAN extends CanBase {
154
160
this . canfdConfig . Mode = 0 // 正常模式
155
161
this . canfdConfig . ISOCRCEnable = 1
156
162
this . canfdConfig . RetrySend = 0
157
- this . canfdConfig . ResEnable = enableRes ? 1 : 0
163
+ this . canfdConfig . ResEnable = enableRes ? 1 : 0
158
164
// 仲裁域波特率配置
159
165
this . canfdConfig . NBT_BRP = info . bitrate . preScaler
160
166
this . canfdConfig . NBT_SEG1 = info . bitrate . timeSeg1
@@ -174,7 +180,7 @@ export class TOOMOSS_CAN extends CanBase {
174
180
TOOMOSS . CANFD_ResetStartTime ( this . handle , this . deviceIndex )
175
181
176
182
177
- // // 启动CANFD
183
+ // 启动CANFD
178
184
ret = TOOMOSS . CANFD_StartGetMsg ( this . handle , this . deviceIndex )
179
185
if ( ret != 0 ) {
180
186
throw new Error ( 'Start CANFD failed' )
@@ -187,7 +193,7 @@ export class TOOMOSS_CAN extends CanBase {
187
193
this . canConfig . CAN_SJW = info . bitrate . sjw
188
194
this . canConfig . CAN_BS1 = info . bitrate . timeSeg1
189
195
this . canConfig . CAN_BS2 = info . bitrate . timeSeg2
190
- this . canConfig . CAN_Mode = enableRes ? ( 0x80 | 0 ) : 0 // 正常模式
196
+ this . canConfig . CAN_Mode = enableRes ? ( 0x80 | 0 ) : 0 // 正常模式
191
197
this . canConfig . CAN_ABOM = 0 // 自动离线恢复
192
198
this . canConfig . CAN_NART = 1 // 自动重传
193
199
this . canConfig . CAN_RFLM = 0 // 接收FIFO锁定模式
@@ -207,24 +213,64 @@ export class TOOMOSS_CAN extends CanBase {
207
213
TOOMOSS . CAN_ResetStartTime ( this . handle , this . deviceIndex )
208
214
}
209
215
216
+ // 绑定回调函数到实例
217
+
210
218
211
- // 启动消息接收
219
+ // 初始化 TSFN
212
220
TOOMOSS . CreateTSFN (
213
221
this . handle ,
214
222
this . deviceIndex ,
215
- info . canfd ,
223
+ this . info . canfd ,
216
224
this . id ,
217
225
this . callback . bind ( this ) ,
218
- this . id + 'error' ,
219
- this . callbackError . bind ( this )
226
+ 'error-' + this . id ,
227
+ this . callbackError . bind ( this ) ,
228
+ 'tx-' + this . id ,
229
+ this . txCallback . bind ( this )
230
+
220
231
)
221
232
}
233
+ txCallback ( obj : any ) {
234
+ const { id, timestamp, result } = obj
235
+
236
+
237
+
238
+ // 更新时间戳偏移
239
+ if ( this . tsOffset == undefined ) {
240
+ this . tsOffset = timestamp * 10 - ( getTsUs ( ) - this . startTime )
241
+ }
242
+ const ts = timestamp * 10 - this . tsOffset
243
+
244
+ // 从待发送队列中找到对应的消息
245
+ const cmdId = Number ( id )
246
+
247
+ const pendingCmds = pendingBaseCmds . get ( cmdId )
248
+
249
+
250
+ if ( pendingCmds ) {
251
+
252
+ const message : CanMessage = {
253
+ device : this . info . name ,
254
+ dir : 'OUT' ,
255
+ id : pendingCmds . id ,
256
+ data : pendingCmds . data ,
257
+ ts : ts ,
258
+ msgType : pendingCmds . msgType ,
259
+ database : pendingCmds . extra ?. database ,
260
+ name : pendingCmds . extra ?. name
261
+ }
262
+ this . log . canBase ( message )
263
+ pendingCmds . resolve ( ts )
264
+ pendingBaseCmds . delete ( cmdId )
222
265
266
+ }
267
+ }
223
268
callback ( msg : any ) {
224
269
if ( msg . id == 0xffffffff ) {
225
270
return
226
271
}
227
272
273
+
228
274
const frame : CANFrame = {
229
275
canId : msg . ID & 0x1fffffff ,
230
276
msgType : {
@@ -236,7 +282,7 @@ export class TOOMOSS_CAN extends CanBase {
236
282
data : msg . Data ,
237
283
ts : ( ( msg . TimeStampHigh << 32 ) | msg . TimeStamp ) * 10
238
284
}
239
-
285
+
240
286
if ( this . info . canfd ) {
241
287
// CANFD消息处理
242
288
frame . msgType . brs = ( msg . Flags & 0x01 ) ? true : false
@@ -257,9 +303,11 @@ export class TOOMOSS_CAN extends CanBase {
257
303
}
258
304
259
305
callbackError ( err : any ) {
306
+ console . log ( 'callbackError' , err )
260
307
this . log . error ( getTsUs ( ) - this . startTime , 'bus error' )
261
308
this . close ( true )
262
309
}
310
+
263
311
setOption ( cmd : string , val : any ) : void {
264
312
null
265
313
}
@@ -268,7 +316,7 @@ export class TOOMOSS_CAN extends CanBase {
268
316
this . tsOffset = frame . ts - ( getTsUs ( ) - this . startTime )
269
317
}
270
318
const ts = frame . ts - this . tsOffset
271
-
319
+
272
320
const cmdId = this . getReadBaseId ( frame . canId , frame . msgType )
273
321
const message : CanMessage = {
274
322
device : this . info . name ,
@@ -319,18 +367,18 @@ export class TOOMOSS_CAN extends CanBase {
319
367
320
368
close ( isReset = false , msg ?: string ) {
321
369
this . readAbort . abort ( )
322
-
370
+
323
371
for ( const [ key , value ] of this . rejectBaseMap ) {
324
372
value . reject ( new CanError ( CAN_ERROR_ID . CAN_BUS_CLOSED , value . msgType ) )
325
373
}
374
+
326
375
this . rejectBaseMap . clear ( )
327
376
328
- for ( const [ key , vals ] of this . pendingBaseCmds ) {
329
- for ( const val of vals ) {
330
- val . reject ( new CanError ( CAN_ERROR_ID . CAN_BUS_CLOSED , val . msgType ) )
331
- }
377
+ for ( const [ key , val ] of pendingBaseCmds ) {
378
+ val . reject ( new CanError ( CAN_ERROR_ID . CAN_BUS_CLOSED , val . msgType ) )
332
379
}
333
- this . pendingBaseCmds . clear ( )
380
+ pendingBaseCmds . clear ( )
381
+
334
382
335
383
if ( isReset ) {
336
384
if ( this . info . canfd ) {
@@ -346,7 +394,7 @@ export class TOOMOSS_CAN extends CanBase {
346
394
}
347
395
return
348
396
} else {
349
-
397
+
350
398
this . closed = true
351
399
this . log . close ( )
352
400
TOOMOSS . FreeTSFN ( this . id )
@@ -376,42 +424,37 @@ export class TOOMOSS_CAN extends CanBase {
376
424
377
425
writeBase ( id : number , msgType : CanMsgType , data : Buffer , extra ?: { database ?: string ; name ?: string } ) : Promise < number > {
378
426
return new Promise ( ( resolve , reject ) => {
379
- const maxLen = msgType . canfd ? 64 : 8
380
- if ( data . length > maxLen ) {
381
- reject ( new CanError ( CAN_ERROR_ID . CAN_PARAM_ERROR , msgType , data ) )
382
- return
383
- }
384
- console . log ( 'send start' )
385
- TOOMOSS . SendCANMsg ( this . handle , this . deviceIndex , this . info . canfd , {
386
- ID : id | ( msgType . idType == CAN_ID_TYPE . EXTENDED ? 0x80000000 : 0 ) | ( msgType . remote ? 0x40000000 : 0 ) ,
387
- RemoteFlag : msgType . remote ? 1 : 0 ,
388
- ExternFlag : msgType . idType == CAN_ID_TYPE . EXTENDED ? 1 : 0 ,
389
- DataLen : data . length ,
390
- Data : data ,
391
- DLC : data . length ,
392
- Flags : ( msgType . brs ? 0x01 : 0 ) | ( msgType . canfd ? 0x04 : 0 )
393
- } ) . then ( ( timestamp : number ) => {
394
- console . log ( 'send ok' )
395
- if ( this . tsOffset == undefined ) {
396
- this . tsOffset = timestamp * 10 - ( getTsUs ( ) - this . startTime )
397
- }
398
- const ts = timestamp * 10 - this . tsOffset
399
- const message : CanMessage = {
400
- device : this . info . name ,
401
- dir : 'OUT' ,
402
- id : id ,
403
- data : data ,
404
- ts : ts , // 转换为微秒
405
- msgType : msgType ,
406
- database : extra ?. database ,
407
- name : extra ?. name
408
- }
409
- this . log . canBase ( message )
410
- resolve ( ts )
411
- } ) . catch ( ( err : any ) => {
412
- console . log ( 'send error' , err )
427
+ const cmdId = txCnt ++
428
+
429
+
430
+
431
+ try {
432
+ pendingBaseCmds . set ( cmdId , { resolve, reject, msgType, id, data, extra } )
433
+ TOOMOSS . SendCANMsg (
434
+ this . handle ,
435
+ this . deviceIndex ,
436
+ this . info . canfd ,
437
+ this . id ,
438
+
439
+ cmdId ,
440
+ {
441
+ ID : id | ( msgType . idType == CAN_ID_TYPE . EXTENDED ? 0x80000000 : 0 ) | ( msgType . remote ? 0x40000000 : 0 ) ,
442
+ RemoteFlag : msgType . remote ? 1 : 0 ,
443
+ ExternFlag : msgType . idType == CAN_ID_TYPE . EXTENDED ? 1 : 0 ,
444
+ DataLen : data . length ,
445
+ Data : data ,
446
+ DLC : data . length ,
447
+ Flags : ( msgType . brs ? 0x01 : 0 ) | ( msgType . canfd ? 0x04 : 0 )
448
+ }
449
+ )
450
+
451
+
452
+
453
+
454
+ } catch ( err : any ) {
455
+ pendingBaseCmds . delete ( cmdId )
413
456
reject ( new CanError ( CAN_ERROR_ID . CAN_INTERNAL_ERROR , msgType , data , err ) )
414
- } )
457
+ }
415
458
} )
416
459
}
417
460
0 commit comments