-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from Jalle19/modbus-v15
Expanded Modbus sensor support
- Loading branch information
Showing
10 changed files
with
268 additions
and
27 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
export enum RegisterType { | ||
HOLDING_REGISTER = 'h', | ||
INPUT_REGISTER = 'i', | ||
COIL = 'c', | ||
DISCRETE_INPUT = 'd', | ||
} | ||
|
||
const dataTypes = ['int16', 'uint16', 'int32', 'uint32', 'boolean', 'float'] | ||
export type DataType = (typeof dataTypes)[number] | ||
|
||
export type ModbusRegister = { | ||
registerType: RegisterType | ||
address: number | ||
dataType: DataType | ||
} | ||
|
||
const REGISTER_DEFINITION_REGEXP = new RegExp('^([a-z]@)?(\\d+)(\\/[a-z0-9]*)?$') | ||
|
||
export const stringify = (r: ModbusRegister): string => { | ||
return `${r.registerType}@${r.address}/${r.dataType}` | ||
} | ||
|
||
export const getRegisterLength = (r: ModbusRegister): number => { | ||
switch (r.dataType) { | ||
case 'int32': | ||
case 'uint32': | ||
return 4 | ||
case 'int16': | ||
case 'uint16': | ||
case 'float': | ||
return 2 | ||
case 'boolean': | ||
default: | ||
return 1 | ||
} | ||
} | ||
|
||
export const parseRegisterDefinition = (definition: string): ModbusRegister => { | ||
const result = REGISTER_DEFINITION_REGEXP.exec(definition) | ||
|
||
if (result === null) { | ||
throw new Error(`Unable to parse register definition "${definition}"`) | ||
} | ||
|
||
let [, registerType, , dataType] = result | ||
const address = result[2] | ||
|
||
// Parse register type | ||
if (registerType === undefined) { | ||
registerType = getDefaultRegisterType() | ||
} else { | ||
registerType = registerType.substring(0, registerType.length - 1) | ||
} | ||
|
||
if (!isValidRegisterType(registerType)) { | ||
throw new Error(`Invalid register type specified: ${registerType}`) | ||
} | ||
|
||
// Parse data address | ||
const parsedAddress = parseInt(address, 10) | ||
|
||
// Parse data type | ||
if (dataType === undefined) { | ||
dataType = getDefaultDataType(registerType) | ||
} else { | ||
dataType = dataType.substring(1) | ||
} | ||
|
||
if (!isValidDataType(dataType)) { | ||
throw new Error(`Invalid data type specified: ${dataType}`) | ||
} | ||
|
||
return { | ||
registerType: registerType as RegisterType, | ||
address: parsedAddress, | ||
dataType, | ||
} | ||
} | ||
|
||
const getDefaultRegisterType = (): RegisterType => { | ||
return RegisterType.HOLDING_REGISTER | ||
} | ||
|
||
const getDefaultDataType = (registerType: RegisterType): DataType => { | ||
if (registerType === RegisterType.INPUT_REGISTER || registerType === RegisterType.HOLDING_REGISTER) { | ||
return 'int16' | ||
} else { | ||
return 'boolean' | ||
} | ||
} | ||
|
||
const isValidRegisterType = (registerType: string): registerType is RegisterType => { | ||
return Object.values<string>(RegisterType).includes(registerType) | ||
} | ||
|
||
const isValidDataType = (dataType: string): dataType is DataType => { | ||
return dataTypes.includes(dataType) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.