Yoctopuce Yocto-RS485-V2 Mode D'emploi page 123

Table des Matières

Publicité

import *
as readline
let serialPort: YSerialPort;
let target: { slave: number, reg:
let g_step:
number
let rl: readline.Interface;
async function startDemo(): Promise<void>
{
await YAPI.LogUnhandledPromiseRejections();
// Setup the API to use the VirtualHub on local machine
let errmsg:
YErrorMsg
if(await YAPI.RegisterHub('127.0.0.1', errmsg) != YAPI.SUCCESS) {
console.log('Cannot contact VirtualHub on 127.0.0.1:
return;
}
// Select specified device, or use first available one
let serial:
string
if (serial[8] != '-') {
// by default use any connected module suitable for the demo
let
anyserial
if (anyserial) {
let module:
serial
} else {
console.log('No matching module connected, check cable
await YAPI.FreeAPI();
return;
}
}
console.log('Using device '
serialPort
= YSerialPort.FindSerialPort(serial+'.serialPort');
rl
= readline.createInterface({
input: process.stdin,
output:
process.stdout
});
console.log('Please enter the MODBUS slave address
console.log('Slave:');
rl.on('line', handleInput);
}
async function handleInput(chunk: string): Promise<void>
{
var val:
number
switch (g_step) {
case 1:
if
(val
console.log("invalid slave
} else {
target.slave
g_step++;
console.log("Slave = "
console.log("Please select a Coil No (>=1), Input Bit No
console.log("
console.log("No:
}
break;
case 2:
if
(val
console.log("invalid register
} else {
target.reg
await printModbusValue(target.slave, target.reg);
g_step++;
console.log("Press ENTER to read again, Q to
if
}
}
break;
case 3:
if (chunk.charAt(0) ==
await YAPI.FreeAPI();
rl.close();
break;
}
www.yoctopuce.com
from 'readline';
number
= 1;
= new YErrorMsg();
= process.argv[process.argv.length-1];
= YSerialPort.FirstSerialPort();
YModule
= await anyserial.get_module();
= await module.get_serialNumber();
+ serial);
= parseInt(chunk);
<
1
||
val
> 255) {
= val;
+ target.slave);
Register No (>=30001) or Input Register No
");
<
1
||
val
>=
50000
= val;
((target.reg
% 30000) < 10000) {
console.log(" or enter a new
'q'
|| chunk.charAt(0) == 'Q') {
15. Utilisation du Yocto-RS485-V2 en TypeScript
} = { slave: 0, reg:
(1...255)');
number");
||
(val
% 10000) == 0) {
number");
value:");
0
}
'+errmsg.msg);
!');
(>=10001+),");
(>=40001)");
quit:");
115

Publicité

Table des Matières
loading

Ce manuel est également adapté pour:

Yocto-rs232

Table des Matières