简体   繁体   中英

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.

I tried other functions and methods, as well as Path module. All give similar results.

related Info: Use node js to access a local network drive

Can anyone help? Thanks.

Information I collected in the last days:

  1. Path ("temp2" above) is the place that the "pointer" points to.
  2. Only hostname (computer name, or ip) the "pointer" won't work.

In my case, the device with a microcontroller (single chip computer) has only limited interfaces, where data are stored differently from a PC. Hence, fs.readdir won't work.

Thanks to hints from @jfriend00, I managed to get the communication protocol, and succeeded in reading the data from the device with 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');
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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