import socket import struct import serial import time from GAME_METHODS import * ##import BEAMNG_FILES as BEAM COM_PORT = 'COM5' UDP_IP = "127.0.0.1" BEAMNG_UDP_PORT = 4444 FORZA_UDP_PORT= 4843 connectedArduino = False connectedWebSocket = False gameSelected = False portToConnect= 0 dataFormat = "" while gameSelected == False: gameNo = input("1:BEAMNG\n2:FORZA\n") if gameNo == "1": portToConnect = BEAMNG_UDP_PORT dataFormat =BEAMNG_METHODS.BEAMNG_DATA_FORMAT gameSelected = True print("BeamNG Selected") elif gameNo == "2": portToConnect = FORZA_UDP_PORT dataFormat = FORZA_METHODS.FROZA_DATA_FORMAT print("Forza Selected") gameSelected=True else: print("please select a number from the list") while connectedWebSocket == False: try: sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) sock.bind((UDP_IP,portToConnect)) connectedWebSocket = True except: print("please check you are able to open the socket on this system\ntired to open port: "+portToConnect) exit() while connectedArduino == False: try: toPi=serial.Serial(COM_PORT,115200) #connect to arduino connectedArduino = True except: print("please check connection to arduino and verify the correct COM port") time.sleep(1) print("ready:\n") while True: data, addr = sock.recvfrom(1024) unpackedData = struct.unpack(dataFormat,data) carData = BEAMNG_METHODS.unpackData(unpackedData) kmh=carData["speed"]*3.6 toPi.write(str(kmh).encode()+":".encode())