简体   繁体   English

Nodejs访问本地网络上的共享文件夹,根目录问题

[英]Nodejs access shared folder on local network, root directory problem

When trying to access shared folder on local network, the following works:尝试访问本地网络上的共享文件夹时,以下工作:

fs.readdir('\\\\192.168.178.28\\temp2', (err, files) ...

while, the following gives error同时,以下给出错误

fs.readdir('\\\\192.168.178.28\\', (err, 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'

} 

If subfolder is not specified, Node takes it as local C: drive despite \ as hostname IP.如果未指定子文件夹,Node 会将其视为本地 C: drive 尽管 \ 作为主机名 IP。

I tried other functions and methods, as well as Path module.我尝试了其他功能和方法,以及 Path 模块。 All give similar results.都给出了相似的结果。

related Info: Use node js to access a local network drive相关信息: 使用 node js 访问本地网络驱动器

Can anyone help?任何人都可以帮忙吗? Thanks.谢谢。

Information I collected in the last days:我最近几天收集的信息:

  1. Path ("temp2" above) is the place that the "pointer" points to.路径(上面的“temp2”)是“指针”指向的地方。
  2. Only hostname (computer name, or ip) the "pointer" won't work.只有主机名(计算机名或 ip)“指针”不起作用。

In my case, the device with a microcontroller (single chip computer) has only limited interfaces, where data are stored differently from a PC.就我而言,带有微控制器(单片机)的设备只有有限的接口,其中数据的存储方式与 PC 不同。 Hence, fs.readdir won't work.因此, fs.readdir 将不起作用。

Thanks to hints from @jfriend00, I managed to get the communication protocol, and succeeded in reading the data from the device with Nodejs.感谢@jfriend00的提示,我设法得到了通信协议,并成功地用Nodejs从设备中读取了数据。 Codes are given as an example below (for beginers like me).下面给出了代码作为示例(对于像我这样的初学者)。


//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');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM