Python module installation, serial port operation, byte conversion, EXE generation, CRC calculation

Source: Internet
Author: User

Python module installation, serial port operation, byte conversion, EXE generation, CRC calculation

Background: For the first time, use Python to make gadgets, installed version 2.7.9 python;1.pycharm license registration problem with License server, license server address: (HTTP://XIDEA.O  nline) 2.pyCharm debugging under the file to create a new project, and then create a new file, you can use integrated debugging; 3. Serial operation-External package can be run directly under cmd: PIP install PYSERIAL4.CRC---external package Can be run through Windows cmd: Pip install crcmod 5. BYTE and ASCII conversion--Internal package Binascii6.python generate EXE---external package PyInstaller7. Serial Operation API usage (1) Open the serial Port API---Serial ("COM19", 115200) [note] : Here you see the first parameter someone wrote 0 (corresponding to COM1), but prompt for input error, require input string type, input ' 0 ' still not, (2) Read Operation API---read (n) need to know in advance the return string length returned STRAPI---readline () read a row,  At the end of OA or OD return str,str the value of ASCII code, such as 0x30 need to write ' 3 ', processing it, need to convert to 16 binary; API---readlines () read multiple lines, Back to list there are other schemes that say mate: the API---inwaiting () returns the length of the string received, then reads the value to read (), the test is unsuccessful, the API---readall () test failed, and (3) The write operation API---write ( Data) data for the str,str inside the value of the ASCII character form, such as 0x33 need to write ' 3 '; The method is described later; 8.byte is converted to ASCIIAPI---unhexlify (data) will be byte type (16 binary value) Converted to ASCII characters, such as 0x33 for the character ' 3 ', for the packet after the data into a character type, and then transfer, api--hexlify (data) to convert ASCII character data into byte (16 binary form), such as the received data, after processing, Other subsequent processing, such as CRC validation, etc. 9.CRC Api--mkcrcfun (0x11021,rev=false,initcrc=0x0000,xorout=0x0000) 0x11021 represents CRC-16 XMODEM (x16+x12+x5+1) Example: Crc16_xmodem = Crcmod.mkcrcfun (0x11021,rev=false,initcrc=0x0000, xorout=0x0000) Readfun = ' 68020000 ' readcrcout = Hex (Crc16_cmodem (unhexlify)) output comes with a value of readfun and the like, str type 10. There are two ways to compile the py file into exe form under Pyinstaller python: Pyinstaller and Py2exe; Installation: cmd input: Pip install Pyinstaller (1) operation mode in the storage. Py Folder Open the cmd for the current path, and then pyinstaller-f the file. py [Note]:1.pip install Pyinstaller must pay attention to the case, see the online a lot of is pyinstaller, and then have been installed unsuccessful; Pip install py 2exe is still not installed successfully; 2. On the comparison between Pyinstaller and Py2exe, it is said that the py2exe will produce a bunch of files in the folder where EXE is generated, Pyinstaller will only generate one; on the other hand: Say yes, Py2exe to 32-bit or 64-bit mutual support is not good, 32-bit on the production of 64-bit may not be used; I use the Pyinsataller, on the 32-bit production, 64-bit is also available; 3.   The Pyinstaller distinguishes between 32-bit and 64-bit, and the installation is consistent with the Python-bit system, 4. About whether you need to copy the module used to the. py Directory online Someone said need to put together, I tried not to put together, still can 11. String processing (1) Intercept section data = data[:-4] #将最后4个干掉 data = Data[2:4] #获取字符串中的第3和第4个 (2) Replace data = Data.replace (' What needs to be replaced ', ' replace the contents of the former ') [Tip]:1. Detect if the module is installed successfully: After the module is installed, you can enter Python under CMD and import the module name to see if the installation is successful; 2. Detection API return value type: print (Type ()) 3.CRC the value calculated by the module can be obtained through the tool (CRC CalCuLator icon is the abacus) to carry out the code: Declaration: No encapsulation, no attention to writing and command, etc., purely for the purpose of realizing the function and learning; 
#-*-Coding:utf-8-*-from serial import *from binascii import *from crcmod import *import time# def crc16cal (x): # a =             0x0000# B = 0x1021# for byte in x:# a ^= ord (byte) # a <<= 8# for I in range (8): # A <<= = A & 0x8000# if last = = 1:# a ^= b# s = Hex (a) # return s[4:6]+s[2:4]readfun = ' 68020000 ' Crc16_cmodem = Crcmod.mkcrcfun (0x11021,rev=false,initcrc=0x0000,xorout= 0x0000) Readcrcout = Hex (Crc16_cmodem (Unhexlify (Readfun))) Readcrcout = Readcrcout[2:4]+readcrcout[4:6]print ( Readcrcout) Readfun + = Readcrcoutprint (readfun) Readfun = unhexlify (readfun) ser = Serial ("COM19", 115200) print (Readfun) Ser.write (readfun) data = ser.read (data) print (data) data = Data.replace (' 68023400 ', ' 68033400 ') print ( Data) data = Data[:-12]print (data) Data + = ' 03000000 ' Print (data) Readcrcout = Hex (crc16_cmodem (data)) Readcrcout = Readcrcout[2:4]+readcrcout[4:6]print (readcrcout) Data += Readcrcoutprint (data) data = unhexlify (data) ser.write (data) #sleeptime. Sleep (2) #resetreset = ' 68010000 ' readcrcout = Hex (Crc16_cmodem (unhexlify (reset))) Readcrcout = Readcrcout[2:4]+readcrcout[4:6]reset + = Readcrcoutreset = Unhexlify (      Reset) ser.write (reset) ser.close ()

  

Python module installation, serial port operation, byte conversion, EXE generation, CRC calculation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.