@@ -29,25 +29,57 @@ if (!plat.match(/^win/)) {
2929function RawSerialInNode ( n ) {
3030 RED . nodes . createNode ( this , n ) ;
3131 this . port = n . port ;
32- this . split = n . split || null ;
33- if ( this . split == '\\n' ) this . split = "\n" ;
34- if ( this . split == '\\r' ) this . split = "\r" ;
32+ this . splitc = n . splitc || null ;
33+ this . out = n . out || "char" ;
34+ this . bin = n . bin || false ;
35+ if ( this . splitc == '\\n' ) this . splitc = "\n" ;
36+ if ( this . splitc == '\\r' ) this . splitc = "\r" ;
37+ if ( ! isNaN ( parseInt ( this . splitc ) ) ) { this . splitc = parseInt ( this . splitc ) ; }
38+ console . log ( "Split is" , this . out , this . splitc ) ;
3539 var node = this ;
3640
3741 var setupSerial = function ( ) {
3842 node . inp = fs . createReadStream ( pre + node . port ) ;
3943 node . log ( "opened " + pre + node . port ) ;
40- node . inp . setEncoding ( 'utf8' ) ;
44+ node . tout = null ;
4145 var line = "" ;
46+ var buf = new Buffer ( 32768 ) ;
47+ var i = 0 ;
4248 node . inp . on ( 'data' , function ( data ) {
43- if ( node . split != null ) {
44- if ( data == node . split ) {
45- node . send ( { payload :line } ) ;
46- line = "" ;
49+ for ( var z = 0 ; z < data . length ; z ++ ) {
50+ if ( ( node . out === "time" ) && ( node . splitc != 0 ) ) {
51+ if ( node . tout ) {
52+ i += 1 ;
53+ buf [ i ] = data [ z ] ;
54+ }
55+ else {
56+ node . tout = setTimeout ( function ( ) {
57+ node . tout = null ;
58+ var m = new Buffer ( i + 1 ) ;
59+ buf . copy ( m , 0 , 0 , i + 1 ) ;
60+ if ( node . bin !== "true" ) { m = m . toString ( ) ; }
61+ node . send ( { "payload" : m } ) ;
62+ } , node . splitc ) ;
63+ i = 0 ;
64+ buf [ 0 ] = data [ z ] ;
65+ }
66+ }
67+ else if ( ( node . out == "char" ) && ( node . splitc != null ) ) {
68+ buf [ i ] = data [ z ] ;
69+ i += 1 ;
70+ if ( ( data [ z ] === node . splitc . charCodeAt ( 0 ) ) || ( i === 32768 ) ) {
71+ var m = new Buffer ( i ) ;
72+ buf . copy ( m , 0 , 0 , i ) ;
73+ if ( node . bin !== "true" ) { m = m . toString ( ) ; }
74+ node . send ( { "payload" :m } ) ;
75+ i = 0 ;
76+ }
77+ }
78+ else {
79+ if ( node . bin !== "true" ) { node . send ( { "payload" : String . fromCharCode ( data [ z ] ) } ) ; }
80+ else { node . send ( { "payload" : new Buffer ( [ data [ z ] ] ) } ) ; }
4781 }
48- else { line += data ; }
4982 }
50- else { node . send ( { payload :data } ) ; }
5183 } ) ;
5284 //node.inp.on('end', function (error) {console.log("End", error);});
5385 node . inp . on ( 'close' , function ( error ) {
0 commit comments