我的蓝牙代码返回错误值时遇到问题

时间:2019-02-09 18:28:58

标签: python arrays bluetooth multiprocessing

我创建了一个测试程序,该程序将合并到涉及多处理的主项目中。我正在使用多处理数组来存储通过蓝牙发送到的值,并将其传递给另一个进程,但是蓝牙发送的变量而是发送随机字符串,而带有定义值的变量则被正确传递并打印正确的值。

import multiprocessing
import time
import os
import sys
#import bluetooth
from bluetooth import*
from ctypes import*
#Make device discoverable
#os.system("hciconfig hci0 piscan")

#Create a new server 
server_socket = BluetoothSocket(RFCOMM)

#Bind to any port
server_socket.bind(("", PORT_ANY))

#Start listening
server_socket.listen(1)

#Get the port the service is listening
port = server_socket.getsockname()[1]
#The Service UUID to advertise
uuid = "00001101-0000-1000-8000-00805F9B34FB"
advertise_service(server_socket, "Camaro",
                  service_id=uuid,
                  service_classes=[uuid, SERIAL_PORT_CLASS],
                  profiles=[SERIAL_PORT_PROFILE])

#Items in queue
def bt(a,z):
    while True:
        print("Waitng for connection on rfcomm channel %d" %port)
        try:
            client_socket = None
            client_socket, client_info = server_socket.accept()
            print("Accepted connection from", client_info)
            while client_socket != None:
                data=client_socket.recv(1024)
                a[1]=data
                print(data)
                a[0]=b'test'
                z.set()
        except IOError:
            pass
def ret(b,y):
    while(True):
        if y.is_set():
            print("Bluetooth sent: ",b[:])
            y.clear()
d = multiprocessing.Event()
t = multiprocessing.Array(c_char_p,[b'',b''])
bluetooth_thread=multiprocessing.Process(target=bt, args=(t,d))
bt1=multiprocessing.Process(target=ret, args=(t,d))
bluetooth_thread.start()
bt1.start()

这是我在测试程序时得到的:

linaro@tinkerboard:~/Documents$ python3 Blue.py
Waitng for connection on rfcomm channel 1
Accepted connection from ('08:AE:D6:09:DC:D3', 1)
b'Hello'
Bluetooth sent:  [b'test', b'9{b_\x8bT\x8eo\xa2Gw~+:\x9bN']

0 个答案:

没有答案