学习总结(二) node.js服务器如何使用net模块向硬件发送命令与接收数据?

服务器server.js:

const net = require("net"); //此模块用于tcp/ip通讯

当收到get请求时:

server.get("/cfjcApi/v1/SkyTempHudi", (req, res) => {

let client = new net.Socket();

client.connect(网络模块端口, 网络模块Ip, () => { //此模块需要设置好波特率等通讯参数

let cmd = dataCtl.makeCmd(0, parseInt("0XA1"), 0, []); //调用函数生成要发送的命令

client.write(cmd); //发送命令

client.on("data", function (data) { //当收到数据时

client.end(); //断开与485转网络模块的连接

return res.send({ result: Array.from(data), type: 1 }) //将收到的数据与设备类型发送至请求方

});

});

}

浏览器:

async function MeasureselectedStore() {

let res = await fetch(`http://192.168.0.163:3000/cfjcApi/v1/SkyTempHudi\`);

let json = await res.json();

let skytemphudi = json;

if (skytemphudi.type === 1) {//对应数据处理}

else if (skytemphudi.type === 2) {//对应数据处理}

}

这是测量通用设备的方式,不涉及根据设备位置查找设备类型.

测量某个位置的设备时,

服务器在res.query.storename中得到设备位置,然后根据设备位置去查找设备类型,然后发送

浏览器则fetch(`http://192.168.0.163:3000/cfjcApi/GrainTempHudi?storename=${storename}\`);

相关推荐
晓梦林1 分钟前
MAZESEC-X1靶场学习笔记
笔记·学习
我的xiaodoujiao37 分钟前
API 接口自动化测试详细图文教程学习系列15--项目实战演练2
python·学习·测试工具·pytest
TImCheng060944 分钟前
职场人AI学习周期评估:不同学习路径的时间成本
人工智能·学习
周末也要写八哥1 小时前
C4D/Cinema 4D 2026超详细下载与安装教程(附资源包)
学习·数学建模
云栖梦泽1 小时前
Linux内核与驱动:14.SPI子系统
linux·运维·服务器·c++
xinzheng新政1 小时前
openclaw dashboard报错 internal server error
学习
yipiantian1 小时前
在Claude项目中实现跨目录访问Skills
linux·运维·服务器
神探小白牙2 小时前
eCharts 多系列柱状图增加背景图
javascript·ecmascript·echarts
cen__y2 小时前
Linux07(信号01)
linux·运维·服务器·c语言·开发语言
nashane2 小时前
HarmonyOS 6学习:RichEditor宽度“暴力”计算与富文本截图避坑
学习·harmonyos 5