Wie aufrufen, python script von NodeJs
Muss ich anrufen das python-Skript in NodeJs.
Read.py
#!/usr/bin/env python
# -*- coding: utf8 -*-
import RPi.GPIO as GPIO
import MFRC522
import signal
continue_reading = True
# Capture SIGINT for cleanup when the script is aborted
def end_read(signal,frame):
global continue_reading
print "Ctrl+C captured, ending read."
continue_reading = False
GPIO.cleanup()
# Hook the SIGINT
signal.signal(signal.SIGINT, end_read)
# Create an object of the class MFRC522
MIFAREReader = MFRC522.MFRC522()
# Welcome message
print "Welcome to the MFRC522 data read example"
print "Press Ctrl-C to stop."
# This loop keeps checking for chips. If one is near it will get the UID and authenticate
while continue_reading:
# Scan for cards
(status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
# If a card is found
if status == MIFAREReader.MI_OK:
# Get the UID of the card
(status,uid) = MIFAREReader.MFRC522_Anticoll()
# If we have the UID, continue
if status == MIFAREReader.MI_OK:
# Print UID
print "Card read UID: "+str(uid[0])+","+str(uid[1])+","+str(uid[2])+","+str(uid[3])
# This is the default key for authentication
key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
# Select the scanned tag
MIFAREReader.MFRC522_SelectTag(uid)
# Authenticate
status = MIFAREReader.MFRC522_Auth(MIFAREReader.PICC_AUTHENT1A, 8, key, uid)
# Check if authenticated
if status == MIFAREReader.MI_OK:
MIFAREReader.MFRC522_Read(8)
MIFAREReader.MFRC522_StopCrypto1()
else:
print "Authentication error"
Früher habe ich python-shell, hier ist die NodeJs-code für die
Test.js
var PythonShell = require('python-shell');
var options = {
scriptPath: '/home/pi/gpio-admin/MFRC522-python/'
};
var pyshell = new PythonShell('Read.py',options);
pyshell.on('message', function (message) {
console.log(message);
});
Aber bei mir lief dieser code, den ich nicht sehen, nichts in der Node Seite. Ich denke, dass problem tritt auf, wenn python-Skript kommt auf dieser Ebene.
(status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
Weil ich lief einfach mit while-Schleife, die nur print-Anweisung, dann funktioniert es. Danach habe ich versucht einen anderen Weg, dies zu erreichen. Aber ich habe dasselbe problem, die ich mit oben.Hier ist eine weitere Methode
AltTest.js
var python = require('child_process').spawn(
'python',
//second argument is array of parameters, e.g.:
["/home/pi/gpio-admin/MFRC522-python/Read.py"]
);
var output = "";
python.stdout.on('data', function(){
output += data ;
console.log(data);
});
python.on('close', function(code){
console.log("Here you are there...");
});
Jede Hilfe würde geschätzt
InformationsquelleAutor GPrathap | 2015-06-07
Du musst angemeldet sein, um einen Kommentar abzugeben.
Gibt es mehrere Möglichkeiten, dies zu tun.
npm install python-shell
und hier ist der code
können Sie eine Nachricht senden, die python-shell mit
pyshell.send('hello');
finden Sie die API-Referenz hier-
https://github.com/extrabacon/python-shell
zweite Möglichkeit - ein anderes Paket, Sie können finden die Knoten in python , müssen Sie
npm install node-python
Dritte Möglichkeit - Sie können sich auf diese Frage, wo finden Sie ein Beispiel für die Verwendung eines child-Prozesses-
Gewusst wie: aufrufen externer Skripte/Programme aus node.js
ein paar mehr Referenzen
https://www.npmjs.com/package/python
wollen Sie service-orientierte Architektur -
http://ianhinsdale.com/code/2013/12/08/communicating-between-nodejs-and-python/
InformationsquelleAutor Tejus Prasad
install python-shell :- npm install python-shell
Index.js
test.py
InformationsquelleAutor Tanmay Shrivastava