Skip to content

Commit f6925c7

Browse files
committed
Fix exceptions description bug!
1 parent b23a3db commit f6925c7

File tree

3 files changed

+29
-14
lines changed

3 files changed

+29
-14
lines changed

src/data-field-strategy.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,28 @@ export class AddressQuantityRegisters extends AddressQuantity {
124124
this.registers = new Registers(data.slice(4));
125125
}
126126
}
127+
128+
export class Exception {
129+
static exceptionDescriptions: ReadonlyMap<number, string> = new Map<number, string>([
130+
[0x81, 'Illegal Function'],
131+
[0x82, 'Illegal Data Address'],
132+
[0x83, 'Illegal Data Value'],
133+
[0x84, 'Server Device Failure'],
134+
[0x85, 'Acknowledge'],
135+
[0x86, 'Server Device Busy'],
136+
[0x87, 'Negative Acknowledge'],
137+
[0x88, 'Memory Parity Error'],
138+
[0x90, 'Gateway Path Unavailable'],
139+
[0x91, 'Gateway Target Device Failed to Respond'],
140+
]);
141+
code: number;
142+
description: string | undefined;
143+
144+
constructor(data: number[]) {
145+
if (data.length !== 1) {
146+
throw new Error('Just on byte of exception code is allowed!');
147+
}
148+
this.code = data[0];
149+
this.description = Exception.exceptionDescriptions.get(this.code);
150+
}
151+
}

src/frame.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export class Frame {
3535
return [
3636
new TableDataColumn(`${Frame.getDateTime()} (${this.type})`, this.type),
3737
new TableDataColumn(`${this.slaveAddress} = 0x${Converters.byteToHex(this.slaveAddress!)}`, this.type),
38-
new TableDataColumn(FunctionCodes.getDescription(this.functionCode), this.type),
38+
new TableDataColumn(FunctionCodes.getDescription(this.functionCode), FunctionCodes.isError(this.functionCode || 0) ? 'error' : this.type),
3939
new TableDataColumn(this.getDataLength().toString(), this.type),
4040
new TableDataColumn(this.getDataAsText(), this.isNoValidDataFormat() ? 'error' : this.type),
4141
this.type === 'error' ? new TableDataColumn('', this.type) : new TableColumnButton('To send form', () => Dom.sendForm.setFormData(this)),

src/function-codes.ts

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Converters } from "./converters";
2-
import { AddressBoolean, AddressQuantity, AddressQuantityBooleans, AddressQuantityRegisters, AddressRegister, Booleans, DataFieldStrategy, Registers } from "./data-field-strategy";
2+
import { AddressBoolean, AddressQuantity, AddressQuantityBooleans, AddressQuantityRegisters, AddressRegister, Booleans, DataFieldStrategy, Exception, Registers } from "./data-field-strategy";
33

44
interface FunctionCodeDetails {
55
readonly code: number;
@@ -20,16 +20,6 @@ const allFunctionCodeDetails: ReadonlySet<FunctionCodeDetails> = new Set<Functio
2020
{ code: 0x0f, description: 'Write Multiple Coils', masterRequestStrategy: AddressQuantityBooleans, slaveResponseStrategy: AddressQuantity },
2121
{ code: 0x10, description: 'Write Multiple Registers', masterRequestStrategy: AddressQuantityRegisters, slaveResponseStrategy: AddressQuantity },
2222
{ code: 0x11, description: 'Identify Device Server' },
23-
{ code: 0x81, description: 'Illegal Function' },
24-
{ code: 0x82, description: 'Illegal Data Address' },
25-
{ code: 0x83, description: 'Illegal Data Value' },
26-
{ code: 0x84, description: 'Server Device Failure' },
27-
{ code: 0x85, description: 'Acknowledge' },
28-
{ code: 0x86, description: 'Server Device Busy' },
29-
{ code: 0x87, description: 'Negative Acknowledge' },
30-
{ code: 0x88, description: 'Memory Parity Error' },
31-
{ code: 0x90, description: 'Gateway Path Unavailable' },
32-
{ code: 0x91, description: 'Gateway Target Device Failed to Respond' },
3323
]);
3424

3525
export type StrategyResult = { object?: Object, error?: string } | undefined;
@@ -51,7 +41,7 @@ export class FunctionCodes {
5141
if (code === undefined) {
5242
return '';
5343
}
54-
const description = this.descriptions.get(code);
44+
const description = this.descriptions.get(code & ~0x80);
5545
if (description) {
5646
return this._getDescription(code, description);
5747
}
@@ -71,7 +61,7 @@ export class FunctionCodes {
7161
}
7262

7363
static newSlaveResponse(code: number, dataFieldBytes: number[]): StrategyResult {
74-
return this._newDataFieldStrategy(this.slaveResponseStrategies.get(code), dataFieldBytes);
64+
return this._newDataFieldStrategy(code & 0x80 ? Exception : this.slaveResponseStrategies.get(code), dataFieldBytes);
7565
}
7666

7767
private static _newDataFieldStrategy(strategy: DataFieldStrategy | undefined, dataFieldBytes: number[]): StrategyResult {

0 commit comments

Comments
 (0)