node js 调用python_如何从NodeJs调用python脚本

我需要在NodeJs中调用这个python脚本。

读取.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"

我使用了python shell,下面是NodeJs代码

测试.jsvar 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);

});

但当我运行这段代码时,在节点端没有看到任何东西。我认为当python脚本达到这个级别时就会出现问题。(status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)

因为我刚刚运行了while循环,它只有print语句,然后就可以工作了。在那之后,我尝试了另一种方法来实现这一点。但我有和上面一样的问题。这里有另一种方法

AltTest.jsvar 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...");

});

任何帮助都将不胜感激

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值