今天給大家介紹的是串口調試工具pzh-py-com誕生之環境搭建 。
在寫pzh-py-com時需要先搭好開發和調試環境,下表列出了開發過程中會用到的所有軟件/工具包:
一、涉及工具列表
工具
功能
下載地址
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)
pzh-py-com工具是一個完全基於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>
Installing pip.exe>
Installing pip3.5-script.py>
Installing pip3.5.exe>
Installing pip3-script.py>
Installing pip3.exe>
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>
Successfully installed PyPubSub-4.0.0 six-1.11.0 typing-3.6.4 wxPython-4.0.3
有了pySerial便可以訪問Serial Port,有了wxPython便可以設計GUI。
單純使用wxPython設計pzh-py-com GUI界面時僅能是手工寫代碼佈局,手工佈局的界面創建和修改起來都比較繁瑣,我們需要一款可視化的界面設計工具,痞子衡選擇的是wxFormBuilder,從其github官網下載安裝包並安裝到C:\tools_mcu\wxFormBuilder目錄下。安裝完成打開軟件便可在Designer裡盡情創作界面,創作完成後點擊"Python"便可看到Python GUI源代碼,這個GUI源代碼後續直接複製到JaysPyCOM工程裡使用。
至此pzh-py-com工具開發的Python基礎環境便搭好了。
三、測試環境搭建(PyCharm + vspd + sscom)
在開發pzh-py-com工具過程中免不了要調試Python代碼,所以我們還需要一個Python IDE,痞子衡選擇的是PyCharm,在jetbrains官網下載PyCharm community免費版並安裝,安裝完成後打開PyCharm並創建名為pzh-py-com空工程,成功創建後會看到pzh-py-com目錄下自動生成一個.idea的文件夾,該文件夾是用於pycharm管理項目。
有了PyCharm環境,便可以開始寫pzh-py-com代碼,代碼在開發過程中,需要結合Serial Port進行聯合調試,如果手裡沒有硬件串口設備,可以使用虛擬串口設備,vspd便是著名的虛擬串口驅動,從eltima官網下載vspd標準版並安裝,安裝完成後打開vspd可看到如下界面,COM10和COM11(COM號是自定義的)便是虛擬出來的串口設備號,並且已經完成了對接。
虛擬Serial Port設備已經有了並且對接了,最後還需要一個成熟的串口調試助手,作為串口通訊的另一方,痞子衡選取的是非常經典的sscom,從大蝦官網下載sscom包,sscom是個免安裝的工具,可以直接打開使用,設置sscom使用COM11,將來pzh-py-com使用COM10。
四、發佈環境搭建(PyInstaller)
pzh-py-com工具開發和調試工作都完成之後,我們希望將她打包成一個獨立的可執行文件(即運行設備不需要安裝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包安裝好之後,環境搭建便大功告成
至此,串口調試工具pzh-py-com誕生之環境搭建便介紹完畢了