以 byte[] 形式发送文本输入

Sent Text input as byte[]

我想通过 WebSockets 将网页上文本输入的 IP 地址传输到我的 Arduino,我希望它按原样处理,而不是作为 char*。

到目前为止我写的是:

HTML:

<tr id="network1" style="display: none;">
            <th>Client IP</th>
            <th>
                    <input type="text" maxlength="3" class="ipInput" id="inputClientIP1" oninput="checkClientIP()">
                    .
                    <input type="text" maxlength="3" class="ipInput" id="inputClientIP2" oninput="checkClientIP()">
                    .
                    <input type="text" maxlength="3" class="ipInput" id="inputClientIP3" oninput="checkClientIP()">
                    .
                    <input type="text" maxlength="3" class="ipInput" id="inputClientIP4" oninput="checkClientIP()">
                </div>
            </th>
        </tr>

通过Websocket发送数据的JS:

 function sendData(){
 ws.send(document.getElementById("inputClientIP1").value + "," + document.getElementById("inputClientIP2").value + "," + document.getElementById("inputClientIP3").value + "," + document.getElementById("inputClientIP4").value
 };

arduino 只是将 IP 作为 char * 接收。 发送的消息是 "192, 168, 178, 100"

有没有什么好的方法可以获取一个字节的ip?

PS 我尝试将输入更改为 type="number" 但没有其他结果

好的 value 属性 通常是一个字符串

即使将类型更改为数字,您仍然会使用 +"," 将其转换为字符串,因此结果始终是字符串 如果没有确切地了解 Arduino 如何处理网络套接字代码,就不可能知道它是如何工作的,但是如果你想在网络套接字中发送一般的二进制数据,你可以制作一个 uint8array 并发送它

ws.binaryType = 'arraybuffer';
var buf = new ArrayBuffer (4)
var byteArr = new Uint8Array(buf)

byteArr[0] = parseInt(document.getElementById("inputClientIP1").value)

byteArr[1] = parseInt(document.getElementById("inputClientIP2").value)

byteArr[2] = parseInt(document.getElementById("inputClientIP3").value)


byteArr[3] = parseInt(document.getElementById("inputClientIP4").value)


we.send(byteArr)