|
serialPortPlus 封装了安卓多开串口通信的能力
- <!DOCTYPE html>
- <html>
- <head>
- <title>Module Develop</title>
- <meta charset="utf-8">
- <meta name="viewport"
- content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
- <style type="text/css">
- html,
- body {
- height: 100%
- }
- body {
- background-color: #fff;
- margin: 0;
- }
- #wrap {
- height: 100%;
- position: relative;
- }
- #header {
- padding-top: 20px;
- background-color: #5082c2;
- height: 44px;
- position: relative;
- }
- #header h1 {
- font-size: 20px;
- height: 44px;
- line-height: 44px;
- margin: 0em;
- color: #fff;
- margin-left: 100px;
- margin-right: 100px;
- text-align: center;
- }
- #main {
- display: -webkit-box;
- -webkit-box-orient: vertical;
- -webkit-box-pack: center;
- }
- a.button {
- display: -webkit-box;
- -webkit-box-orient: vertical;
- -webkit-box-pack: center;
- -webkit-box-align: center;
- height: 32px;
- margin: 8px;
- background-color: rgba(240, 240, 240, 1.0);
- border-color: rgba(220, 220, 220, 1.0);
- border-width: 2px;
- border-style: solid;
- }
- a.active {
- background-color: rgba(240, 240, 240, 0.7);
- }
- .input1 {
- display: -webkit-box;
- -webkit-box-orient: vertical;
- -webkit-box-pack: center;
- -webkit-box-align: center;
- height: 32px;
- margin: 8px;
- background-color: rgba(240, 240, 240, 1.0);
- border-color: rgba(220, 220, 220, 1.0);
- border-width: 2px;
- border-style: solid;
- }
- </style>
- </head>
- <body>
- <div id="wrap">
- <div id="main"><br/><br/>
- 接收数据类型;(text hex):
- <input type="text" class="input1" name="revType" id="revType" size="40" value="hex" />
- <a class="button" tapmode="active" onclick="getAllDevices()">getAllDevices</a>
- <a class="button" tapmode="active" onclick="getAllDeicesPath()">getAllDeicesPath</a>
- 端口:
- <input type="text" class="input1" name="port" id="port" size="40" value="/dev/ttyS2" />
- 波特率:
- <input type="text" class="input1" name="baudRate" id="baudRate" size="40" value="9600" />
- 支持设置停止位(1:1位停止位 2:2位停止位):
- <input type="text" class="input1" name="stopBits" id="stopBits" size="40" value="2" />
- 支持设置数据位(5:5位数据位 6:6位数据位 7:7位数据位 8:8位数据位):
- <input type="text" class="input1" name="dataBits" id="dataBits" size="40" value="8" />
- 支持设置检验位(0:无奇偶校验 1:奇校验 2:偶校验):
- <input type="text" class="input1" name="parity" id="parity" size="40" value="0" />
- 支持设置流控(0:不使用流控 1:硬件流控 2:软件流控):
- <input type="text" class="input1" name="flowCon" id="flowCon" size="40" value="0" />
- <a class="button" tapmode="active" onclick="open1()">打开串口</a>
- 发送数据类型;(text hex):
- <input type="text" class="input1" name="sendType" id="sendType" size="40" value="hex" />
- 发送数据:
- <input type="text" class="input1" name="data" id="data" size="40" value="41" />
- <a class="button" tapmode="active" onclick="send()">发送消息</a>
- <a class="button" tapmode="active" onclick="close1()">关闭串口</a>
- <textarea id="msgId" name="msgId" cols="30" rows="50"></textarea>
- </div>
- </div>
- </body>
- <script type="text/javascript" src="../script/api.js"></script>
- <script>
- var demo;
- function getAllDevices() {
- var ret = demo.getAllDevices();
- showText("getAllDevices>>" + JSON.stringify(ret));
- }
- function getAllDeicesPath() {
- var ret = demo.getAllDeicesPath();
- showText("getAllDevices>>" + JSON.stringify(ret));
- }
- function open1() {
- var _port = document.getElementById("port").value;
- var _baudRate = document.getElementById("baudRate").value;
- var _stopBits = document.getElementById("stopBits").value;
- var _dataBits = document.getElementById("dataBits").value;
- var _parity = document.getElementById("parity").value;
- var _flowCon = document.getElementById("flowCon").value;
- demo.open({
- port: _port,
- baudRate: _baudRate,
- stopBits: _stopBits,
- dataBits: _dataBits,
- parity: _parity,
- flowCon: _flowCon
- }, function (ret, err) {
- showText("open>>" + JSON.stringify(ret) + " " + JSON.stringify(err));
- });
- }
- function send() {
- var _sendType = document.getElementById("sendType").value;
- var _data = document.getElementById("data").value;
- var ret = demo.send({
- sendType: _sendType,
- data: _data
- });
- showText("send>>" + JSON.stringify(ret));
- }
- function close1() {
- var ret = demo.close();
- showText("close>>" + JSON.stringify(ret));
- }
- function showText(msg) {
- var _msgId = document.getElementById("msgId").value;
- document.getElementById("msgId").value = msg + "\n" + _msgId;
- }
- function apiready() {
- demo = api.require('serialPortPlus');
- var _revType = document.getElementById("revType").value;
- demo.addEventListener({
- revType: _revType
- }, function (ret, err) {
- showText("addEventListener>>" + JSON.stringify(ret) + " " + JSON.stringify(err));
- });
- }
- </script>
- </html>
复制代码
|
|