Python2.7 serial port Compiling. py is a windows Executable exe file,
1. python serial port
First download and install the serial port module pyserial.
Code implementation:
import serialser = serial.Serial('/dev/ttyUSB2', 115200)print ser.portstrwhile True: str = ser.read(10) print str strInput = raw_input('enter some words:') n = t.write(strInput) Here,/dev/ttyUSB2 is a serial port device file for linux, and com1 and com2 can be used for windows.
2. Use py2exe to compile the py file into an exe Executable File
This is still necessary, especially when running python programs on machines that do not have a python environment installed.
First download the py2exe program: Click to open the link
Assume that you want to compile a upsdaemon. py program into an exe file. First, you need a setup. py file:
# setup.pyfrom distutils.core import setupimport py2exe setup(console=["upsdaemon.py"])
And put it in the same directory as the code.
Run the following command on the cmd Serial Port:
After the preceding command is executed, a sub-directory named dist will be generated:
If your upsdaemon. if the compiled C extension module is used in The py script, these modules will be copied to sub-directories. Similarly, all dll files are required at runtime, besides system dll files.
The files in the dist subdirectory contain what is necessary for your program. You should publish all the content in this subdirectory together.
By default, py2exe creates the following necessary files in the dist directory:
1. One or more exe files.
2. python ##. dll.
3. Several. pyd files, which are compiled extensions and required by the exe files. With other. dll files, these. dll files are required by. pyd.
4.a library.zip file that contains compiled python modules such as. pyc or. pyo
Your upsdaemon.pycreates A upsdaemon.exe program that controls the console. If you want to create a program in the graphic user field, you only need to set the upsdaemon. console = ["upsdaemon. replace py "] with windows = [" upsdaemon. py.
Py2exe can create multiple exe files at a time. You need to pass the 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 command line labels of the py2exe command.
Python upsdaemon. py py2exe -- help
3. create shortcuts for relative paths
Because the executable file is in the dist directory, it is inconvenient to execute it. It is necessary to create a shortcut to the previous directory of the dist, but the shortcut uses an absolute path by default, copying data to other computers may not be executed.
Therefore, the relative path is used.
Create a shortcut to the directory on the top of dist, right-click the attribute, and modify the target:
%windir%\system32\cmd.exe /c start dist\upsdaemon.exe
Save and double-click the shortcut.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.