Saturday 15 April 2017

How read lock status in NodeJs?

I am running an external python code in NodeJs. The python code send command to the usb controller which interacts with a lock. The commands can be one of the following three:

  • Open
  • Close
  • Status

I am able to succesfully send open/close command, but I am trying to see how to work out the status command. The status command simply state the status of the current lock such as open3, or close2, but i need to grab that information and figure out a way to display it to console once that command is issued.

Below is how the open/close command are sent:

 var comport = rows[i].comport;
          var command = "open" + rows[i].cubby_id;
          var commandClose = "close" + rows[i].cubby_id;


          console.log(command);
          console.log(comport);


          var options = {
            scriptPath: 'python/scripts',
            args: [command, comport, commandClose] // pass arguments to the script here

          };


          PythonShell.run('controlLock.py', options, function (err, results) {
            if (err) throw err;
            console.log('results: %j', results);
          });

I tried sending the status command, but the results return is null.

Below, are the possible command that can be sent. enter image description here

Below is the controlLock.py (without status)

import serial
import sys
import time

# 0 is the script itself, technically an argument to python
script = sys.argv[0]

# 1 is the command arg you passed
command = sys.argv[1]

# 2 is the comport arg you passed
comport = sys.argv[2]

commandClose = sys.argv[3]


ser = serial.Serial()
ser.baudrate = 38400 #Suggested rate in Southco documentation, both locks and program MUST be at same rate 
ser.port = "COM{}".format(comport) 
ser.timeout = 5 
ser.open() 
#call the serial_connection() function 
ser.write(("%s\r\n"%command).encode('ascii'))
time.sleep(5)
ser.write(("%s\r\n"%commandClose).encode('ascii'))



via John

No comments:

Post a Comment