a Python operation serial port
First download Install the serial port module pyserial.
Code implementation:
Import Serialser = serial. Serial ('/DEV/TTYUSB2 ', 115200) print ser.portstrwhile True: str = ser.read (TEN) print str strinput = raw_ Input (' Enter some words: ') n = t.write (strinput)
The/DEV/TTYUSB2 here is a serial device file for Linux, which can be com1,com2 on Windows.
two use Py2exe to compile the py file into exe executable file
This is still necessary, especially if you are running a Python program on a machine that does not have a Python environment installed.
First download the Py2exe program: Click to open the link
Assuming now that a upsdaemon.py program needs to be compiled into an EXE file, you first need a setup.py file:
# setup.pyfrom Distutils.core Import setupimport py2exe setup (console=["upsdaemon.py"])
And it needs to be placed in the same directory as the code.
Executes on the CMD serial port:
When the above command executes, it will produce a subdirectory named Dist:
If your upsdaemon.py script uses compiled C extensions, the modules are also copied in the subdirectory, and all DLL files are required at run time, in addition to the system's DLL files.
The files in the Dist subdirectory contain what is necessary for your program, and you should publish all the content in this subdirectory.
By default, Py2exe creates the following required files under Directory dist:
1. One or more EXE files.
2.python##.dll.
3. Several. pyd files, which are compiled extensions that are required by the EXE file, plus other. dll files that are required by the. PYD.
4. A library.zip file that contains compiled pure Python modules such as. PYc or. pyo
The above upsdaemon.py creates a console Upsdaemon.exe program, if you want to create a graphical user interface program, then you only need to upsdaemon.py console=["upsdaemon.py") Replace with windows=["upsdaemon.py"].
Py2exe You can create multiple EXE files at once, you need to pass a list of these script files to the console or Windows keyword parameters. This is useful if you have several associated scripts.
Run the following command to display all the command line tokens for the Py2exe command.
Python upsdaemon.py py2exe--help
three shortcuts for creating relative paths
Since the executable is not easy to execute in the Dist directory, it is necessary to create a shortcut to the previous level of the Dist, but the shortcut defaults to the absolute path, and the copy to the other computer will not necessarily be performed.
So the way to use relative paths.
To create a shortcut to Dist, right-click on the property and modify the target to:
%WINDIR%\SYSTEM32\CMD.EXE/C Start Dist\upsdaemon.exe
Once saved, double-click the shortcut to execute.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
python2.7 serial operation mode compile. py for Windows executable exe file