当前位置:   article > 正文

树莓派+Python+pyserial 2.7实现串口通信_pyserial-2.7

pyserial-2.7

手上有个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程式来实现串口通信:

  1. import serial
  2. from time import sleep
  3. ser = serial.Serial('/dev/ttyUSB0', 9600, timeout=0.5)
  4. def recv(serial):
  5. data
  6. while True:
  7. data =serial.read(30)
  8. if data == '':
  9. continue
  10. else:
  11. break
  12. sleep(0.02)
  13. return data
  14. while True:
  15. data =recv(ser)
  16. ser.write(data)

 

来自官网的Sample(wxTerminal.py)也很不错,可以通过UI选择和配置端口:

  1. #!/usr/bin/env python
  2. # generated by wxGlade 0.3.1 on Fri Oct 03 23:23:45 2003
  3. #from wxPython.wx import *
  4. import wx
  5. import wxSerialConfigDialog
  6. import serial
  7. import threading
  8. #----------------------------------------------------------------------
  9. # Create an own event type, so that GUI updates can be delegated
  10. # this is required as on some platforms only the main thread can
  11. # access the GUI without crashing. wxMutexGuiEnter/wxMutexGuiLeave
  12. # could be used too, but an event is more elegant.
  13. SERIALRX = wx.NewEventType()
  14. # bind to serial data receive events
  15. EVT_SERIALRX = wx.PyEventBinder(SERIALRX, 0)
  16. class SerialRxEvent(wx.PyCommandEvent):
  17. eventType = SERIALRX
  18. def __init__(self, windowID, data):
  19. wx.PyCommandEvent.__init__(self, self.eventType, windowID)
  20. self.data = data
  21. def Clone(self):
  22. self.__class__(self.GetId(), self.data)
  23. #----------------------------------------------------------------------
  24. ID_CLEAR = wx.NewId()
  25. ID_SAVEAS = wx.NewId()
  26. ID_SETTINGS = wx.NewId()
  27. ID_TERM = wx.NewId()
  28. ID_EXIT = wx.NewId()
  29. NEWLINE_CR = 0
  30. NEWLINE_LF = 1
  31. NEWLINE_CRLF = 2
  32. class TerminalSetup:
  33. """Placeholder for various terminal settings. Used to pass the
  34. options to the TerminalSettingsDialog."""
  35. def __init__(self):
  36. self.echo = False
  37. self.unprintable = False
  38. self.newline = NEWLINE_CRLF
  39. class TerminalSettingsDialog(wx.Dialog):
  40. """Simple dialog with common terminal settings like echo, newline mode."""
  41. def __init__(self, *args, **kwds):
  42. self.settings = kwds['settings']
  43. del kwds['settings']
  44. # begin wxGlade: TerminalSettingsDialog.__init__
  45. kwds["style"] = wx.DEFAULT_DIALOG_STYLE
  46. wx.Dialog.__init__(self, *args, **kwds)
  47. self.checkbox_echo = wx.CheckBox(self, -1, "Local Echo")
  48. self.checkbox_unprintable = wx.CheckBox(self, -1, "Show unprintable characters")
  49. self.radio_box_newline = wx.RadioBox(self, -1, "Newline Handling", choices=["CR only", "LF only", "CR+LF"], majorDimension=0, style=wx.RA_SPECIFY_ROWS)
  50. self.button_ok = wx.Button(self, -1, "OK")
  51. self.button_cancel = wx.Button(self, -1, "Cancel")
  52. self.__set_properties()
  53. self.__do_layout()
  54. # end wxGlade
  55. self.__attach_events()
  56. self.checkbox_echo.SetValue(self.settings.echo)
  57. self.checkbox_unprintable.SetValue(self.settings.unprintable)
  58. self.radio_box_newline.SetSelection(self.settings.newline)
  59. def __set_properties(self):
  60. # begin wxGlade: TerminalSettingsDialog.__set_properties
  61. self.SetTitle("Terminal Settings")
  62. self.radio_box_newline.SetSelection(0)
  63. self.button_ok.SetDefault()
  64. # end wxGlade
  65. def __do_layout(self):
  66. # begin wxGlade: TerminalSettingsDialog.__do_layout
  67. sizer_2 = wx.BoxSizer(wx.VERTICAL)
  68. sizer_3 = wx.BoxSizer(wx.HORIZONTAL)
  69. sizer_4 = wx.StaticBoxSizer(wx.StaticBox(self, -1, "Input/Output"), wx.VERTICAL)
  70. sizer_4.Add(self.checkbox_echo, 0, wx.ALL, 4)
  71. sizer_4.Add(self.checkbox_unprintable, 0, wx.ALL, 4)
  72. sizer_4.Add(self.radio_box_newline, 0, 0, 0)
  73. sizer_2.Add(sizer_4, 0, wx.EXPAND, 0)
  74. sizer_3.Add(self.button_ok, 0, 0, 0)
  75. sizer_3.Add(self.button_cancel, 0, 0, 0)
  76. sizer_2.Add(sizer_3, 0, wx.ALL|wx.ALIGN_RIGHT, 4)
  77. self.SetAutoLayout(1)
  78. self.SetSizer(sizer_2)
  79. sizer_2.Fit(self)
  80. sizer_2.SetSizeHints(self)
  81. self.Layout()
  82. # end wxGlade
  83. def __attach_events(self):
  84. self.Bind(wx.EVT_BUTTON, self.OnOK, id = self.button_ok.GetId())
  85. self.Bind(wx.EVT_BUTTON, self.OnCancel, id = self.button_cancel.GetId())
  86. def OnOK(self, events):
  87. """Update data wil new values and close dialog."""
  88. self.settings.echo = self.checkbox_echo.GetValue()
  89. self.settings.unprintable = self.checkbox_unprintable.GetValue()
  90. self.settings.newline = self.radio_box_newline.GetSelection()
  91. self.EndModal(wx.ID_OK)
  92. def OnCancel(self, events):
  93. """Do not update data but close dialog."""
  94. self.EndModal(wx.ID_CANCEL)
  95. # end of class TerminalSettingsDialog
  96. class TerminalFrame(wx.Frame):
  97. """Simple terminal program for wxPython"""
  98. def __init__(self, *args, **kwds):
  99. self.serial = serial.Serial()
  100. self.serial.timeout = 0.5 #make sure that the alive event can be checked from time to time
  101. self.settings = TerminalSetup() #placeholder for the settings
  102. self.thread = None
  103. self.alive = threading.Event()
  104. # begin wxGlade: TerminalFrame.__init__
  105. kwds["style"] = wx.DEFAULT_FRAME_STYLE
  106. wx.Frame.__init__(self, *args, **kwds)
  107. self.text_ctrl_output = wx.TextCtrl(self, -1, "", style=wx.TE_MULTILINE|wx.TE_READONLY)
  108. # Menu Bar
  109. self.frame_terminal_menubar = wx.MenuBar()
  110. self.SetMenuBar(self.frame_terminal_menubar)
  111. wxglade_tmp_menu = wx.Menu()
  112. wxglade_tmp_menu.Append(ID_CLEAR, "&Clear", "", wx.ITEM_NORMAL)
  113. wxglade_tmp_menu.Append(ID_SAVEAS, "&Save Text As...", "", wx.ITEM_NORMAL)
  114. wxglade_tmp_menu.AppendSeparator()
  115. wxglade_tmp_menu.Append(ID_SETTINGS, "&Port Settings...", "", wx.ITEM_NORMAL)
  116. wxglade_tmp_menu.Append(ID_TERM, "&Terminal Settings...", "", wx.ITEM_NORMAL)
  117. wxglade_tmp_menu.AppendSeparator()
  118. wxglade_tmp_menu.Append(ID_EXIT, "&Exit", "", wx.ITEM_NORMAL)
  119. self.frame_terminal_menubar.Append(wxglade_tmp_menu, "&File")
  120. # Menu Bar end
  121. self.__set_properties()
  122. self.__do_layout()
  123. # end wxGlade
  124. self.__attach_events() #register events
  125. self.OnPortSettings(None) #call setup dialog on startup, opens port
  126. if not self.alive.isSet():
  127. self.Close()
  128. def StartThread(self):
  129. """Start the receiver thread"""
  130. self.thread = threading.Thread(target=self.ComPortThread)
  131. self.thread.setDaemon(1)
  132. self.alive.set()
  133. self.thread.start()
  134. def StopThread(self):
  135. """Stop the receiver thread, wait util it's finished."""
  136. if self.thread is not None:
  137. self.alive.clear() #clear alive event for thread
  138. self.thread.join() #wait until thread has finished
  139. self.thread = None
  140. def __set_properties(self):
  141. # begin wxGlade: TerminalFrame.__set_properties
  142. self.SetTitle("Serial Terminal")
  143. self.SetSize((546, 383))
  144. # end wxGlade
  145. def __do_layout(self):
  146. # begin wxGlade: TerminalFrame.__do_layout
  147. sizer_1 = wx.BoxSizer(wx.VERTICAL)
  148. sizer_1.Add(self.text_ctrl_output, 1, wx.EXPAND, 0)
  149. self.SetAutoLayout(1)
  150. self.SetSizer(sizer_1)
  151. self.Layout()
  152. # end wxGlade
  153. def __attach_events(self):
  154. #register events at the controls
  155. self.Bind(wx.EVT_MENU, self.OnClear, id = ID_CLEAR)
  156. self.Bind(wx.EVT_MENU, self.OnSaveAs, id = ID_SAVEAS)
  157. self.Bind(wx.EVT_MENU, self.OnExit, id = ID_EXIT)
  158. self.Bind(wx.EVT_MENU, self.OnPortSettings, id = ID_SETTINGS)
  159. self.Bind(wx.EVT_MENU, self.OnTermSettings, id = ID_TERM)
  160. self.text_ctrl_output.Bind(wx.EVT_CHAR, self.OnKey)
  161. self.Bind(EVT_SERIALRX, self.OnSerialRead)
  162. self.Bind(wx.EVT_CLOSE, self.OnClose)
  163. def OnExit(self, event):
  164. """Menu point Exit"""
  165. self.Close()
  166. def OnClose(self, event):
  167. """Called on application shutdown."""
  168. self.StopThread() #stop reader thread
  169. self.serial.close() #cleanup
  170. self.Destroy() #close windows, exit app
  171. def OnSaveAs(self, event):
  172. """Save contents of output window."""
  173. filename = None
  174. dlg = wx.FileDialog(None, "Save Text As...", ".", "", "Text File|*.txt|All Files|*", wx.SAVE)
  175. if dlg.ShowModal() == wx.ID_OK:
  176. filename = dlg.GetPath()
  177. dlg.Destroy()
  178. if filename is not None:
  179. f = file(filename, 'w')
  180. text = self.text_ctrl_output.GetValue()
  181. if type(text) == unicode:
  182. text = text.encode("latin1") #hm, is that a good asumption?
  183. f.write(text)
  184. f.close()
  185. def OnClear(self, event):
  186. """Clear contents of output window."""
  187. self.text_ctrl_output.Clear()
  188. def OnPortSettings(self, event=None):
  189. """Show the portsettings dialog. The reader thread is stopped for the
  190. settings change."""
  191. if event is not None: #will be none when called on startup
  192. self.StopThread()
  193. self.serial.close()
  194. ok = False
  195. while not ok:
  196. dialog_serial_cfg = wxSerialConfigDialog.SerialConfigDialog(None, -1, "",
  197. show=wxSerialConfigDialog.SHOW_BAUDRATE|wxSerialConfigDialog.SHOW_FORMAT|wxSerialConfigDialog.SHOW_FLOW,
  198. serial=self.serial
  199. )
  200. result = dialog_serial_cfg.ShowModal()
  201. dialog_serial_cfg.Destroy()
  202. #open port if not called on startup, open it on startup and OK too
  203. if result == wx.ID_OK or event is not None:
  204. try:
  205. self.serial.open()
  206. except serial.SerialException, e:
  207. dlg = wx.MessageDialog(None, str(e), "Serial Port Error", wx.OK | wx.ICON_ERROR)
  208. dlg.ShowModal()
  209. dlg.Destroy()
  210. else:
  211. self.StartThread()
  212. self.SetTitle("Serial Terminal on %s [%s, %s%s%s%s%s]" % (
  213. self.serial.portstr,
  214. self.serial.baudrate,
  215. self.serial.bytesize,
  216. self.serial.parity,
  217. self.serial.stopbits,
  218. self.serial.rtscts and ' RTS/CTS' or '',
  219. self.serial.xonxoff and ' Xon/Xoff' or '',
  220. )
  221. )
  222. ok = True
  223. else:
  224. #on startup, dialog aborted
  225. self.alive.clear()
  226. ok = True
  227. def OnTermSettings(self, event):
  228. """Menu point Terminal Settings. Show the settings dialog
  229. with the current terminal settings"""
  230. dialog = TerminalSettingsDialog(None, -1, "", settings=self.settings)
  231. result = dialog.ShowModal()
  232. dialog.Destroy()
  233. def OnKey(self, event):
  234. """Key event handler. if the key is in the ASCII range, write it to the serial port.
  235. Newline handling and local echo is also done here."""
  236. code = event.GetKeyCode()
  237. if code < 256: #is it printable?
  238. if code == 13: #is it a newline? (check for CR which is the RETURN key)
  239. if self.settings.echo: #do echo if needed
  240. self.text_ctrl_output.AppendText('\n')
  241. if self.settings.newline == NEWLINE_CR:
  242. self.serial.write('\r') #send CR
  243. elif self.settings.newline == NEWLINE_LF:
  244. self.serial.write('\n') #send LF
  245. elif self.settings.newline == NEWLINE_CRLF:
  246. self.serial.write('\r\n') #send CR+LF
  247. else:
  248. char = chr(code)
  249. if self.settings.echo: #do echo if needed
  250. self.text_ctrl_output.WriteText(char)
  251. self.serial.write(char) #send the charcater
  252. else:
  253. print "Extra Key:", code
  254. def OnSerialRead(self, event):
  255. """Handle input from the serial port."""
  256. text = event.data
  257. if self.settings.unprintable:
  258. text = ''.join([(c >= ' ') and c or '<%d>' % ord(c) for c in text])
  259. self.text_ctrl_output.AppendText(text)
  260. def ComPortThread(self):
  261. """Thread that handles the incomming traffic. Does the basic input
  262. transformation (newlines) and generates an SerialRxEvent"""
  263. while self.alive.isSet(): #loop while alive event is true
  264. text = self.serial.read(1) #read one, with timout
  265. if text: #check if not timeout
  266. n = self.serial.inWaiting() #look if there is more to read
  267. if n:
  268. text = text + self.serial.read(n) #get it
  269. #newline transformation
  270. if self.settings.newline == NEWLINE_CR:
  271. text = text.replace('\r', '\n')
  272. elif self.settings.newline == NEWLINE_LF:
  273. pass
  274. elif self.settings.newline == NEWLINE_CRLF:
  275. text = text.replace('\r\n', '\n')
  276. event = SerialRxEvent(self.GetId(), text)
  277. self.GetEventHandler().AddPendingEvent(event)
  278. #~ self.OnSerialRead(text) #output text in window
  279. # end of class TerminalFrame
  280. class MyApp(wx.App):
  281. def OnInit(self):
  282. wx.InitAllImageHandlers()
  283. frame_terminal = TerminalFrame(None, -1, "")
  284. self.SetTopWindow(frame_terminal)
  285. frame_terminal.Show(1)
  286. return 1
  287. # end of class MyApp
  288. if __name__ == "__main__":
  289. app = MyApp(0)
  290. app.MainLoop()


 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/180796
推荐阅读
相关标签
  

闽ICP备14008679号