Code: Select all
___ ___ 3V3 ---|___|---. .---|___|--- 3V3 ___ | | ___ RX <---|___|---^---< <---. .---^---|___|---> RX X TX >---------------> >---' `---------------< TX 0V ----------------> >---------------------- 0V
Code: Select all
from serial import * import time class CommPort: ''' commPort class ''' def __init__(self, baud=19200, sendInit=False, bytesize=EIGHTBITS): self.dev = '/dev/ttyAMA0' self.baudRate = baud # default self.byteSize = bytesize self.parity = PARITY_NONE # this is pySerial default anyway self.stopBits = STOPBITS_ONE # also pySerial default self.timeOut = 3 self.message = '' try: self.serPort = Serial(self.dev, baudrate = self.baudRate, parity = self.parity, bytesize = self.byteSize, stopbits = self.stopBits, timeout=0.1, xonxoff=0, rtscts=0, interCharTimeout=None) self.serPort.flushOutput() self.serPort.flushInput() except serial.serialutil.SerialException as e: print(e) exit() if sendInit: # Opening a serial connection to an Arduino can cause it to # reset, so let's send it an init message then give it a moment # to get its act together self.send('++INIT++') time.sleep(2) self.serPort.flushOutput() self.serPort.flushInput() def send(self, sendStr): ''' Send a message. A newline is appended, so don't need to include that in the string. ''' sendStr = sendStr.strip() # clean it up first sendStr = sendStr + '\n' self.serPort.write(sendStr.encode('utf-8', 'replace')) return def newMsgs(self): ''' newMsgs() - obtain list of incoming messages. Assumes each message is terminated by newline & is utf-8 encoded. Dodgy bytes are treated using the .replace() method which does standard replacing according to codec. This avoids the program falling over should a bad byte appear that the utf-8 codec can't handle. But might be better to do some error handling here - maybe some basic flow control to request resending of message ''' msgList =  while self.serPort.inWaiting() > 0: # using readline could create a problem where data has been sent # but isn't terminated by a newline. In this case, the while above # would cause this program to wait forever. For time being that's tolerable # because we'll be strict about always using a newline msgList.append(self.serPort.readline().decode('utf-8', 'replace').strip()) return msgList def __str__(self): ''' Print information about this port ''' print('COMM PORT', self.dev,'-', str(self.baudRate) + ' baud', str(self.byteSize) + '-' + self.parity + '-' + str(self.stopBits)) return ' '
Of course it does - absolutely no reason it wouldn't as long as you get your voltages correct (using a converter where appropriate), get your RX and TX the right way round, and set matching baudrates.speculatrix wrote:I'm doing something very similar to this, connecting an RPi to a BeagleBone. I'm also connecting the BB to an Arduino this way and maybe the RPi to an Arduino. It's a festival of serial. And it's working fine.
I'm using level shifters between the BeagleBone & the Arduino. Also using the same level shifters when using the BB to work with 5V I2C devices. The ones I'm using are Adafruit's (I love that company): http://www.adafruit.com/products/757.AndrewS wrote:as long as you get your voltages correct (using a converter where appropriate)
Users browsing this forum: No registered users and 17 guests