简体   繁体   中英

Matlab Server with python client on raspberry pi BrokenPipeError: [Errno 32] Broken pipe

I am trying to gather data from sensors connected to a raspberry pi and send them over to my laptop for some processing. I have written a very basic matlab server (for on my laptop)

t = tcpip('127.0.0.1', 42069, 'NetworkRole', 'server');
fopen(t);

flag = true;
while flag
        if t.BytesAvailable > 0
            t.BytesAvailable
            raw_data = fread(t, t.BytesAvailable);
            data = typecast(uint8(raw_data), 'double');
            current_x = data(1);
            current_y = a.cell_size*a.rows - data(2);
            current_th = -data(3);
            flag = false;
        end
end
.
.
.
fclose(t)

On my PI I have written the following class to handle sending the data over.

class LocalizationClient():

    #TCP_IP = '127.0.0.1'
    TCP_IP = '192.168.1.5'
    TCP_PORT = 42069
    BUFFER_SIZE = 1000
    # MESSAGE = "Hello, World!"
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    def __init__(self):
        self.s.connect((self.TCP_IP, self.TCP_PORT))
        print("connected to server")


    def sendData(self,MESSAGE):
        try:
            self.s.send(MESSAGE)
        except BrokenPipeError as e:
            print(e)
            self.close()
            raise RuntimeError()

In my driver code I create an object and try to send a message like so:

lo_c = lc.LocalizationClient()
lo_c.sendData(np.array([float(x), float(y), float(th)]))
.
.
.
for x in range(50):

    measures = lidar.measures
    measures = np.append([float(dx), float(dy), float(dth)], measures)

    lo_c.sendData(measures)
    time.sleep(.2)


All of this was working fine and dandy while I was just testing with loopback on my laptop, but when I tried to put it on the pi I get the following error on the python client side:

connected to server [Errno 32] Broken pipe

Traceback (most recent call last):
  File "/home/pi/Desktop/EXAMPLES/LocalizationClient.py", line 21, in sendData
    self.s.send(MESSAGE)
BrokenPipeError: [Errno 32] Broken pipe

It seems that connecting still works but when I try to send to the server the client throws an error. The server seems fine and Im not trying to send any data back over to the client yet. I have been banging my head into this one for a while now and any help would be appreciated.

ps some details about the network setup if it helps, I have put the pi in ad-hoc mode with IP statically assigned as 192.168.1.1 and connected my macbook to the network it created. My Mac is assigned static IP 192.168.1.5.

So I figured out the issue. While I could see the port was open the way the matlab TCPIP object works is that it will silently refuse any connection that does not come from the IP passed into the constructor. So I was able to construct the object which would listen for a connection on the port. On the python side my client would see this "connect" but really the server was refusing the connection causing broken pipe. (this is also why it was working fine on local host but not remote)

Fix is easy just change:

t = tcpip('127.0.0.1', 42069, 'NetworkRole', 'server');
fopen(t);

to:

t = tcpip('0.0.0.0', 42069, 'NetworkRole', 'server');
fopen(t);

This will allow matlab server to accept a connection from any ip on LAN on specified port. Alternatively one could supply matlab server with clients IP to only accept connections from the one device but if IP is not static then it might change on you. Also is important to disable firewalls on devices.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM