簡體   English   中英

Nodejs訪問本地網絡上的共享文件夾,根目錄問題

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

嘗試訪問本地網絡上的共享文件夾時,以下工作:

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

同時,以下給出錯誤

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'

} 

如果未指定子文件夾,Node 會將其視為本地 C: drive 盡管 \ 作為主機名 IP。

我嘗試了其他功能和方法,以及 Path 模塊。 都給出了相似的結果。

相關信息: 使用 node js 訪問本地網絡驅動器

任何人都可以幫忙嗎? 謝謝。

我最近幾天收集的信息:

  1. 路徑(上面的“temp2”)是“指針”指向的地方。
  2. 只有主機名(計算機名或 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');
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM