手上有个CCD Camera(Barcode Reader/Scanner Module),它是通过RS232通信的,用RS232转USB的转接线连接树莓派,即可完成硬件连接。对于串口通信,可以通过pyserial实现。
首先,安装pyserial:
从https://pypi.python.org/pypi/pyserial下载最新版本的安装包,再通过下面的命令完成安装:
- tar zxvf pyserial-2.7.tar.gz
- cd pyserial-2.7
- python setup.py install
通过命令lsusb查看串口是否存在:
通过命令python -m serial.tools.list_ports可以查看大可用的端口:
测试通信:
通过以上的准备后,就可以写一个简单的Python程式来实现串口通信:
- import serial
- from time import sleep
- ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=0.5)
- def recv(serial):
- data
- while True:
- data =serial.read(30)
- if data == '':
- continue
- else:
- break
- sleep(0.02)
- return data
- while True:
- data =recv(ser)
- ser.write(data)
来自官网的Sample(wxTerminal.py)也很不错,可以通过UI选择和配置端口:
- #!/usr/bin/env python
- # generated by wxGlade 0.3.1 on Fri Oct 03 23:23:45 2003
- #from wxPython.wx import *
- import wx
- import wxSerialConfigDialog
- import serial
- import threading
- #----------------------------------------------------------------------
- # Create an own event type, so that GUI updates can be delegated
- # this is required as on some platforms only the main thread can
- # access the GUI without crashing. wxMutexGuiEnter/wxMutexGuiLeave
- # could be used too, but an event is more elegant.
- SERIALRX = wx.NewEventType()
- # bind to serial data receive events
- EVT_SERIALRX = wx.PyEventBinder(SERIALRX, 0)
- class SerialRxEvent(wx.PyCommandEvent):
- eventType = SERIALRX
- def __init__(self, windowID, data):
- wx.PyCommandEvent.__init__(self, self.eventType, windowID)
- self.data = data
- def Clone(self):
- self.__class__(self.GetId(), self.data)
- #----------------------------------------------------------------------
- ID_CLEAR = wx.NewId()
- ID_SAVEAS = wx.NewId()
- ID_SETTINGS = wx.NewId()
- ID_TERM = wx.NewId()
- ID_EXIT = wx.NewId()
- NEWLINE_CR = 0
- NEWLINE_LF = 1
- NEWLINE_CRLF = 2
- class TerminalSetup:
- """Placeholder for various terminal settings. Used to pass the
- options to the TerminalSettingsDialog."""
- def __init__(self):
- self.echo = False
- self.unprintable = False
- self.newline = NEWLINE_CRLF
- class TerminalSettingsDialog(wx.Dialog):
- """Simple dialog with common terminal settings like echo, newline mode."""
- def __init__(self, *args, **kwds):
- self.settings = kwds['settings']
- del kwds['settings']
- # begin wxGlade: TerminalSettingsDialog.__init__
- kwds["style"] = wx.DEFAULT_DIALOG_STYLE
- wx.Dialog.__init__(self, *args, **kwds)
- self.checkbox_echo = wx.CheckBox(self, -1, "Local Echo")
- self.checkbox_unprintable = wx.CheckBox(self, -1, "Show unprintable characters")
- self.radio_box_newline = wx.RadioBox(self, -1, "Newline Handling", choices=["CR only", "LF only", "CR+LF"], majorDimension=0, style=wx.RA_SPECIFY_ROWS)
- self.button_ok = wx.Button(self, -1, "OK")
- self.button_cancel = wx.Button(self, -1, "Cancel")
- self.__set_properties()
- self.__do_layout()
- # end wxGlade
- self.__attach_events()
- self.checkbox_echo.SetValue(self.settings.echo)
- self.checkbox_unprintable.SetValue(self.settings.unprintable)
- self.radio_box_newline.SetSelection(self.settings.newline)
- def __set_properties(self):
- # begin wxGlade: TerminalSettingsDialog.__set_properties
- self.SetTitle("Terminal Settings")
- self.radio_box_newline.SetSelection(0)
- self.button_ok.SetDefault()
- # end wxGlade
- def __do_layout(self):
- # begin wxGlade: TerminalSettingsDialog.__do_layout
- sizer_2 = wx.BoxSizer(wx.VERTICAL)
- sizer_3 = wx.BoxSizer(wx.HORIZONTAL)
- sizer_4 = wx.StaticBoxSizer(wx.StaticBox(self, -1, "Input/Output"), wx.VERTICAL)
- sizer_4.Add(self.checkbox_echo, 0, wx.ALL, 4)
- sizer_4.Add(self.checkbox_unprintable, 0, wx.ALL, 4)
- sizer_4.Add(self.radio_box_newline, 0, 0, 0)
- sizer_2.Add(sizer_4, 0, wx.EXPAND, 0)
- sizer_3.Add(self.button_ok, 0, 0, 0)
- sizer_3.Add(self.button_cancel, 0, 0, 0)
- sizer_2.Add(sizer_3, 0, wx.ALL|wx.ALIGN_RIGHT, 4)
- self.SetAutoLayout(1)
- self.SetSizer(sizer_2)
- sizer_2.Fit(self)
- sizer_2.SetSizeHints(self)
- self.Layout()
- # end wxGlade
- def __attach_events(self):
- self.Bind(wx.EVT_BUTTON, self.OnOK, id = self.button_ok.GetId())
- self.Bind(wx.EVT_BUTTON, self.OnCancel, id = self.button_cancel.GetId())
- def OnOK(self, events):
- """Update data wil new values and close dialog."""
- self.settings.echo = self.checkbox_echo.GetValue()
- self.settings.unprintable = self.checkbox_unprintable.GetValue()
- self.settings.newline = self.radio_box_newline.GetSelection()
- self.EndModal(wx.ID_OK)
- def OnCancel(self, events):
- """Do not update data but close dialog."""
- self.EndModal(wx.ID_CANCEL)
- # end of class TerminalSettingsDialog
- class TerminalFrame(wx.Frame):
- """Simple terminal program for wxPython"""
- def __init__(self, *args, **kwds):
- self.serial = serial.Serial()
- self.serial.timeout = 0.5 #make sure that the alive event can be checked from time to time
- self.settings = TerminalSetup() #placeholder for the settings
- self.thread = None
- self.alive = threading.Event()
- # begin wxGlade: TerminalFrame.__init__
- kwds["style"] = wx.DEFAULT_FRAME_STYLE
- wx.Frame.__init__(self, *args, **kwds)
- self.text_ctrl_output = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE|wx.TE_READONLY)
- # Menu Bar
- self.frame_terminal_menubar = wx.MenuBar()
- self.SetMenuBar(self.frame_terminal_menubar)
- wxglade_tmp_menu = wx.Menu()
- wxglade_tmp_menu.Append(ID_CLEAR, "&Clear", "", wx.ITEM_NORMAL)
- wxglade_tmp_menu.Append(ID_SAVEAS, "&Save Text As...", "", wx.ITEM_NORMAL)
- wxglade_tmp_menu.AppendSeparator()
- wxglade_tmp_menu.Append(ID_SETTINGS, "&Port Settings...", "", wx.ITEM_NORMAL)
- wxglade_tmp_menu.Append(ID_TERM, "&Terminal Settings...", "", wx.ITEM_NORMAL)
- wxglade_tmp_menu.AppendSeparator()
- wxglade_tmp_menu.Append(ID_EXIT, "&Exit", "", wx.ITEM_NORMAL)
- self.frame_terminal_menubar.Append(wxglade_tmp_menu, "&File")
- # Menu Bar end
- self.__set_properties()
- self.__do_layout()
- # end wxGlade
- self.__attach_events() #register events
- self.OnPortSettings(None) #call setup dialog on startup, opens port
- if not self.alive.isSet():
- self.Close()
- def StartThread(self):
- """Start the receiver thread"""
- self.thread = threading.Thread(target=self.ComPortThread)
- self.thread.setDaemon(1)
- self.alive.set()
- self.thread.start()
- def StopThread(self):
- """Stop the receiver thread, wait util it's finished."""
- if self.thread is not None:
- self.alive.clear() #clear alive event for thread
- self.thread.join() #wait until thread has finished
- self.thread = None
- def __set_properties(self):
- # begin wxGlade: TerminalFrame.__set_properties
- self.SetTitle("Serial Terminal")
- self.SetSize((546, 383))
- # end wxGlade
- def __do_layout(self):
- # begin wxGlade: TerminalFrame.__do_layout
- sizer_1 = wx.BoxSizer(wx.VERTICAL)
- sizer_1.Add(self.text_ctrl_output, 1, wx.EXPAND, 0)
- self.SetAutoLayout(1)
- self.SetSizer(sizer_1)
- self.Layout()
- # end wxGlade
- def __attach_events(self):
- #register events at the controls
- self.Bind(wx.EVT_MENU, self.OnClear, id = ID_CLEAR)
- self.Bind(wx.EVT_MENU, self.OnSaveAs, id = ID_SAVEAS)
- self.Bind(wx.EVT_MENU, self.OnExit, id = ID_EXIT)
- self.Bind(wx.EVT_MENU, self.OnPortSettings, id = ID_SETTINGS)
- self.Bind(wx.EVT_MENU, self.OnTermSettings, id = ID_TERM)
- self.text_ctrl_output.Bind(wx.EVT_CHAR, self.OnKey)
- self.Bind(EVT_SERIALRX, self.OnSerialRead)
- self.Bind(wx.EVT_CLOSE, self.OnClose)
- def OnExit(self, event):
- """Menu point Exit"""
- self.Close()
- def OnClose(self, event):
- """Called on application shutdown."""
- self.StopThread() #stop reader thread
- self.serial.close() #cleanup
- self.Destroy() #close windows, exit app
- def OnSaveAs(self, event):
- """Save contents of output window."""
- filename = None
- dlg = wx.FileDialog(None, "Save Text As...", ".", "", "Text File|*.txt|All Files|*", wx.SAVE)
- if dlg.ShowModal() == wx.ID_OK:
- filename = dlg.GetPath()
- dlg.Destroy()
- if filename is not None:
- f = file(filename, 'w')
- text = self.text_ctrl_output.GetValue()
- if type(text) == unicode:
- text = text.encode("latin1") #hm, is that a good asumption?
- f.write(text)
- f.close()
- def OnClear(self, event):
- """Clear contents of output window."""
- self.text_ctrl_output.Clear()
- def OnPortSettings(self, event=None):
- """Show the portsettings dialog. The reader thread is stopped for the
- settings change."""
- if event is not None: #will be none when called on startup
- self.StopThread()
- self.serial.close()
- ok = False
- while not ok:
- dialog_serial_cfg = wxSerialConfigDialog.SerialConfigDialog(None, -1, "",
- show=wxSerialConfigDialog.SHOW_BAUDRATE|wxSerialConfigDialog.SHOW_FORMAT|wxSerialConfigDialog.SHOW_FLOW,
- serial=self.serial
- )
- result = dialog_serial_cfg.ShowModal()
- dialog_serial_cfg.Destroy()
- #open port if not called on startup, open it on startup and OK too
- if result == wx.ID_OK or event is not None:
- try:
- self.serial.open()
- except serial.SerialException, e:
- dlg = wx.MessageDialog(None, str(e), "Serial Port Error", wx.OK | wx.ICON_ERROR)
- dlg.ShowModal()
- dlg.Destroy()
- else:
- self.StartThread()
- self.SetTitle("Serial Terminal on %s [%s, %s%s%s%s%s]" % (
- self.serial.portstr,
- self.serial.baudrate,
- self.serial.bytesize,
- self.serial.parity,
- self.serial.stopbits,
- self.serial.rtscts and ' RTS/CTS' or '',
- self.serial.xonxoff and ' Xon/Xoff' or '',
- )
- )
- ok = True
- else:
- #on startup, dialog aborted
- self.alive.clear()
- ok = True
- def OnTermSettings(self, event):
- """Menu point Terminal Settings. Show the settings dialog
- with the current terminal settings"""
- dialog = TerminalSettingsDialog(None, -1, "", settings=self.settings)
- result = dialog.ShowModal()
- dialog.Destroy()
- def OnKey(self, event):
- """Key event handler. if the key is in the ASCII range, write it to the serial port.
- Newline handling and local echo is also done here."""
- code = event.GetKeyCode()
- if code < 256: #is it printable?
- if code == 13: #is it a newline? (check for CR which is the RETURN key)
- if self.settings.echo: #do echo if needed
- self.text_ctrl_output.AppendText('\n')
- if self.settings.newline == NEWLINE_CR:
- self.serial.write('\r') #send CR
- elif self.settings.newline == NEWLINE_LF:
- self.serial.write('\n') #send LF
- elif self.settings.newline == NEWLINE_CRLF:
- self.serial.write('\r\n') #send CR+LF
- else:
- char = chr(code)
- if self.settings.echo: #do echo if needed
- self.text_ctrl_output.WriteText(char)
- self.serial.write(char) #send the charcater
- else:
- print "Extra Key:", code
- def OnSerialRead(self, event):
- """Handle input from the serial port."""
- text = event.data
- if self.settings.unprintable:
- text = ''.join([(c >= ' ') and c or '<%d>' % ord(c) for c in text])
- self.text_ctrl_output.AppendText(text)
- def ComPortThread(self):
- """Thread that handles the incomming traffic. Does the basic input
- transformation (newlines) and generates an SerialRxEvent"""
- while self.alive.isSet(): #loop while alive event is true
- text = self.serial.read(1) #read one, with timout
- if text: #check if not timeout
- n = self.serial.inWaiting() #look if there is more to read
- if n:
- text = text + self.serial.read(n) #get it
- #newline transformation
- if self.settings.newline == NEWLINE_CR:
- text = text.replace('\r', '\n')
- elif self.settings.newline == NEWLINE_LF:
- pass
- elif self.settings.newline == NEWLINE_CRLF:
- text = text.replace('\r\n', '\n')
- event = SerialRxEvent(self.GetId(), text)
- self.GetEventHandler().AddPendingEvent(event)
- #~ self.OnSerialRead(text) #output text in window
- # end of class TerminalFrame
- class MyApp(wx.App):
- def OnInit(self):
- wx.InitAllImageHandlers()
- frame_terminal = TerminalFrame(None, -1, "")
- self.SetTopWindow(frame_terminal)
- frame_terminal.Show(1)
- return 1
- # end of class MyApp
- if __name__ == "__main__":
- app = MyApp(0)
- app.MainLoop()
相关推荐
总之,树莓派和STM32的串口通信涉及到了嵌入式系统的硬件接口配置、软件编程以及通信协议的设计。通过理解这些基础知识,你就能有效地实现两个平台间的双向数据交换,为你的项目构建可靠的数据通道。
本压缩包“树莓派串口与外部设备通信程序代码.zip”包含了实现这一功能的相关代码,帮助开发者更好地理解和应用串口通信技术。 在树莓派上,串口通常指的是UART(Universal Asynchronous Receiver/Transmitter),...
注意,目前搜到的大多数描述树莓派 4 串口的文章,大多数开头都是禁用下蓝牙,这个做法针对树莓派0-3 是必须的,因为本身串口不够用,但对树莓派 4 来说并不需要,因为有额外 4 个串口可以利用,默认配置好的两串口...
总的来说,通过STM32和树莓派的串口通信控制LORA进行远距离通讯,不仅可以学习到微控制器的基本编程和嵌入式系统的设计,还能掌握无线通信技术和物联网应用的实现方法。这是一次全面且实用的学习实践,对于想要深入...
快速上手Stm32与树莓派的串口通信方法,有源码有视频
硬件串口由硬件实现,有单独的波特率时钟源,性能高、可靠。 mini串口性能低,功能也简单,并且没有波特率专用的时钟源而是由CPU内核时钟提供,因此mini串口有个致命的弱点是:波特率受到内核时钟的影响。内核若在...
树莓派串口通信程序,串口发送字符或数据。
树莓派串口通信是利用树莓派的通用异步收发传输器(UART)接口进行数据通信的一种技术。在物联网(IoT)项目、自动化设备控制或远程数据传输等场景中,串口通信因其简单、可靠而被广泛应用。本文将深入探讨如何使用...
在这些应用中,串行通信接口(Serial Port)扮演着重要角色,它允许树莓派与各种硬件设备如传感器、微控制器或其他计算机进行低级别数据交换。这个“树莓派3串口操作例子源码”压缩包提供了一种实践方法来掌握串口...
3. **rasapi32 API**(可能是误解或特定项目名词):可能涉及到树莓派串口通信的特定接口或方法。 4. **upperobj**:可能是一个自定义的Python类,封装了串口操作,提供更高级别的接口。 5. **Python游戏编程**:...
树莓派I2C通信,使用python的UPD网络远程控制树莓派i2c发送指令和读取数据指令,里面包含了3个i2c子设备
SPI(Serial Peripheral Interface)是一种同步串行接口,通常用于连接微控制器与外围设备,具有高速、低引脚数的特点。 在树莓派3B上,我们需要配置SPI接口并安装必要的软件库来与MCP2515通信。树莓派3B内建了SPI...
### 四、树莓派串口通信编程 1. **自收发测试**:将树莓派的 8 号和 10 号引脚短接,通过 LabVIEW 实现串口通信的测试。 2. **电平转换**:树莓派的串口通信采用 TTL 电平,与其他设备通信(如电脑)时,可能需要 ...
小组内成员共享代码,互相提出修改意见,内容包括电机测试,舵机测试,编码器使用函数,巡线模块,超声测距模块使用,机器人运动逻辑说明等等,希望大家努力工作,进入决赛!
它配备有多个接口,包括USB、HDMI、以太网以及串行通用接口(UART),后者是我们进行串口通信的关键。 串口通讯是一种简单且实用的通信方式,常用于设备间的短距离通信。在树莓派上,UART通常用于与外部设备如...
在本文中,我们将深入探讨如何在Ubuntu MATE操作系统下的ROS Melodic版本中,实现树莓派与STM32单片机之间的串口通信,并利用yaml文件进行参数调用。ROS(Robot Operating System)是一个开源机器人软件框架,它提供...
它们将USB接口转换为标准的串行通信端口,如UART(通用异步接收/发送器),使得树莓派可以通过USB接口与串口设备通信。 在树莓派上配置USB转串口设备,你需要确保安装了必要的驱动程序和库。对于FTDI设备,树莓派...
这是一种多主控、双向二线制同步串行接口,由飞利浦(现为恩智浦半导体)在1982年推出。IIC协议允许树莓派与其他设备,如传感器和控制器,进行高效通信,仅需两根数据线(SDA和SCL)即可完成数据传输。 PCF8591是一...
HAL库编写stm32实现与树莓派串口通信,上,下位机均有 之前没有考虑到python没有明确区分short型和int型,这里引入numpy模块来强制转化数据,具体操作及树莓派上数据的处理见calculate.py文件 该资源内项目源码是...