Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
让XBee在Node.js启动的python脚本中连接_Python_Html_Css_Node.js_User Interface - Fatal编程技术网

让XBee在Node.js启动的python脚本中连接

让XBee在Node.js启动的python脚本中连接,python,html,css,node.js,user-interface,Python,Html,Css,Node.js,User Interface,我正在使用Python脚本连接到Xbee。我已经设法让这个脚本正常工作,并在从终端执行python脚本时连接到Xbee。我现在尝试使用名为“PythonShell”的npm包从Node.js执行脚本 PythonShell运作良好。当我运行其他脚本时,它工作正常,消息从pythonshell发送回node 当我尝试运行下面的脚本时,我似乎没有得到任何回报,或者似乎什么都没有发生 Python脚本: ''' --------------------------------------------

我正在使用Python脚本连接到Xbee。我已经设法让这个脚本正常工作,并在从终端执行python脚本时连接到Xbee。我现在尝试使用名为“PythonShell”的npm包从Node.js执行脚本

PythonShell运作良好。当我运行其他脚本时,它工作正常,消息从pythonshell发送回node

当我尝试运行下面的脚本时,我似乎没有得到任何回报,或者似乎什么都没有发生

Python脚本:


'''
-----------------------------------------------
|                                             |
|       RF Receive Data Python Script         |
|                                             |
-----------------------------------------------

Script used to connect to the XBee RF module to receive
incoming data and to save to separate csv files.

'''


from digi.xbee.devices import XBeeDevice
import time
from csv import writer
import datetime
import sys

baud_rate = 115200
COM_port = "/dev/ttyUSB0"

if sys.argv[1] != None and sys.argv[2] != None:
    baud_rate = sys.argv[1]
    COM_port = sys.argv[2]
    print(baud_rate)
    print(COM_port)


#Order of the CSV files and data variables

'''
AMS - AMS_Data.csv
ECU - ECU_Data.csv
INV - INV_Data.csv
POS - POS_Data.csv

data vars  | csv index ranges
̅̅̅‾‾‾‾‾‾‾‾‾‾‾|‾‾‾‾‾‾‾‾‾‾‾‾‾
AMS        |  [0] - [34]
ECU        |  [35] - [49]
INV        |  [50] - [96]
POS        |  [97] - 

'''

#XBee setup and config

print("connecting to Xbee device at")
print(COM_port)

device = XBeeDevice("/dev/ttyUSB0", baud_rate) #COM port and Baud rate

#device.open() #Open connection with the Xbee

device.execute_command("AP", bytearray('\x01', 'utf8'))
device.set_parameter("BD", bytearray('\x07', 'utf8'))
print("Connected to the Xbee Successfully!")
#Add a callback for when the XBee receives data
def my_data_received_callback(xbee_message):
    address = xbee_message.remote_device.get_64bit_addr()
    data = xbee_message.data.decode('utf8')
    #add a timestamp which will be appended to each of the files
    time = str(datetime.datetime.now().time()).split('.')[0] + "." + str(datetime.datetime.now().time()).split('.')[1][0:3]
    #Split the data up using the ',' delimiter
    message = data.split(",")

    #Now save data into the files based on the ranges shown in the table above
    #AMS Data being saved into the AMS_Data.csv file
    AMS_File = open("../public/AMS_Data.csv", "a")
    AMS_CSV_Writer = writer(AMS_File)
    AMS_array = message[0:34]
    AMS_array.append(time) #Append the time stamp to the end of the array
    AMS_CSV_Writer.writerow([i for i in AMS_array])
    AMS_File.close()

    #ECU Data being saved into the ECU_Data.csv file

    ECU_File = open("../public/ECU_Data.csv", "a")
    ECU_CSV_Writer = writer(ECU_File)
    ECU_array = message[35:49]
    ECU_array.append(time)
    ECU_CSV_Writer.writerow([i for i in ECU_array])
    ECU_File.close()

    #INV data being saved into the INV_Data.csv file

    INV_File = open("../public/INV_Data.csv", "a")
    INV_CSV_Writer = writer(INV_File)
    INV_array = message[50:96]
    INV_array.append(time)
    INV_CSV_Writer.writerow([i for i in INV_array])
    INV_File.close()

    #Need to add the POS and PRI data saving here!

#Add the callback to the device

device.add_data_received_callback(my_data_received_callback)

while True:
    time.sleep(0.01)
但是,如果我注释掉device.open(),那么我会设法将一些消息返回到Node.js,但是它们是错误消息,表示“NoneType”对象没有属性“is_op_mode_valid”。我认为这个错误是因为Xbee没有连接。因此,device.open()命令似乎暂停了python程序,不允许它将任何消息发送回node.js

以下是我的Node.js代码:


app.post("/xbee/connect", function(req, res){
    var baudRate = req.body.baud_rate;
    var COMport = req.body.com_port;
    var XbeeID;
    
    if(xbee_connected === false && testing_status === false){
        xbeeShell = new PythonShell('./python_scripts/receive-telem.py', {args:[baudRate, COMport]});
        xbee_connected = true;
    }
    xbeeShell.on('message', function(message){
        io.to('Data-link Room').emit('log-data', {data:message});
    })
    xbeeShell.end(function(err) {
        if(err){
            console.log(err)
            io.to('Data-link Room').emit('log-data', {data:String(err)});
            xbee_connected = false;
        }
    })
    res.send("success");
})


如果您有任何帮助,我们将不胜感激。

为了使此功能正常工作

sys.stdout.flush()
在while循环中的某个地方需要。这是因为程序进入了while循环。因此,在循环的某个地方,需要flush命令来刷新标准输出