当前位置:   article > 正文

html页面自己写js脚本收发数据,支持串口和TCP通讯(二)TCP客户端收发_h5+js发送tcp接口

h5+js发送tcp接口

本工具作为TCP客户端,模拟终端与服务器进行通讯。
在这里插入图片描述

完整示例:

<body>
    <div>tcp测试客户端</div><br />
    <input type="text" id="txtServer" value="127.0.0.1" /><br />
    <input type="text" id="txtPort" value="60000" /><br />
    <button onclick="conn()">连接</button>
    <button onclick="close()">断开</button>
    <button onclick="sendData()">发送</button>
    <button onclick="recvData()">接收</button>
</body>
<script type="text/javascript">
    (async function () {
        await CefSharp.BindObjectAsync("tcpClientHelper");
        
    })();


    function conn() {
        (async function () {
            await CefSharp.BindObjectAsync("tcpClientHelper");
            var server = document.getElementById("txtServer").value;
            var port = document.getElementById("txtPort").value;
            console.log(server, port);
            await tcpClientHelper.conn(server, parseInt(port));

            //建立连接后发登录
            await tcpClientHelper.sendData("383635333734303530363031353933");

            setTimeout("recvData()", 1000);
        })();
    }

    function close() {
        (async function () {
            await CefSharp.BindObjectAsync("tcpClientHelper");

            await tcpClientHelper.close();
        })();
    }

    function sendData() {
        (async function () {
            await CefSharp.BindObjectAsync("tcpClientHelper");

            await tcpClientHelper.sendData("313233");

            console.log('sendData');
        })();
    }

    function recvData() {
        (async function () {
            await CefSharp.BindObjectAsync("tcpClientHelper");

            let recvString = await tcpClientHelper.recvData();
            console.log('[' + recvString+ ']');
            if (recvString.trim() == '680100010068FFFFFFFFFFFF010100CE16') {
                console.log('ok 31');
                await tcpClientHelper.sendData('68 03 00 03 00 68 FF FF FF FF FF FF 81 01 00 01 AA BB B8 16');
            }
            else if (recvString.trim() == '680100010068FFFFFFFFFFFF020000552316') {
                console.log('ok 32');
                await tcpClientHelper.sendData('68 01 00 01 00 68 FF FF FF FF FF FF 82 01 00 FF 4E 16');
            }

            setTimeout("recvData()", 1000);
        })();       
    }  
</script>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68

效果

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/163591
推荐阅读
相关标签
  

闽ICP备14008679号