Skip to content

Commit b7562ce

Browse files
committed
need seperate toomoss
1 parent 4cd179b commit b7562ce

File tree

4 files changed

+200
-25
lines changed

4 files changed

+200
-25
lines changed

src/main/docan/toomoss/index.ts

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ export class TOOMOSS_CAN extends CanBase {
6464
}
6565
>()
6666

67-
constructor(info: CanBaseInfo) {
67+
constructor(info: CanBaseInfo,enableRes=false) {
6868
super()
6969
this.id = info.id
7070
this.info = info
@@ -154,7 +154,7 @@ export class TOOMOSS_CAN extends CanBase {
154154
this.canfdConfig.Mode = 0 // 正常模式
155155
this.canfdConfig.ISOCRCEnable = 1
156156
this.canfdConfig.RetrySend = 0
157-
this.canfdConfig.ResEnable = 1
157+
this.canfdConfig.ResEnable = enableRes?1:0
158158
// 仲裁域波特率配置
159159
this.canfdConfig.NBT_BRP = info.bitrate.preScaler
160160
this.canfdConfig.NBT_SEG1 = info.bitrate.timeSeg1
@@ -175,10 +175,10 @@ export class TOOMOSS_CAN extends CanBase {
175175

176176

177177
// // 启动CANFD
178-
// ret = TOOMOSS.CANFD_StartGetMsg(this.handle, this.deviceIndex)
179-
// if (ret != 0) {
180-
// throw new Error('Start CANFD failed')
181-
// }
178+
ret = TOOMOSS.CANFD_StartGetMsg(this.handle, this.deviceIndex)
179+
if (ret != 0) {
180+
throw new Error('Start CANFD failed')
181+
}
182182

183183
} else {
184184
// 普通CAN配置
@@ -187,10 +187,11 @@ export class TOOMOSS_CAN extends CanBase {
187187
this.canConfig.CAN_SJW = info.bitrate.sjw
188188
this.canConfig.CAN_BS1 = info.bitrate.timeSeg1
189189
this.canConfig.CAN_BS2 = info.bitrate.timeSeg2
190-
this.canConfig.CAN_Mode = 0 // 正常模式
190+
this.canConfig.CAN_Mode = enableRes?(0x80|0):0 // 正常模式
191191
this.canConfig.CAN_ABOM = 0 // 自动离线恢复
192192
this.canConfig.CAN_NART = 1 // 自动重传
193193
this.canConfig.CAN_RFLM = 0 // 接收FIFO锁定模式
194+
194195
this.canConfig.CAN_TXFP = 0 // 发送FIFO优先级
195196

196197
ret = TOOMOSS.CAN_Init(this.handle, this.deviceIndex, this.canConfig)
@@ -199,10 +200,10 @@ export class TOOMOSS_CAN extends CanBase {
199200
}
200201

201202
// 启动CAN
202-
// ret = TOOMOSS.CAN_StartGetMsg(this.handle, this.deviceIndex)
203-
// if (ret != 0) {
204-
// throw new Error('Start CAN failed')
205-
// }
203+
ret = TOOMOSS.CAN_StartGetMsg(this.handle, this.deviceIndex)
204+
if (ret != 0) {
205+
throw new Error('Start CAN failed')
206+
}
206207
TOOMOSS.CAN_ResetStartTime(this.handle, this.deviceIndex)
207208
}
208209

@@ -223,6 +224,7 @@ export class TOOMOSS_CAN extends CanBase {
223224
if (msg.id == 0xffffffff) {
224225
return
225226
}
227+
226228
const frame: CANFrame = {
227229
canId: msg.ID & 0x1fffffff,
228230
msgType: {
@@ -334,12 +336,13 @@ export class TOOMOSS_CAN extends CanBase {
334336
if (this.info.canfd) {
335337
TOOMOSS.CANFD_ClearMsg(this.handle, this.deviceIndex)
336338
TOOMOSS.CANFD_Stop(this.handle, this.deviceIndex)
337-
338339
TOOMOSS.CANFD_Init(this.handle, this.deviceIndex, this.canfdConfig)
340+
TOOMOSS.CANFD_StartGetMsg(this.handle, this.deviceIndex)
339341
} else {
340342
TOOMOSS.CAN_ClearMsg(this.handle, this.deviceIndex)
341343
TOOMOSS.CAN_Stop(this.handle, this.deviceIndex)
342344
TOOMOSS.CAN_Init(this.handle, this.deviceIndex, this.canConfig)
345+
TOOMOSS.CAN_StartGetMsg(this.handle, this.deviceIndex)
343346
}
344347
return
345348
} else {
@@ -378,7 +381,7 @@ export class TOOMOSS_CAN extends CanBase {
378381
reject(new CanError(CAN_ERROR_ID.CAN_PARAM_ERROR, msgType, data))
379382
return
380383
}
381-
384+
console.log('send start')
382385
TOOMOSS.SendCANMsg(this.handle, this.deviceIndex, this.info.canfd, {
383386
ID: id | (msgType.idType == CAN_ID_TYPE.EXTENDED ? 0x80000000 : 0) | (msgType.remote ? 0x40000000 : 0),
384387
RemoteFlag: msgType.remote ? 1 : 0,
@@ -388,6 +391,7 @@ export class TOOMOSS_CAN extends CanBase {
388391
DLC: data.length,
389392
Flags: (msgType.brs ? 0x01 : 0) | (msgType.canfd ? 0x04 : 0)
390393
}).then((timestamp: number) => {
394+
console.log('send ok')
391395
if (this.tsOffset == undefined) {
392396
this.tsOffset = timestamp*10 - (getTsUs() - this.startTime)
393397
}
@@ -405,6 +409,7 @@ export class TOOMOSS_CAN extends CanBase {
405409
this.log.canBase(message)
406410
resolve(ts)
407411
}).catch((err: any) => {
412+
console.log('send error',err)
408413
reject(new CanError(CAN_ERROR_ID.CAN_INTERNAL_ERROR, msgType, data, err))
409414
})
410415
})

src/main/docan/toomoss/swig/tsfn.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ void threadEntry(TsfnContext *context) {
227227

228228

229229
// 添加短暂延时以降低CPU占用
230-
std::this_thread::sleep_for(std::chrono::milliseconds(1));
230+
// std::this_thread::sleep_for(std::chrono::milliseconds(1));
231231
}
232232
}
233233

test/docan/cantp.test.ts

Lines changed: 90 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ import {
1414
import { CAN_TP, CAN_TP_SOCKET, CanTp } from 'src/main/docan/cantp'
1515
import { ZLG_CAN } from 'src/main/docan/zlg'
1616
import { KVASER_CAN } from 'src/main/docan/kvaser'
17+
import { TOOMOSS_CAN } from 'src/main/docan/toomoss'
1718

1819

1920
const dllPath = path.join(__dirname, '../../resources/lib')
2021
PEAK_TP.loadDllPath(dllPath)
2122
ZLG_CAN.loadDllPath(dllPath)
2223
KVASER_CAN.loadDllPath(dllPath)
24+
TOOMOSS_CAN.loadDllPath(dllPath)
2325
function createIncrementArray(length: number, start = 0) {
2426
const array: number[] = []
2527
for (let i = 0; i < length; i++) {
@@ -207,8 +209,8 @@ const addrList = [
207209
]
208210
const dataList = [
209211
// Buffer.from([1]),
210-
Buffer.from(createIncrementArray(5)),
211-
// Buffer.from(createIncrementArray(6)),
212+
// Buffer.from(createIncrementArray(5)),
213+
Buffer.from(createIncrementArray(6)),
212214
// Buffer.from(createIncrementArray(7)),
213215
// Buffer.from(createIncrementArray(8)),
214216
// Buffer.from(createIncrementArray(60)),
@@ -218,10 +220,11 @@ const dataList = [
218220
// Buffer.from(createIncrementArray(1064)),
219221
]
220222

221-
describe('peak tp', () => {
223+
describe('peak cantp', () => {
222224
let client!: CanTp
223225
let server!: CanTp
224226
beforeAll(() => {
227+
225228
server = new CAN_TP(
226229
new PEAK_TP({
227230
handle: 81,
@@ -290,10 +293,11 @@ describe('peak tp', () => {
290293
})
291294
})
292295

293-
describe('zlg tp', () => {
296+
describe('zlg cantp', () => {
294297
let client!: CanTp
295298
let server!: CanTp
296299
beforeAll(() => {
300+
297301
server = new CAN_TP(
298302
new ZLG_CAN({
299303
handle: '41_0_0',
@@ -361,10 +365,11 @@ describe('zlg tp', () => {
361365
})
362366
})
363367

364-
describe('kvaser tp', () => {
368+
describe('kvaser cantp', () => {
365369
let client!: CanTp
366370
let server!: CanTp
367371
beforeAll(() => {
372+
368373
server = new CAN_TP(
369374
new KVASER_CAN({
370375
handle: 0,
@@ -434,4 +439,83 @@ describe('kvaser tp', () => {
434439
// client.close()
435440
// server.close()
436441
})
437-
})
442+
})
443+
444+
describe('toomoss cantp', () => {
445+
let client!: CanTp
446+
let server!: CanTp
447+
beforeAll(() => {
448+
449+
server = new CAN_TP(
450+
new TOOMOSS_CAN({
451+
handle: '1417675180:0',
452+
name: 'server',
453+
vendor: 'toomoss',
454+
canfd: true,
455+
456+
bitrate: {
457+
freq: 500000,
458+
preScaler: 1,
459+
timeSeg1: 68,
460+
timeSeg2: 11,
461+
sjw: 11
462+
},
463+
bitratefd: {
464+
freq: 1000000,
465+
preScaler: 1,
466+
timeSeg1: 20,
467+
timeSeg2: 19,
468+
sjw: 19
469+
},
470+
id: 'toomoss0',
471+
},true)
472+
)
473+
474+
475+
client = new CAN_TP(
476+
new TOOMOSS_CAN({
477+
handle: '1417675180:1',
478+
name: 'client',
479+
vendor: 'toomoss',
480+
canfd: true,
481+
482+
483+
bitrate: {
484+
freq: 500000,
485+
preScaler: 1,
486+
timeSeg1: 68,
487+
timeSeg2: 11,
488+
sjw: 11
489+
},
490+
bitratefd: {
491+
freq: 1000000,
492+
preScaler: 1,
493+
timeSeg1: 20,
494+
timeSeg2: 19,
495+
sjw: 19
496+
},
497+
id: 'toomoss1',
498+
},true)
499+
)
500+
501+
})
502+
503+
dataList.forEach((data) => {
504+
addrList.forEach((addr) => {
505+
it(`write ${data.length} bytes data to ${addr.name}`, async () => {
506+
const clientSocket = new CAN_TP_SOCKET(client, addr)
507+
const serverSocket = new CAN_TP_SOCKET(server, swapAddr(addr))
508+
await clientSocket.write(data)
509+
const result = await serverSocket.read(1000)
510+
console.log('result',result)
511+
deepEqual(result.data, data)
512+
clientSocket.close()
513+
serverSocket.close()
514+
})
515+
})
516+
})
517+
afterAll(() => {
518+
client.close()
519+
server.close()
520+
})
521+
})

test/docan/toomoss.test.ts

Lines changed: 91 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ describe.skip('toomoss test bus error can', () => {
139139

140140
client.close()
141141
client2.close()
142-
142+
143143

144144

145145

@@ -248,20 +248,20 @@ describe('toomoss test bus error canfd', () => {
248248

249249
})
250250
// //write frame again
251-
const t1=await client.writeBase(4, {
251+
const t1 = await client.writeBase(4, {
252252
idType: CAN_ID_TYPE.STANDARD,
253253
brs: false,
254254
canfd: true,
255255
remote: false
256256
}, Buffer.alloc(64, 0x33))
257-
console.log('timestamp1',t1)
258-
const t2=await client.writeBase(4, {
257+
console.log('timestamp1', t1)
258+
const t2 = await client.writeBase(4, {
259259
idType: CAN_ID_TYPE.STANDARD,
260260
brs: false,
261261
canfd: true,
262262
remote: false
263263
}, Buffer.alloc(64, 0x33))
264-
console.log('timestamp2',t2)
264+
console.log('timestamp2', t2)
265265

266266
})
267267
afterAll(async () => {
@@ -274,3 +274,89 @@ describe('toomoss test bus error canfd', () => {
274274

275275
})
276276
})
277+
278+
279+
describe('toomoss multi send canfd', () => {
280+
let client!: TOOMOSS_CAN
281+
let client2!: TOOMOSS_CAN
282+
beforeAll(() => {
283+
client = new TOOMOSS_CAN({
284+
handle: '1417675180:0',
285+
name: 'test',
286+
id: 'toomoss',
287+
vendor: 'toomoss',
288+
canfd: true,
289+
bitrate: {
290+
freq: 250000,
291+
preScaler: 2,
292+
timeSeg1: 68,
293+
timeSeg2: 11,
294+
sjw: 11,
295+
clock: '40M'
296+
},
297+
bitratefd: {
298+
freq: 1000000,
299+
preScaler: 1,
300+
timeSeg1: 29,
301+
timeSeg2: 10,
302+
sjw: 2
303+
},
304+
database: ''
305+
306+
}, true)
307+
client2 = new TOOMOSS_CAN({
308+
handle: '1417675180:1',
309+
name: 'test2',
310+
id: 'toomoss',
311+
vendor: 'toomoss',
312+
canfd: true,
313+
314+
bitrate: {
315+
freq: 250000,
316+
preScaler: 2,
317+
timeSeg1: 68,
318+
timeSeg2: 11,
319+
sjw: 11,
320+
clock: '40M'
321+
},
322+
bitratefd: {
323+
freq: 1000000,
324+
preScaler: 1,
325+
timeSeg1: 29,
326+
timeSeg2: 10,
327+
sjw: 2
328+
},
329+
database: ''
330+
331+
332+
}, true)
333+
})
334+
test('write multi frame', async () => {
335+
336+
337+
const list = []
338+
for (let i = 0; i < 5; i++) {
339+
list.push(client.writeBase(3 + i, {
340+
idType: CAN_ID_TYPE.STANDARD,
341+
brs: false,
342+
canfd: true,
343+
remote: false
344+
}, Buffer.alloc(64, 0x33))
345+
)
346+
}
347+
348+
349+
await Promise.all(list)
350+
351+
352+
})
353+
afterAll(async () => {
354+
console.log('close client')
355+
client.close()
356+
client2.close()
357+
// await delay(1000)
358+
359+
360+
361+
})
362+
})

0 commit comments

Comments
 (0)