我有一个程序写入处理(处理实时音频)和用python编写的程序(调用gatttool通过蓝牙低功耗与外设通话)。有没有一种直接的方法将值从处理发送到python?我应该创建一个串行连接并以这种方式传递字节吗?
答案 0 :(得分:11)
请记住,它们在同一台计算机上运行,最好使用套接字在Python端创建服务器,在处理端创建客户端,并将数据从客户端发送到服务器。 Python服务器将等待来自Processing客户端的连接,并在收到数据后使用它们。
您可以在网络上找到示例等,但以下是Proccessing和Python文档提供的示例:
import processing.net.*;
Client myClient;
void setup() {
size(200, 200);
/* Connect to the local machine at port 5204
* (or whichever port you choose to run the
* server on).
* This example will not run if you haven't
* previously started a server on this port.
*/
myClient = new Client(this, "127.0.0.1", 5204);
}
void draw() {
myClient.write("Paging Python!"); // send whatever you need to send here
}
# Echo server program
import socket
HOST = '' # Symbolic name meaning all available interfaces
PORT = 50007 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print('Connected by', addr)
while True:
data = conn.recv(1024)
if not data: break
print(data) # Paging Python!
# do whatever you need to do with the data
conn.close()
# optionally put a loop here so that you start
# listening again after the connection closes