Yoctopuce Yocto-Serial Mode D'emploi page 66

Table des Matières

Publicité

9. Utilisation du Yocto-Serial en JavaScript / EcmaScript
<!DOCTYPE html>
<html>
<head>
<meta
charset="UTF-8">
<title>Hello
World</title>
<script
src="../../lib/yocto_api.js"></script>
<script
src="../../lib/yocto_serialport.js"></script>
<script>
let serialPort;
async function startDemo() {
await YAPI.LogUnhandledPromiseRejections();
await YAPI.DisableExceptions();
// Setup the API to use the VirtualHub on local machine
let errmsg = new YErrorMsg();
if (await YAPI.RegisterHub('127.0.0.1', errmsg) != YAPI.SUCCESS) {
alert('Cannot contact VirtualHub on 127.0.0.1: ' + errmsg.msg);
return;
}
let serial = document.getElementById('serial').value;
if(serial == '') {
// by default use any connected module suitable for the demo
let anySerial = YSerialPort.FirstSerialPort();
if(anySerial) {
let module = await anySerial.module();
serial = await module.get_serialNumber();
document.getElementById('serial').value = serial;
}
}
serialPort = YSerialPort.FindSerialPort(serial + ".serialPort");
await serialPort.set_serialMode("9600,8N1");
await serialPort.set_protocol("Line");
await serialPort.reset();
refresh();
}
async function refresh()
{
let line;
if (await serialPort.isOnline()) {
line = await serialPort.readLine();
document.getElementById('returned_value').value +=line;
} else {
console.log('Module not connected');
}
setTimeout(refresh, 500);
}
window.send_data = function()
{
var tosend = document.getElementById('line_input').value;
serialPort.writeLine(tosend);
};
startDemo();
</script>
</head>
<body>
Module to use:
<input
Type line to
send<br>
<input
id="line_input" onchange='javascript:send_data()'><br>
<b>make sure voltage
<b>are properly configured
Received:
<br/>
<textarea
id="returned_value"></textarea>
</body>
</html>
Aucune installation n'est nécessaire pout utiliser cet exemple, il suffit d'ouvrir la page HTML avec un
navigateur web.
60
id='serial'><br>
levels</b><br>
</b><br>
www.yoctopuce.com

Publicité

Table des Matières
loading

Table des Matières