added some if statements such that when connecting to a serial device, the for loop only tries to connect until a connection is made