The data
argument in your 'data' event handler is already a Buffer. By calling data.toString()
you are converting that Buffer to a (UTF-8 by default) string. You're probably better off keeping it as a Buffer and using that if you need the original binary data.
Node tcp socket: Read binary data from Enfora MT4000
-
23-07-2023 - |
题
I'm working with an Enfora MT4000 device. The device sends data to tcp or udp server when certain event has occurred. Data can be send in binary or ASCII format, but I need to use binary.
Enfora device is configured with AT commands like this:
AT$EVENT=14,0,7,1,1
AT$EVENT=14,3,52,14,1578098
When I configure the device with ASCII, the server receives data in this format:
r 13 0 0 $GPRMC,211533.00,A,3321.856934,S,07040.240234,W,0.0,0.0,120514,2.3,W,A*2B
But, when I use binary, the data looks like this:
$2K� �Dk����a�H
Anyone knows how Node.js can convert binary data from a socket? I'm trying to do this with a very simple script.
// server
require('net').createServer(function (socket) {
console.log("connected");
socket.setEncoding(null);
socket.on('data', function (data) {
console.log(data.toString());
});
})
.listen(3041);
thanks.
解决方案
不隶属于 StackOverflow