Python Socket TCP错误32:使用“ sendall”时管道损坏

时间:2019-07-17 19:38:52

标签: python sockets tcp fpga

我使用Python语言通过TCP套接字在RaspberryPi(RPi)和FPGA之间建立了连接。首先,我将从RPi向FPGA发送数据模式。之后,我将从FPGA接收回数据,并将数据与某个值“ data_compare”进行比较,以获取许多错误。

问题是,如代码中所示,开始时,代码将运行良好并生成正确的错误数据,但是经过5轮之后,它将在第67行停止,错误32:管道损坏。我真的不明白。

另一件事是,如果我使用“ while”循环将整个代码括起来,这意味着将“ while”放在“ import”之后,代码也将运行良好。我不想这样做的原因是这样,代码会占用更多时间。

# This code is for SRAM by MCU communication

import socket
import time
import sys

port_MCU = 5000
Ipaddr_MCU = '192.168.0.123'

ser_MCU = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#ser_MCU.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE,1)
ser_MCU.connect((Ipaddr_MCU,port_MCU))

RX = [0]*2056

while True:

#    port_MCU = 5000
#    Ipaddr_MCU = '192.168.0.123'
    buffer_size = 1

    #server_UDPsocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    #server_UDPsocket.bind(('', 61557))

#    ser_MCU = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#    ser_MCU.connect((Ipaddr_MCU,port_MCU))

    #data_pattern can be 0, 1, 10 or 11
    data_pattern = 0

    #data_compare is used to test if there is an error
    data_compare = 0

    #test_round is the number that how many rounds the testing code will run. It should be in range of [0,255]
    #test_round = 0

    try:

        print('Connection to MCU has been built!')
        TX = [0]*6
    #   RX = [None]*2056

    # send configuration info to fpga

        TX[0] = 85
        if (data_pattern == 0):
                TX[1] = 0
                data_compare = 0
        elif (data_pattern == 1):
                TX[1] = 1
                data_compare = 85
        elif (data_pattern == 10):
                TX[1] = 2
                data_compare = 170
        elif (data_pattern == 11):
                TX[1] = 3
                data_compare = 255

        test_round = 1

        TX[2] = test_round

        print("the configuration list TX is: " + str(TX))

    #   ser_MCU.send(b'8501000')

        ser_MCU.sendall(bytes(TX))
    #   for data in TX:
    #       ser_MCU.send(bytes(data))
    #        message, address = server_UDPsocket.recvfrom(64)
            #message = message.upper()
    #        print('The received info is %s.'%message)

    #        if message == b'yes':

        TX[0] = 31
        print('The trigger configuration list is: ' + str(TX))

        ser_MCU.sendall(bytes(TX))

        err_sum = 0


        for i in range (2056):        
            rx = ser_MCU.recv(1)

    #        rx_int = ord(rx)
            if len(rx) == 1:
                RX[i] = ord(rx)
            #RX[i] = int.from_bytes(rx,'big')
    #       print(rx)
        for i in range (2056):
            if(i != 0 and i%257 != 0):
                err_number = RX[i]^ data_compare
                err_number_binary = bin(err_number).count('1')
                err_sum = err_number_binary + err_sum



        print('The total number of generated errors is: ' + str(err_sum))


        time.sleep(1)



    except KeyboardInterrupt:
        ser_MCU.shutdown()
        ser_MCU.close()

        print('Connection is closed!')

我要寻找的是保持代码稳定运行,直到关闭tcp连接为止。

欢迎任何评论,并对任何建议深表感谢。

0 个答案:

没有答案