@@ -19,6 +19,9 @@ const debugArgv = process.argv.indexOf('--debug') > -1;
1919const continuousArgv = process . argv . indexOf ( '--continuous' ) > - 1 ;
2020const rawArgv = process . argv . indexOf ( '--raw' ) > - 1 ;
2121
22+ let stdinAttached = false ;
23+ let serial : SerialConnector | undefined ;
24+
2225const configFactory = new Config ( ) ;
2326// tslint:disable-next-line:no-floating-promises
2427( async ( ) => {
@@ -59,11 +62,18 @@ function sleep(ms: number) {
5962 return new Promise ( ( res ) => setTimeout ( res , ms ) ) ;
6063}
6164
65+ function onStdIn ( data : Buffer ) {
66+ if ( ! serial ) return ;
67+
68+ // tslint:disable-next-line: no-floating-promises
69+ serial . write ( Buffer . from ( data . toString ( 'ascii' ) . trim ( ) + '\r\n' , 'ascii' ) ) ;
70+ }
71+
6272async function connectToSerial ( deviceId : string ) {
6373 // if this is set it means we have a connection
6474 let config : EiSerialDeviceConfig | undefined ;
6575
66- const serial = new SerialConnector ( deviceId , 115200 ) ;
76+ serial = new SerialConnector ( deviceId , 115200 ) ;
6777 const serialProtocol = new EiSerialProtocol ( serial ) ;
6878 serial . on ( 'error' , err => {
6979 console . log ( SERIAL_PREFIX , 'Serial error - retrying in 5 seconds' , err ) ;
@@ -102,17 +112,26 @@ async function connectToSerial(deviceId: string) {
102112 let inferenceStarted = false ;
103113
104114 serial . on ( 'data' , data => {
105- if ( ( serial . isConnected ( ) && inferenceStarted ) || rawArgv ) {
115+ if ( ( serial && serial . isConnected ( ) && inferenceStarted ) || rawArgv ) {
106116 process . stdout . write ( data . toString ( 'ascii' ) ) ;
107117 }
108118 } ) ;
109119 async function connectLogic ( ) {
110- if ( ! serial . isConnected ( ) ) {
120+ if ( serial && ! serial . isConnected ( ) ) {
111121 inferenceStarted = false ;
112122 return setTimeout ( serial_connect , 5000 ) ;
113123 }
114124
115- if ( rawArgv ) return ;
125+ if ( rawArgv ) {
126+ process . stdin . resume ( ) ;
127+ if ( ! stdinAttached ) {
128+ process . stdin . on ( 'data' , onStdIn ) ;
129+ stdinAttached = true ;
130+ }
131+
132+ console . log ( SERIAL_PREFIX , 'Connected to' , deviceId ) ;
133+ return ;
134+ }
116135
117136 config = undefined ;
118137 console . log ( SERIAL_PREFIX , 'Serial is connected, trying to read config...' ) ;
@@ -163,6 +182,8 @@ async function connectToSerial(deviceId: string) {
163182 serial . on ( 'connected' , connectLogic ) ;
164183
165184 async function serial_connect ( ) {
185+ if ( ! serial ) return ;
186+
166187 try {
167188 await serial . connect ( ) ;
168189 }
0 commit comments