added auto detection of system to set serial port and added ability to change serial port

This commit is contained in:
Seth Samuel 2024-11-11 23:55:45 +13:00
parent cb8c861407
commit 61160fcd70
7 changed files with 19 additions and 6 deletions

Binary file not shown.

Binary file not shown.

View file

@ -1,16 +1,18 @@
#please use python 3.11
import socket import socket
import struct import struct
import serial import serial
import time import time
import csv import platform
from enum import Enum from enum import Enum
from GAME_METHODS import * from GAME_METHODS import *
runningOs = platform.system()
COM_PORT = 'COM5'
UDP_IP = "127.0.0.1" UDP_IP = "127.0.0.1"
BEAMNG_UDP_PORT = 4444 BEAMNG_UDP_PORT = 4444
FORZA_UDP_PORT= 4843 FORZA_UDP_PORT= 4843
SERIAL_PORT = ""
class GameType(Enum): class GameType(Enum):
NONE=0, NONE=0,
@ -26,9 +28,19 @@ carData = 0
#code runs from here<----------------------------------------------------------------------------------------------------------- #code runs from here<-----------------------------------------------------------------------------------------------------------
if runningOs == 'Windows':
print("Windows detacted setting serial port to 'COM5'\n")
SERIAL_PORT= 'COM5'
elif runningOs == 'Linux':
print("Linux detected setting serial port to '/dev/ttyAMC0'\n")
SERIAL_PORT = '/dev/ttyACM0'
else:
print("OS detection failed setting serial port to 'COM5'\n")
SERIAL_PORT = 'COM5'
#select game #select game
while gameSelected == False: while gameSelected == False:
gameNo = input("1:BEAMNG\n2:FORZA\n") gameNo = input("1:BEAMNG\n2:FORZA\n\n\n9:SET SERIAL PORT ("+SERIAL_PORT+")\n")
if gameNo == "1": if gameNo == "1":
portToConnect = BEAMNG_UDP_PORT portToConnect = BEAMNG_UDP_PORT
gameSelected = True gameSelected = True
@ -39,7 +51,8 @@ while gameSelected == False:
gameType=GameType.FORZA gameType=GameType.FORZA
gameSelected=True gameSelected=True
print("Forza Selected") print("Forza Selected")
elif gameNo == "9":
SERIAL_PORT = input("set serial port: ")
else: else:
print("please select a number from the list") print("please select a number from the list")
@ -55,10 +68,10 @@ while connectedWebSocket == False:
while connectedArduino == False: while connectedArduino == False:
try: try:
toPi=serial.Serial(COM_PORT,115200) #connect to arduino toPi=serial.Serial(SERIAL_PORT,115200) #connect to arduino
connectedArduino = True connectedArduino = True
except: except:
print("please check connection to arduino and verify the correct COM port") print("please check connection to arduino and verify the correct serial port")
time.sleep(1) time.sleep(1)
csvOut = False csvOut = False