Re: [nodejs] Node.js-net socket - Sending frame

801 views
Skip to first unread message

Ben Noordhuis

unread,
Aug 8, 2012, 6:14:59 PM8/8/12
to nod...@googlegroups.com
On Wed, Aug 8, 2012 at 11:43 PM, trembl <scoobi...@hotmail.com> wrote:
> Hello,
>
> I have a problem concerning the implementation of a communication between a
> node server and a device. I use the library net in node that provides a
> communication port type TCP client .
>
> code:
>
> var net = require('net');
>
> var HOSTPLC = '192.168.45.120'; //Adresse de l'appareil.
> var PORTPLC = 9600; //Port de communication.
>
> var client = new net.Socket();
>
>
>
> The problem is that when I send my string for the specific communication
> protocol to my device using eg:
>
> code:
>
> client.write(sFinsCmdRead);
>
>
> The function automatically adds unwanted values to each 16 hex value
> (observed using a network protocol analyzer) that just create an error in
> the interpreter of the device. The value is sent C2:
>
> eg:
>>
>>
>> 46:49:4e:53:00:00:00:1a:00:00:00:02:00:00:00:00:c2:80:00:02:00:78:00:00:ef:bf:bd:00:00:01:01:c2:82:00:64:00:00:01
>
>
> This imply error in the communication protocol.
>
> My questions: Is there a way to define the size of frames sent or otherwise
> eliminate the C2 value in the code? To do this, where do I look (in the
> code) ?
>
> The initial objective was to communicate directly between the device and
> page hml5 but as it is not possible, I use the server node client side to
> interact with the device. Is the basic idea is valid
> or is there better way to implement this ?
>
> Thank you all for your help.

You didn't tell what type sFinsCmdRead is but I suspect it's a string
and you're running into an encoding issue. Use a buffer instead.

The reason I think that is because I see a ef:bf:bd in there, which is
the UTF-8 encoding for U+FFFD, the 'invalid character' replacement.

trembl

unread,
Aug 8, 2012, 10:44:20 PM8/8/12
to nod...@googlegroups.com
Tanks for the answer mr. Noordhuis,

it is effectively as string that i send wich is at the base an array of hex values eg:

//=====================================
    var mCmd = new Array(34);
    mCmd[0] = 0x46;
    mCmd[1] = 0x49;
    ...

         for (i = 0; i < 34; i++) {
        sFinsCmdRead= sFinsCmdRead+ String.fromCharCode(mCmd[i]);
        }
//=====================================

The question is how do i use buffer ? Is that as simples as :

//=====================================
buf = new Buffer(str.length);

for (var i = 0; i < str.length ; i++) {
  buf[i] = 0x00+i;
}

client.write(buf);
//=====================================


I will try tomorow if it is the case. 

What do you think about if using node.js is the  best way to achieve my purpose ?

Tell me if i'm not clear.

Thank again





Tim Caswell

unread,
Aug 9, 2012, 1:49:05 AM8/9/12
to nod...@googlegroups.com
NodeJS is great for sending binary data over a TCP socket. I do it
all the time. Only use strings if you want one of the string
encodings of your data. The default is "utf8" which disallows several
bit patterns. Buffers act somewhat like arrays of integers and let
you send exact bytes with full control.
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+un...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en

Ben Noordhuis

unread,
Aug 9, 2012, 10:49:38 AM8/9/12
to nod...@googlegroups.com
Yes, that should work. Like Tim said, buffers are essentially arrays
of integers in the range 0-255.

海圆张

unread,
Sep 4, 2014, 9:13:29 PM9/4/14
to nod...@googlegroups.com
...

var byteBuffer = new Buffer("8A830...", 'hex');
client.write(byteBuffer);
Reply all
Reply to author
Forward
0 new messages