1

我正在尝试将 BCG 传感器(Murata Sensor)连接到 wifi 网络并从中获取数据。我已经在假设一个名为“Wifi1”的 wifi 中配置了传感器,然后我试图从我的 Mac 上的传感器访问数据,该传感器也连接在 Wifi1 上。python 脚本首先尝试使用 ssdpSearch 函数查找节点,然后执行后面的代码。这是整个代码:

import socket 
import time 
import datetime

REPEAT = 10

def ssdpSearch():   
    print("Starting SSDP Search. 10 seconds.")   
    UDP_IP = '<broadcast>'   
    UDP_PORT = 2000   
    UDP_MESSAGE = '{"type":"SCS-DISCOVER","hostname":"Host-SCS"}'   
    networks = socket.gethostbyname_ex(socket.gethostname())[2] # Find all networks (i.e, wifi, wired)   
    sockets = []   
    for net in networks: # Connect to all networks
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
        sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) # Allow broadcast
        sock.bind((net, UDP_PORT)) # Connect
        sock.settimeout(1.0) # Set timeout (if no answer when reading)
        sockets.append(sock) # Save "sock" to sockets   
        timeStart = time.time()   
        devices = []   
        print('Found devices:')   
        time.sleep(0.1) 
        while time.time() - timeStart < REPEAT:
            for sock in sockets:
              try:
                sock.sendto(UDP_MESSAGE.encode(), (UDP_IP, UDP_PORT))
                data, addr = sock.recvfrom(1024)
                data = data.decode()
                data = data[1:].split(',')
                if data[0] == '"type":"SCS-NOTIFY"': # Only accept correct responses
                  oldDevice = 0
                  # print(data)
                  for dev in devices:
                    if dev[0] == data[1]:
                      oldDevice = 1
                  if not oldDevice:
                    devices.append([data[1],data[2]]) # Save found devices
                    print('\t' + data[1] + ' ' + data[2])
              except:
                1
            time.sleep(0.2) 
            if not len(devices):
                print('\t No devices found.')
                print('')
                for sock in sockets:
                    sock.close()

def readLine(s):
# Function to read status from BCG data
    line = s.recv(1024).decode()
    return line

def main():
    print('BCG Data Logger \n Logs either raw data or BCG algorithm data to a file \ depending on configured mode.')
    # Open file
    IP = input('Insert IP address (empty for SSDP): ')
    while len(IP) == 0:
        ssdpSearch()
        IP = input('Insert IP address (empty for SSDP): ')
    PORT = 8080
    filename = 'logged_data_' + str(datetime.datetime.now().strftime('%Y-%m-%d_%H-%M-%S')) + '.txt'
    fid = open(filename,'w')
    print('')
    print('Starting to read data. Press \"ctrl+c\" to quit.')
    while True:
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.settimeout(10)
            s.connect((IP, PORT))
            while True: #
                data = readLine(s)
                print(data,)
                fid.write(data)
        except (KeyboardInterrupt, SystemExit):
            print('Exiting program.')
            fid.close()
            break
        except (socket.timeout):
            print('Timed out, reconnecting.')
        except socket.error as msg:
            print(msg)
            print('Trying to reconnect.')

if __name__ == '__main__':
    main()

在输出中,它首先询问我的 IP 地址,但我不知道它询问的是哪个 IP 地址。如果它是 SSDP,我们可以将其留空,因此如果我将其留空,则会出现以下错误:

>Insert IP address (empty for SSDP): 
>Starting SSDP Search. 10 seconds.
>Traceback (most recent call last):
>File "DataCollection.py", line 91, in <module>
>main()
>File "DataCollection.py", line 64, in main
>ssdpSearch()
>File "DataCollection.py", line 17, in ssdpSearch
>networks = socket.gethostbyname_ex(socket.gethostname())[2] # Find all networks (i.e, wifi, wired)
>socket.gaierror: [Errno 8] nodename nor servname provided, or not known

谁能帮我理解这一点。

先感谢您。

4

0 回答 0