mirror of
https://github.com/YikeStone/ros_arduino_bridge.git
synced 2025-08-05 20:14:07 +05:30
Added fix to arduino_driver.py so that serial port now opens consistently.
This commit is contained in:
parent
6a8f9413b9
commit
896e60d417
@ -27,7 +27,7 @@ import os
|
|||||||
import time
|
import time
|
||||||
import sys, traceback
|
import sys, traceback
|
||||||
from serial.serialutil import SerialException
|
from serial.serialutil import SerialException
|
||||||
from serial import Serial
|
import serial
|
||||||
|
|
||||||
class Arduino:
|
class Arduino:
|
||||||
''' Configuration Parameters
|
''' Configuration Parameters
|
||||||
@ -59,12 +59,21 @@ class Arduino:
|
|||||||
def connect(self):
|
def connect(self):
|
||||||
try:
|
try:
|
||||||
print "Connecting to Arduino on port", self.port, "..."
|
print "Connecting to Arduino on port", self.port, "..."
|
||||||
self.port = Serial(port=self.port, baudrate=self.baudrate, timeout=self.timeout, writeTimeout=self.writeTimeout)
|
|
||||||
# The next line is necessary to give the firmware time to wake up.
|
# The port has to be open once with the default baud rate before opening again for real
|
||||||
time.sleep(1)
|
self.serial_port = serial.Serial(port=self.port)
|
||||||
|
|
||||||
|
# Needed for Leonardo only
|
||||||
|
while not self.port:
|
||||||
|
time.sleep(0.5)
|
||||||
|
|
||||||
|
# Now open the port with the real settings
|
||||||
|
self.serial_port = serial.Serial(port=self.port, baudrate=self.baudrate, timeout=self.timeout, writeTimeout=self.writeTimeout)
|
||||||
|
|
||||||
|
# Test the connection by reading the baudrate
|
||||||
test = self.get_baud()
|
test = self.get_baud()
|
||||||
if test != self.baudrate:
|
if test != self.baudrate:
|
||||||
time.sleep(1)
|
time.sleep(0.5)
|
||||||
test = self.get_baud()
|
test = self.get_baud()
|
||||||
if test != self.baudrate:
|
if test != self.baudrate:
|
||||||
raise SerialException
|
raise SerialException
|
||||||
@ -82,18 +91,18 @@ class Arduino:
|
|||||||
def open(self):
|
def open(self):
|
||||||
''' Open the serial port.
|
''' Open the serial port.
|
||||||
'''
|
'''
|
||||||
self.port.open()
|
self.serial_port.open()
|
||||||
|
|
||||||
def close(self):
|
def close(self):
|
||||||
''' Close the serial port.
|
''' Close the serial port.
|
||||||
'''
|
'''
|
||||||
self.port.close()
|
self.serial_port.close()
|
||||||
|
|
||||||
def send(self, cmd):
|
def send(self, cmd):
|
||||||
''' This command should not be used on its own: it is called by the execute commands
|
''' This command should not be used on its own: it is called by the execute commands
|
||||||
below in a thread safe manner.
|
below in a thread safe manner.
|
||||||
'''
|
'''
|
||||||
self.port.write(cmd + '\r')
|
self.serial_port.write(cmd + '\r')
|
||||||
|
|
||||||
def recv(self, timeout=0.5):
|
def recv(self, timeout=0.5):
|
||||||
timeout = min(timeout, self.timeout)
|
timeout = min(timeout, self.timeout)
|
||||||
@ -105,7 +114,7 @@ class Arduino:
|
|||||||
value = ''
|
value = ''
|
||||||
attempts = 0
|
attempts = 0
|
||||||
while c != '\r':
|
while c != '\r':
|
||||||
c = self.port.read(1)
|
c = self.serial_port.read(1)
|
||||||
value += c
|
value += c
|
||||||
attempts += 1
|
attempts += 1
|
||||||
if attempts * self.interCharTimeout > timeout:
|
if attempts * self.interCharTimeout > timeout:
|
||||||
@ -148,7 +157,7 @@ class Arduino:
|
|||||||
self.mutex.acquire()
|
self.mutex.acquire()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.port.flushInput()
|
self.serial_port.flushInput()
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -156,12 +165,12 @@ class Arduino:
|
|||||||
attempts = 0
|
attempts = 0
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.port.write(cmd + '\r')
|
self.serial_port.write(cmd + '\r')
|
||||||
value = self.recv(self.timeout)
|
value = self.recv(self.timeout)
|
||||||
while attempts < ntries and (value == '' or value == 'Invalid Command' or value == None):
|
while attempts < ntries and (value == '' or value == 'Invalid Command' or value == None):
|
||||||
try:
|
try:
|
||||||
self.port.flushInput()
|
self.serial_port.flushInput()
|
||||||
self.port.write(cmd + '\r')
|
self.serial_port.write(cmd + '\r')
|
||||||
value = self.recv(self.timeout)
|
value = self.recv(self.timeout)
|
||||||
except:
|
except:
|
||||||
print "Exception executing command: " + cmd
|
print "Exception executing command: " + cmd
|
||||||
@ -180,7 +189,7 @@ class Arduino:
|
|||||||
self.mutex.acquire()
|
self.mutex.acquire()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.port.flushInput()
|
self.serial_port.flushInput()
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -188,12 +197,12 @@ class Arduino:
|
|||||||
attempts = 0
|
attempts = 0
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.port.write(cmd + '\r')
|
self.serial_port.write(cmd + '\r')
|
||||||
values = self.recv_array()
|
values = self.recv_array()
|
||||||
while attempts < ntries and (values == '' or values == 'Invalid Command' or values == [] or values == None):
|
while attempts < ntries and (values == '' or values == 'Invalid Command' or values == [] or values == None):
|
||||||
try:
|
try:
|
||||||
self.port.flushInput()
|
self.serial_port.flushInput()
|
||||||
self.port.write(cmd + '\r')
|
self.serial_port.write(cmd + '\r')
|
||||||
values = self.recv_array()
|
values = self.recv_array()
|
||||||
except:
|
except:
|
||||||
print("Exception executing command: " + cmd)
|
print("Exception executing command: " + cmd)
|
||||||
@ -218,7 +227,7 @@ class Arduino:
|
|||||||
self.mutex.acquire()
|
self.mutex.acquire()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.port.flushInput()
|
self.serial_port.flushInput()
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -226,12 +235,12 @@ class Arduino:
|
|||||||
attempts = 0
|
attempts = 0
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.port.write(cmd + '\r')
|
self.serial_port.write(cmd + '\r')
|
||||||
ack = self.recv(self.timeout)
|
ack = self.recv(self.timeout)
|
||||||
while attempts < ntries and (ack == '' or ack == 'Invalid Command' or ack == None):
|
while attempts < ntries and (ack == '' or ack == 'Invalid Command' or ack == None):
|
||||||
try:
|
try:
|
||||||
self.port.flushInput()
|
self.serial_port.flushInput()
|
||||||
self.port.write(cmd + '\r')
|
self.serial_port.write(cmd + '\r')
|
||||||
ack = self.recv(self.timeout)
|
ack = self.recv(self.timeout)
|
||||||
except:
|
except:
|
||||||
print "Exception executing command: " + cmd
|
print "Exception executing command: " + cmd
|
||||||
|
Loading…
x
Reference in New Issue
Block a user