问题描述
尝试访问本地网络上的共享文件夹时,以下操作有效:
fs.readdir('\\\\192.168.178.28\\temp2',(err,files) ...
虽然,以下给出了错误
fs.readdir('\\\\192.168.178.28\\',files) ...
[Error: ENOENT: no such file or directory,scandir 'C:\192.168.178.28'] {
errno: -4058,code: 'ENOENT',syscall: 'scandir',path: 'C:\\192.168.178.28'
}
如果没有指定子文件夹,Node 会将其作为本地 C: 驱动器,尽管 \ 作为主机名 IP。
我尝试了其他函数和方法,以及Path模块。都给出了相似的结果。
相关信息: Use node js to access a local network drive
有人可以帮忙吗?谢谢。
解决方法
我最近几天收集的信息:
- 路径(上面的“temp2”)是“指针”指向的位置。
- 只有主机名(计算机名或 ip)“指针”不起作用。
就我而言,带有微控制器(单芯片计算机)的设备只有有限的接口,其中数据的存储方式与 PC 不同。因此, fs.readdir 将不起作用。
多亏@jfriend00 的提示,我成功获取了通信协议,并成功地使用Nodejs 从设备读取了数据。下面以代码为例(对于像我这样的初学者)。
//instruction packet from protocol:
//header: 0xA5 0x5A
//length: 0x00 0x05
//instruction code: 0x52 (device response depending on this code)
//parameters: 0x00 0x00 0x00 0x05
//ending: 0x0D 0x0A
const net = require ('net');
const hexString = "A55A000552000000050D0A";//0xA5 0x5A 0x00 0x05 0x52 0x00 0x00 0x00 0x05 0x0D 0x0A
const reqHex = Buffer.from(hexString,'hex');
var client = new net.Socket();
client.connect(8080,'192.168.4.1',() => {
console.log('Connected to device at 8080');
client.write(reqHex);
});
client.on('data',(data) => { //listen to data response from device
console.log('Received from device: ' + data);
client.destroy();
});
client.on('close',() => {
console.log('Connection to device closed');
});