当前位置:   article > 正文

[VUE] Web Serial API的简单示例_vue web serial api

vue web serial api
  1. <template>
  2. <div class="home">
  3. <div>
  4. <input type="text" v-model="inputData" placeholder="输入要发送的数据" />
  5. <button @click="sendData">发送</button>
  6. </div>
  7. <div>
  8. <textarea v-model="receivedData" readonly></textarea>
  9. </div>
  10. <button @click="initSerial">Initialize Serial Connection</button>
  11. </div>
  12. </template>
  13. <script>
  14. export default {
  15. data() {
  16. return {
  17. port: null,
  18. inputData: "", // 用于存储输入的数据
  19. receivedData: "", // 用于存储接收到的数据
  20. };
  21. },
  22. created() {
  23. },
  24. methods: {
  25. async initSerial() {
  26. try {
  27. this.port = await navigator.serial.requestPort();
  28. console.log("port: ", this.port);
  29. await this.port.open({ baudRate: 115200 });
  30. this.readData();
  31. // 监听设备连接
  32. navigator.serial.addEventListener("connect", (event) => {
  33. console.log("设备已连接");
  34. });
  35. // 监听设备断开
  36. navigator.serial.addEventListener("disconnect", (event) => {
  37. console.log("设备已断开");
  38. });
  39. } catch (error) {
  40. console.error("Serial connection error:", error);
  41. }
  42. },
  43. async readData() {
  44. const decoder = new TextDecoder("utf-8"); // 指定字符编码方式
  45. const reader = this.port.readable.getReader();
  46. try {
  47. while (true) {
  48. const { value, done } = await reader.read();
  49. if (done) {
  50. break;
  51. }
  52. const decodedString = decoder.decode(value); // 将字节数组解码为字符串
  53. // 处理接收到的数据
  54. console.log("Received data:", decodedString);
  55. this.receivedData += decodedString;
  56. }
  57. } catch (error) {
  58. console.error("Serial read error:", error);
  59. } finally {
  60. reader.releaseLock();
  61. }
  62. },
  63. async sendData() {
  64. console.log("data: ", this.inputData);
  65. const writer = this.port.writable.getWriter();
  66. const encoder = new TextEncoder();
  67. const dataArray = encoder.encode(this.inputData);
  68. try {
  69. await writer.write(dataArray);
  70. writer.releaseLock();
  71. } catch (error) {
  72. console.error("Serial write error:", error);
  73. }
  74. this.inputData = "";
  75. },
  76. async disconnectSerialPort(port) {
  77. try {
  78. await port.close(); // 关闭串口连接
  79. console.log("Serial port disconnected.");
  80. } catch (error) {
  81. console.error("Error disconnecting serial port:", error);
  82. }
  83. },
  84. },
  85. };
  86. </script>

 

Web Serial API:Web Serial API

WebUSB API:WebUSB API - Web APIs | MDN

Serial Terminal : https://googlechromelabs.github.io/serial-terminal/

相关文章:

什么,网页也能直接与硬件通信?Web Serial API!|8月更文挑战 什么,网页也能直接与硬件通信?Web Serial API!|8月更文挑战 - 掘金

【10. 连接硬件设备至 web 应用】 10. 连接硬件设备至 web 应用_哔哩哔哩_bilibili

使用 Web Serial API 在浏览器上实现基于 WEB 的串口通信 GitHub - WangTiantian139/serial-logger-and-plotter

跨越嵌入式到云端的新型容器:WebAssembly Micro Runtime 跨越嵌入式到云端的新型容器:WebAssembly Micro Runtime_开源_王鑫_InfoQ精选文章

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

闽ICP备14008679号