学习总结(二) 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}\`);

相关推荐
镜宇秋霖丶21 小时前
2026.5.6@霖宇博客制作中遇见的问题
前端·javascript·vue.js
Yupureki1 天前
《Linux网络编程》8.网络层IP原理
linux·运维·服务器·网络·ip
大厂数码评测员1 天前
免费菜谱管理小程序怎么做才顺手:从情侣、个人、家庭三类场景拆需求和实现
服务器·小程序·apache
吴声子夜歌1 天前
Vue3——TypeScript基础
javascript·typescript
yyuuuzz1 天前
aws亚马逊入门常见认知误区
运维·服务器·网络·云计算·github·aws
小李子呢02111 天前
前端八股Vue---Vue-router路由管理器
前端·javascript·vue.js
百锦再1 天前
Auto.js变成基础知识学习
开发语言·javascript·学习·sqlite·kotlin·android studio·数据库开发
KuaCpp1 天前
C++新特性学习
c++·学习
Bigger1 天前
Bun 能上生产吗?我的实战结论
前端·node.js·bun
eucalyptus-DE1 天前
Nova 计算节点故障排查指南
服务器·openstack