41 lines
1 KiB
Python
41 lines
1 KiB
Python
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"
|
|
UDP_PORT = 4444
|
|
|
|
connectedArduino = False
|
|
connectedWebSocket = False
|
|
|
|
while connectedWebSocket == False:
|
|
try:
|
|
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
|
|
sock.bind((UDP_IP,UDP_PORT))
|
|
connectedWebSocket = True
|
|
except:
|
|
print("please check you are able to open the socket on this system")
|
|
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(BEAMNG_METHODS.BEAMNG_DATA_FORMAT,data)
|
|
carData = BEAMNG_METHODS.unpackData(unpackedData)
|
|
kmh=carData["speed"]*3.6
|
|
toPi.write(str(kmh).encode()+":".encode())
|
|
|