added safety checking so the program doesnt crash when no arduino attached

This commit is contained in:
Seth Samuel 2024-11-10 16:09:51 +13:00
parent cec09d6bcb
commit e55927dbe2
2 changed files with 23 additions and 5 deletions

0
FORZA_METHODS Normal file
View file

View file

@ -1,17 +1,35 @@
import socket
import struct
import serial
import time
from GAME_METHODS import *
##import BEAMNG_FILES as BEAM
toPi=serial.Serial('COM5',115200) #connect to arduino
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))
print("waiting for data:\n")
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: