帖子
帖子
用户
博客
课程
123下一页
返回列表 发新帖
显示全部楼层
216
帖子
5
勋章
5909
Y币

serialPortPlus模块demo示例

[复制链接]
发表于 2022-3-10 21:43:34
serialPortPlus 封装了安卓多开串口通信的能力

  1. <!DOCTYPE html>
  2. <html>

  3. <head>
  4.         <title>Module Develop</title>
  5.         <meta charset="utf-8">
  6.         <meta name="viewport"
  7.                 content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
  8.         <style type="text/css">
  9.                 html,
  10.                 body {
  11.                         height: 100%
  12.                 }

  13.                 body {
  14.                         background-color: #fff;
  15.                         margin: 0;
  16.                 }

  17.                 #wrap {
  18.                         height: 100%;
  19.                         position: relative;
  20.                 }

  21.                 #header {
  22.                         padding-top: 20px;
  23.                         background-color: #5082c2;
  24.                         height: 44px;
  25.                         position: relative;
  26.                 }

  27.                 #header h1 {
  28.                         font-size: 20px;
  29.                         height: 44px;
  30.                         line-height: 44px;
  31.                         margin: 0em;
  32.                         color: #fff;
  33.                         margin-left: 100px;
  34.                         margin-right: 100px;
  35.                         text-align: center;
  36.                 }

  37.                 #main {
  38.                         display: -webkit-box;
  39.                         -webkit-box-orient: vertical;
  40.                         -webkit-box-pack: center;
  41.                 }

  42.                 a.button {
  43.                         display: -webkit-box;
  44.                         -webkit-box-orient: vertical;
  45.                         -webkit-box-pack: center;
  46.                         -webkit-box-align: center;
  47.                         height: 32px;
  48.                         margin: 8px;
  49.                         background-color: rgba(240, 240, 240, 1.0);
  50.                         border-color: rgba(220, 220, 220, 1.0);
  51.                         border-width: 2px;
  52.                         border-style: solid;
  53.                 }

  54.                 a.active {
  55.                         background-color: rgba(240, 240, 240, 0.7);
  56.                 }

  57.                 .input1 {
  58.                         display: -webkit-box;
  59.                         -webkit-box-orient: vertical;
  60.                         -webkit-box-pack: center;
  61.                         -webkit-box-align: center;
  62.                         height: 32px;
  63.                         margin: 8px;
  64.                         background-color: rgba(240, 240, 240, 1.0);
  65.                         border-color: rgba(220, 220, 220, 1.0);
  66.                         border-width: 2px;
  67.                         border-style: solid;
  68.                 }
  69.         </style>
  70. </head>

  71. <body>
  72.         <div id="wrap">
  73.                 <div id="main"><br/><br/>
  74.                           接收数据类型;(text hex):
  75.                         <input type="text" class="input1" name="revType" id="revType" size="40" value="hex" />
  76.                         <a class="button" tapmode="active" onclick="getAllDevices()">getAllDevices</a>
  77.                         <a class="button" tapmode="active" onclick="getAllDeicesPath()">getAllDeicesPath</a>
  78.                           端口:
  79.                         <input type="text" class="input1" name="port" id="port" size="40" value="/dev/ttyS2" />
  80.                           波特率:
  81.                         <input type="text" class="input1" name="baudRate" id="baudRate" size="40" value="9600" />
  82.                           支持设置停止位(1:1位停止位 2:2位停止位):
  83.                         <input type="text" class="input1" name="stopBits" id="stopBits" size="40" value="2" />
  84.                           支持设置数据位(5:5位数据位 6:6位数据位 7:7位数据位 8:8位数据位):
  85.                         <input type="text" class="input1" name="dataBits" id="dataBits" size="40" value="8" />
  86.                           支持设置检验位(0:无奇偶校验 1:奇校验 2:偶校验):
  87.                         <input type="text" class="input1" name="parity" id="parity" size="40" value="0" />
  88.                           支持设置流控(0:不使用流控 1:硬件流控 2:软件流控):
  89.                         <input type="text" class="input1" name="flowCon" id="flowCon" size="40" value="0" />
  90.                         <a class="button" tapmode="active" onclick="open1()">打开串口</a>
  91.                           发送数据类型;(text hex):
  92.                         <input type="text" class="input1" name="sendType" id="sendType" size="40" value="hex" />
  93.                           发送数据:
  94.                         <input type="text" class="input1" name="data" id="data" size="40" value="41" />
  95.                         <a class="button" tapmode="active" onclick="send()">发送消息</a>
  96.                         <a class="button" tapmode="active" onclick="close1()">关闭串口</a>
  97.                         <textarea id="msgId" name="msgId" cols="30" rows="50"></textarea>
  98.                 </div>
  99.         </div>
  100. </body>
  101. <script type="text/javascript" src="../script/api.js"></script>
  102. <script>
  103.         var demo;
  104.         function getAllDevices() {
  105.                 var ret = demo.getAllDevices();
  106.                 showText("getAllDevices>>" + JSON.stringify(ret));
  107.         }

  108.         function getAllDeicesPath() {
  109.                 var ret = demo.getAllDeicesPath();
  110.                 showText("getAllDevices>>" + JSON.stringify(ret));
  111.         }

  112.         function open1() {
  113.                 var _port = document.getElementById("port").value;
  114.                 var _baudRate = document.getElementById("baudRate").value;
  115.                 var _stopBits = document.getElementById("stopBits").value;
  116.                 var _dataBits = document.getElementById("dataBits").value;
  117.                 var _parity = document.getElementById("parity").value;
  118.                 var _flowCon = document.getElementById("flowCon").value;
  119.                 demo.open({
  120.                         port: _port,
  121.                         baudRate: _baudRate,
  122.                         stopBits: _stopBits,
  123.                         dataBits: _dataBits,
  124.                         parity: _parity,
  125.                         flowCon: _flowCon
  126.                 }, function (ret, err) {
  127.                         showText("open>>" + JSON.stringify(ret) + "   " + JSON.stringify(err));
  128.                 });
  129.         }

  130.         function send() {
  131.                 var _sendType = document.getElementById("sendType").value;
  132.                 var _data = document.getElementById("data").value;

  133.                 var ret = demo.send({
  134.                         sendType: _sendType,
  135.                         data: _data
  136.                 });

  137.                 showText("send>>" + JSON.stringify(ret));
  138.         }

  139.         function close1() {
  140.                 var ret = demo.close();
  141.                 showText("close>>" + JSON.stringify(ret));
  142.         }

  143.         function showText(msg) {
  144.                 var _msgId = document.getElementById("msgId").value;
  145.                 document.getElementById("msgId").value = msg + "\n" + _msgId;
  146.         }

  147.         function apiready() {
  148.                 demo = api.require('serialPortPlus');
  149.                 var _revType = document.getElementById("revType").value;
  150.                 demo.addEventListener({
  151.                         revType: _revType
  152.                 }, function (ret, err) {
  153.                         showText("addEventListener>>" + JSON.stringify(ret) + "   " + JSON.stringify(err));
  154.                 });
  155.         }
  156. </script>

  157. </html>
复制代码


380
帖子
4
勋章
6
Y币
点赞
0
帖子
0
勋章
4
Y币
这个支持向安卓手机typec充电口发送命令吗?
216
帖子
5
勋章
5909
Y币
没试过。不晓得行不行。
90
帖子
0
勋章
423
Y币
支持多个串口?同时使用?
90
帖子
0
勋章
423
Y币
打开指定序号 通过这个来多个串口?
216
帖子
5
勋章
5909
Y币
嗯,你试试呢?
107
帖子
2
勋章
1355
Y币
52yaoer · 2022-4-12 19:20嗯,你试试呢?

打开多个串口,只能打开一个 ,另外一个提示失败
216
帖子
5
勋章
5909
Y币
zzhangqi · 2023-3-10 11:21打开多个串口,只能打开一个 ,另外一个提示失败

失败啊,这就不好判断了啊,打开一个串口的逻辑是成功的嘛。
6
帖子
0
勋章
827
Y币
打开串口,返回的为什么是false

本帖子中包含更多资源,您需要 登录 才可以下载或查看,没有帐号?立即注册

X
123下一页
您需要登录后才可以回帖 登录

本版积分规则