车辆2车辆通信代码

时间:2016-03-16 12:46:17

标签: python

我编写了一个代码,用于在两台自动驾驶汽车之间实现V2V通信。但是,它没有按预期工作。控制器基本上使用航点列表在另一个代码中调用发送数据方法。然后,其他端口接收数据。但是m个端口根本没有接收数据。

在控制器代码中,我让每辆车都成了一个线程。因此,每个线程都访问此类。我希望我在这里解释得恰到好处。

 class V2V_comm():
        global ports
        def __init__(self,port):
            self.host = "127.0.0.1"
            self.port = port
            self.skt = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
            self.skt.bind((self.host,self.port))
            self.skt.settimeout(1)
            self.loc_info = []
            self.receiving_data()

        def sending_data(self,wp):
            try:
                message = wp
                for port in ports:
                    if port != self.port:
                        print "Sending location of: ", self.port

                        self.skt.sendto(str(message),("127.0.0.1",port))
            except:
                print "Unable to send message from: ", self.port

        def receiving_data(self):

            try:
                print "Trying to receive data at: ", self.port
                data,addr = self.skt.recvfrom(1024)
                #print "The points received are: ", str(data)
                #print "The points are received at: ", self.port
                self.loc_info.append(data)
                print self.loc_info, "for port: ", self.port

            except:
                print "Unable to receive message at: ", self.port

0 个答案:

没有答案