痞子衡嵌入式:串列埠除錯工具Jays-PyCOM誕生記(1)- 環境搭建(Python2.7.14 + pySerial3.4 + wxPython4.0.3)

痞子衡發表於2017-05-03

  大家好,我是痞子衡,是正經搞技術的痞子。今天痞子衡給大家介紹的是串列埠除錯工具Jays-PyCOM誕生之環境搭建

  在寫Jays-PyCOM時需要先搭好開發和除錯環境,下表列出了開發過程中會用到的所有軟體/工具包:

一、涉及工具列表

工具 功能 下載地址
Python 2.7.14 Python官方包(直譯器) https://www.python.org/
pySerial 3.4 Serial Port訪問的Python封裝庫 https://pypi.org/project/pyserial/
https://github.com/pyserial/pyserial
https://pythonhosted.org/pyserial/
wxPython 4.0.3 跨平臺開源GUI庫 [wxWidgets](https://www.wxwidgets.org/) 的Python封裝庫 https://www.wxpython.org/
https://pypi.org/project/wxPython/
wxFormBuilder 3.8.0 wxPython GUI介面構建工具 https://github.com/wxFormBuilder/wxFormBuilder
PyCharm Community 2018.02 一款流行的Python整合開發環境 http://www.jetbrains.com/pycharm/
PyInstaller 3.3.1 Python應用程式打包工具 http://www.pyinstaller.org/
https://github.com/pyinstaller/pyinstaller
vspd 9 虛擬串列埠驅動,可以在PC上虛擬出Serial Port https://www.eltima.com/products/vspdxp/
sscom 5.13.1 大蝦和丁丁聯合推出的一款很流行的串列埠除錯工具 http://www.daxia.com/sscom/

二、開發環境搭建(Python + pySerial + wxPython + wxFormBuilder)

  Jays-PyCOM工具是一個完全基於Python語言開發的應用軟體,首先安裝好Python 2.7.14,痞子衡的安裝目錄為C:\tools_mcu\Python27,安裝完成後確保系統環境變數裡包括該路徑(C:\tools_mcu\Python27),因為該路徑下包含python.exe,後續python命令需呼叫這個python.exe完成的。
  在C:\tools_mcu\Python27\Scripts目錄下預設有easy_install.exe,這是PEAK(Python Enterprise Application Kit)開發的setuptools包裡的工具,這個工具可以用來完成安裝python第三方模組的工作。我們需要藉助easy_install.exe來安裝pip工具:

PS C:\tools_mcu\Python27\Scripts> .\easy_install.exe pip

Searching for pip
Best match: pip 9.0.1
Adding pip 9.0.1 to easy-install.pth file
Installing pip-script.py script to c:\tools_mcu\python27\Scripts
Installing pip.exe script to c:\tools_mcu\python27\Scripts
Installing pip3.5-script.py script to c:\tools_mcu\python27\Scripts
Installing pip3.5.exe script to c:\tools_mcu\python27\Scripts
Installing pip3-script.py script to c:\tools_mcu\python27\Scripts
Installing pip3.exe script to c:\tools_mcu\python27\Scripts

Using c:\tools_mcu\python27\lib\site-packages
Processing dependencies for pip
Finished processing dependencies for pip

PS C:\tools_mcu\Python27\Scripts> python -m pip install --upgrade pip

Collecting pip
  Downloading > https://files.pythonhosted.org/packages/5f/25/e52d3f31441505a5f3af41213346e5b6c221c9e086a166f3703d2ddaf940/pip-18.0-py2.py3-none-any.whl (1.3MB)
Installing collected packages: pip
  Found existing installation: pip 9.0.1
    Uninstalling pip-9.0.1:
      Successfully uninstalled pip-9.0.1
Successfully installed pip-18.0

  pip是Python的包管理工具,提供了對Python包的查詢、下載、安裝、解除安裝的功能。安裝好pip工具之後,可以看到C:\tools_mcu\Python27\Scripts目錄下多了pip.exe,為方便後續使用pip來安裝其他Python包,確保系統環境變數裡包括pip路徑(C:\tools_mcu\Python27\Scripts)。我們可以藉助pip來安裝pySerial和wxPython包:

PS C:\tools_mcu\Python27\Scripts> .\pip.exe install pyserial

Collecting pyserial
  Downloading https://files.pythonhosted.org/packages/0d/e4/2a744dd9e3be04a0c0907414e2a01a7c88bb3915cbe3c8cc06e209f59c30/pyserial-3.4-py2.py3-none-any.whl (193kB)
Installing collected packages: pyserial
Successfully installed pyserial-3.4

PS C:\tools_mcu\Python27\Scripts> .\pip.exe install wxPython

Collecting wxPython
  Downloading https://files.pythonhosted.org/packages/88/88/a23b96662c5ab82dd8dbbb68c68dedea466229e8151fd2911713a1cd27b2/wxPython-4.0.3-cp27-cp27m-win_amd64.whl (22.8MB)
Collecting six (from wxPython)
  Downloading https://files.pythonhosted.org/packages/67/4b/141a581104b1f6397bfa78ac9d43d8ad29a7ca43ea90a2d863fe3056e86a/six-1.11.0-py2.py3-none-any.whl
Collecting PyPubSub (from wxPython)
  Downloading https://files.pythonhosted.org/packages/14/80/8e1d34848fea10826763600ca7eeb7a76d914ccab7cb0d64c9c180c30a73/Pypubsub-4.0.0.zip (64kB)
Collecting typing (from PyPubSub->wxPython)
  Downloading https://files.pythonhosted.org/packages/0d/4d/4e5985d075d241d686a1663fa1f88b61d544658d08c1375c7c6aac32afc3/typing-3.6.4-py2-none-any.whl
Installing collected packages: six, typing, PyPubSub, wxPython
  Running setup.py install for PyPubSub ... done
  The scripts helpviewer.exe, img2png.exe, img2py.exe, img2xpm.exe, pycrust.exe, pyshell.exe, pyslices.exe, pyslicesshell.exe, pywxrc.exe, wxdemo.exe, wxdocs.exe and wxget.exe are installed in 'c:\tools_mcu\python27\Scripts'
Successfully installed PyPubSub-4.0.0 six-1.11.0 typing-3.6.4 wxPython-4.0.3

  有了pySerial便可以訪問Serial Port,有了wxPython便可以設計GUI。
  單純使用wxPython設計Jays-PyCOM GUI介面時僅能是手工寫程式碼佈局,手工佈局的介面建立和修改起來都比較繁瑣,我們需要一款視覺化的介面設計工具,痞子衡選擇的是wxFormBuilder,從其github官網下載安裝包並安裝到C:\tools_mcu\wxFormBuilder目錄下。安裝完成開啟軟體便可在Designer裡盡情創作介面,創作完成後點選"Python"便可看到Python GUI原始碼,這個GUI原始碼後續直接複製到JaysPyCOM工程裡使用。

痞子衡嵌入式:串列埠除錯工具Jays-PyCOM誕生記(1)- 環境搭建(Python2.7.14 + pySerial3.4 + wxPython4.0.3)

  至此Jays-PyCOM工具開發的Python基礎環境便搭好了。

三、測試環境搭建(PyCharm + vspd + sscom)

  在開發Jays-PyCOM工具過程中免不了要除錯Python程式碼,所以我們還需要一個Python IDE,痞子衡選擇的是PyCharm,在jetbrains官網下載PyCharm community免費版並安裝,安裝完成後開啟PyCharm並建立名為Jays-PyCOM空工程,成功建立後會看到Jays-PyCOM目錄下自動生成一個.idea的資料夾,該資料夾是用於pycharm管理專案。

痞子衡嵌入式:串列埠除錯工具Jays-PyCOM誕生記(1)- 環境搭建(Python2.7.14 + pySerial3.4 + wxPython4.0.3)

  有了PyCharm環境,便可以開始寫Jays-PyCOM程式碼,程式碼在開發過程中,需要結合Serial Port進行聯合除錯,如果手裡沒有硬體串列埠裝置,可以使用虛擬串列埠裝置,vspd便是著名的虛擬串列埠驅動,從eltima官網下載vspd標準版並安裝,安裝完成後開啟vspd可看到如下介面,COM10和COM11(COM號是自定義的)便是虛擬出來的串列埠裝置號,並且已經完成了對接。

痞子衡嵌入式:串列埠除錯工具Jays-PyCOM誕生記(1)- 環境搭建(Python2.7.14 + pySerial3.4 + wxPython4.0.3)

  虛擬Serial Port裝置已經有了並且對接了,最後還需要一個成熟的串列埠除錯助手,作為串列埠通訊的另一方,痞子衡選取的是非常經典的sscom,從大蝦官網下載sscom包,sscom是個免安裝的工具,可以直接開啟使用,設定sscom使用COM11,將來Jays-PyCOM使用COM10。

痞子衡嵌入式:串列埠除錯工具Jays-PyCOM誕生記(1)- 環境搭建(Python2.7.14 + pySerial3.4 + wxPython4.0.3)

四、釋出環境搭建(PyInstaller)

  Jays-PyCOM工具開發和除錯工作都完成之後,我們希望將她打包成一個獨立的可執行檔案(即執行裝置不需要安裝Python+pySerial+wxPython),PyInstaller可以幫助我們完成打包工作,所以我們需要利用pip工具安裝PyInstaller包:

PS C:\tools_mcu\Python27\Scripts> .\pip.exe install pyinstaller

Collecting pyinstaller
  Downloading https://files.pythonhosted.org/packages/3c/86/909a8c35c5471919b3854c01f43843d9b5aed0e9948b63e560010f7f3429/PyInstaller-3.3.1.tar.gz (3.5MB)
Requirement already satisfied: setuptools in c:\tools_mcu\python27\lib\site-packages (from pyinstaller) (28.8.0)
Collecting pefile>=2017.8.1 (from pyinstaller)
  Downloading https://files.pythonhosted.org/packages/ed/cc/157f20038a80b6a9988abc06c11a4959be8305a0d33b6d21a134127092d4/pefile-2018.8.8.tar.gz (62kB)
Collecting macholib>=1.8 (from pyinstaller)
  Downloading https://files.pythonhosted.org/packages/a1/01/845b2df65117dbdabf00c6df879625f4968ede6f512956710f05f4c7663a/macholib-1.10-py2.py3-none-any.whl
Collecting dis3 (from pyinstaller)
  Downloading https://files.pythonhosted.org/packages/c8/a1/bb0ab17df7e6cbc6d1555dd1c6fdaa09e90842f0f683507042b9dae83e2d/dis3-0.1.2.tar.gz
Collecting future (from pyinstaller)
  Downloading https://files.pythonhosted.org/packages/00/2b/8d082ddfed935f3608cc61140df6dcbf0edea1bc3ab52fb6c29ae3e81e85/future-0.16.0.tar.gz (824kB)
Collecting pypiwin32 (from pyinstaller)
  Downloading https://files.pythonhosted.org/packages/13/e8/4f38eb30c4dae36634a53c5b2cd73b517ea3607e10d00f61f2494449cec0/pypiwin32-223.tar.gz
Collecting altgraph>=0.15 (from macholib>=1.8->pyinstaller)
  Downloading https://files.pythonhosted.org/packages/0a/cc/646187eac4b797069e2e6b736f14cdef85dbe405c9bfc7803ef36e4f62ef/altgraph-0.16.1-py2.py3-none-any.whl
Collecting pywin32>=223 (from pypiwin32->pyinstaller)
  Downloading https://files.pythonhosted.org/packages/65/83/0b14690d70bcd193a67c8b0a640129717e37a11d8e6a3e28a01e47910737/pywin32-223-cp27-cp27m-win_amd64.whl (7.3MB)
Installing collected packages: future, pefile, altgraph, macholib, dis3, pywin32, pypiwin32, pyinstaller
  Running setup.py install for future ... done
  Running setup.py install for pefile ... done
  Running setup.py install for dis3 ... done
  Running setup.py install for pypiwin32 ... done
  Running setup.py install for pyinstaller ... done
Successfully installed altgraph-0.16.1 dis3-0.1.2 future-0.16.0 macholib-1.10 pefile-2018.8.8 pyinstaller-3.3.1 pypiwin32-223 pywin32-223

  PyInstaller包安裝好之後,環境搭建便大功告成

  至此,串列埠除錯工具Jays-PyCOM誕生之環境搭建痞子衡便介紹完畢了,掌聲在哪裡~~~

相關文章