Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # !/usr/bin/python
- # coding = utf8
- # https://pyserial.readthedocs.io/en/latest/shortintro.html
- # Document Reference No.: FT_000161
- # Aliasing Vcp Baud Rates Application Note AN_120
- # Version 1.2
- # MSB of the divisor is 0, so divisor is dividing a 3 MHz clock.
- # Each field consists of 4 bytes, ordered as follows: Byte0, Byte1, Byte2, Byte3.
- # Bits 13 through 0 denote the integer divisor
- # while bits 16, 15 and 14 denote the sub-integer divisor, as follows
- # 10,27,00,00 are re-arranged as 00,00,27,10
- # This gives 0000 0000 0000 0000 0010 0111 0001 0000
- # 16,15,14 = 000 - sub-integer divisor = 0
- # FtdiPort.NT.HW.AddReg]
- # HKR,,"ConfigData",1,11,00,3F,3F,10,27,00,00,88,13,00,00,C4,09,00,00,E2,04,00,00,
- # 71,02,00,00,38,41,00,00,9C,80,00,00,4E,C0,00,00,34,00,00,00,1A,00,00,00,
- # 0D,00,00,00,06,40,00,00,03,80,00,00,00,00,00,00,D0,80,00,00
- # HKR,,"ConfigData",1,11,00,3F,3F,
- # 10,27,00,00, => divisor = 0'h2710 = 0'd10000, rate = 300
- # 88,13,00,00, => divisor = 5000, rate = 600
- # C4,09,00,00, => divisor = 2500, rate = 1,200
- # E2,04,00,00, => divisor = 1250, rate = 2,400
- # 71,02,00,00, => divisor = 625, rate = 4,800
- # 38,41,00,00, => divisor = 312.5, rate = 9,600
- # 9C,80,00,00, => divisor = 156, rate = 19,23075
- # 4E,C0,00,00, => divisor = 78, rate = 38,4615
- # 34,00,00,00, => divisor = 52, rate = 57,69225
- # 1A,00,00,00, => divisor = 26, rate = 115,3845
- # 0D,00,00,00, => divisor = 13, rate = 230,76925
- # 06,40,00,00, => divisor = 6.5, rate = 461,53825
- # 03,80,00,00, => divisor = 3.25, rate = 923,077
- # 00,00,00,00, => RESERVED
- # D0,80,00,00 => divisor = 208.25, rate = 14,40575
- # Altera Cyclone III clock is 64 MHz, so let's assume that we could manage to
- # achieve highest possible uart rate for FT2232H stated as 12 MBaud
- # At page 11/17 see 4.4 Aliasing the FT232H, FT2232H and FT4232H for Baud Rates
- # up to 12 MBaud sayng that to alias baud rates between 3 MBaud and 12 MBaud it is
- # necessary to use driver version 2.4.20 or later and the most significant bit
- # (MSB) of the divisor must be a 1. This will ensure the divisor is dividing a
- # 12 MHz clock and not a 3 MHz clock.
- # To alias a rate of 12 MBaud to the standard 921600 baud menu option under windows:
- # find the best divisor, check that it is sufficiently accurate and then modify the
- # appropriate INF file entry.
- # 12000000 / 921600 = 13.0208
- # The best divisor is 13.25
- # 12000000 / 13.25 = 905660 (approx)
- # 921600 / 905660 = 101.76%
- # This is just within the required 3% tolerance
- # Set bits 16:15:14 to 010 for a sub-integer divisor of 0.25, and the lower bits to
- # 0'd13 = 0'hD
- # MSB must be a 1 as the master clock is 12 MHz
- # This gives 1000 0000 0000 0000 1000 0000 0000 1101.
- # This gives the 4 bytes as 80 00 80 0D.
- # These are re-arranged as 0D,80,00,80
- # Replace the 03,80,00,00, entry for the 923077 baud with 0D,80,00,80.
- # Incorporating these two changes the FTDIPORT.INF entry becomes
- # FtdiPort.NT.HW.AddReg]
- # HKR,,"ConfigData",1,11,00,3F,3F,10,27,00,00,88,13,00,00,C4,09,00,00,E2,04,00,00,
- # 71,02,00,00,38,41,00,00,9C,80,00,00,4E,C0,00,00,34,00,00,00,1A,00,00,00,
- # 0D,00,00,00,06,40,00,00,0D,80,00,80,00,00,00,00,D0,80,00,00
- # To alias a rate of 12 MBaud to the standard 19200 baud menu option under windows:
- # Note: Divisor = 1 and Divisor = 0 are special cases. A divisor of 0 will give 12 MBaud, and a
- # divisor of 1 will give 8MBaud. Sub-integer divisors are not allowed if the main divisor (n) is
- # either 0 or 1.
- # So replace the 9C,80,00,00, entry for the 19,200 baud with 00,00,00,80.
- # Incorporating these two changes the FTDIPORT.INF entry becomes
- # FtdiPort.NT.HW.AddReg]
- # HKR,,"ConfigData",1,11,00,3F,3F,10,27,00,00,88,13,00,00,C4,09,00,00,E2,04,00,00,
- # 71,02,00,00,38,41,00,00,00,00,00,80,4E,C0,00,00,34,00,00,00,1A,00,00,00,
- # 0D,00,00,00,06,40,00,00,03,80,00,00,00,00,00,00,D0,80,00,00
- # https://superuser.com/questions/954184/disable-automatic-driver-installation-in-windows-10
- import os
- import serial
- import msvcrt
- print(" ////////////////////////////////////////////////////////////////////")
- print(" File name : comport.py ")
- print(" Author : Anatoly A. Sharapov ")
- print(" Email : a.a.sharapov@gmail.com ")
- print(" Phone : +7 903 231-11-26 ")
- print(" Company : PJS MIEA ")
- print(" Dep : 311 ")
- print(" Description : python comport script for reading from com port ")
- print(" Last revision : 30.03.2021 ")
- print(" ////////////////////////////////////////////////////////////////////")
- is_debug = 0
- is_log = 1
- ser_buf_size = 4096
- ser_baudrate = 19200
- is_crc8_included = 1
- if is_crc8_included:
- if is_debug:
- print("crc8 is included")
- packet_size = 4 # crc8, addr, data_high, data_low
- else:
- if is_debug:
- print("crc8 is not included")
- packet_size = 3 # addr, data_high, data_low
- ThisPCLocation = os.environ.get('ThisPCLocation')
- print("ThisPCLocation = {:s}".format(ThisPCLocation))
- if ThisPCLocation== "HOME":
- com_port = 'COM4'
- else: # ThisPCLocation== "MIEA":
- com_port = 'COM14'
- def _two_8bits_hex_to_16bits_hex(data_high_byte, data_low_byte):
- return data_high_byte << 8 | data_low_byte
- def CheckBits(bit1, bit2):
- if bit1 == bit2:
- print("Error occured")
- def ReadData(num_bytes):
- bytes_read = ser.read(num_bytes) # read up to num_bytes bytes
- # or as much is in the buffer
- num_bytes_read = len(bytes_read)
- return num_bytes_read != num_bytes, num_bytes_read, bytes_read
- def ParseData(bytes):
- addr = bytes[0]
- data = _two_8bits_hex_to_16bits_hex(bytes[1], bytes[2])
- return addr, data
- def ReadBuf(data_prev):
- bytes_read = ser.read(ser_buf_size) # read up to buf_size bytes
- # or as much is in the buffer
- num_bytes_read = len(bytes_read)
- if is_log:
- miea1_data_file.write("ReadBuf --------------> num_bytes_read = {:#d}\n".format(num_bytes_read))
- if is_debug:
- print("ReadBuf -> data_prev = {:#x}, num_bytes_read = {:#d}" .format(data_prev, num_bytes_read))
- i = 0
- addr_list = []
- if data_prev == 0:
- data_list = []
- else:
- data_list = [data_prev]
- while num_bytes_read >= i + packet_size:
- if is_crc8_included:
- addr, data = ParseData([bytes_read[i+1], bytes_read[i+2], bytes_read[i+3]])
- else:
- addr, data = ParseData([bytes_read[i], bytes_read[i+1], bytes_read[i+2]])
- if is_log:
- miea1_data_file.write("ReadBuf -> address = {:#o}, data = {:#x}\n".format(addr, data))
- if is_debug and i < packet_size:
- print("ReadBuf -> data_first = {:#x}" .format(data))
- if is_debug and i > num_bytes_read - packet_size:
- print("ReadBuf -> data_last = {:#x}" .format(data))
- addr_list.append(addr)
- data_list.append(data)
- i += packet_size
- if is_debug:
- print("ReadBuf -> data_last = {:#x}, len(data_list) = {:#d}" .format(data, len(data_list)))
- return num_bytes_read, addr_list, data_list
- def CheckDataList(data_list, buf_num):
- data_list_len = len(data_list)
- data_last = 0
- if is_debug:
- print("CheckDataList -> data_list_len = {:#d}".format(data_list_len))
- if data_list_len > 1:
- data_previous = data_list[0]
- if is_debug:
- print("CheckDataList -> data_previous = {:#x}".format(data_previous))
- for i in range(1, data_list_len):
- data_next = data_list[i]
- if is_debug:
- print("CheckDataList -> data_next = {:#x}".format(data_next))
- if not (data_next == data_previous + 1 or data_next == 0 and data_previous == 0xffff):
- print("At buf_num = {:#d}, num = {:#d} failure detected: data_previous = {:#x} data_next = {:#x}" .format(buf_num, i, data_previous, data_next))
- return False, data_last
- data_previous = data_next
- data_last = data_next
- return True, data_last
- return False, data_last
- ser = serial.Serial(port = com_port,
- baudrate = ser_baudrate,
- bytesize = 8,
- parity = 'N',
- stopbits = 1,
- timeout = None,
- xonxoff = 0,
- rtscts = 0)
- if not ser.is_open:
- ser.open()
- # https://pyserial.readthedocs.io/en/latest/pyserial_api.html
- ser.flush()
- ser.flush()
- print("Port {:s} is opened with baudrate = {:#d} (possibly aliased)".format(ser.name, ser_baudrate))
- miea1_data_file = open('miea1_data.txt', 'w')
- print("Com port reading and checking in progress and will be stopped")
- print("in case any error detected. Print any key to interrupt.")
- num = 0
- done = False
- # 1st read buf
- buf_num = 0
- num_bytes_read, addr_list, data_list = ReadBuf(0) # 1st buf - no prevous data
- progress = {
- 0 : '-',
- 1 : '\\',
- 2 : '|',
- 3 : '/'
- }
- while not done:
- if is_debug:
- print("num = {:#d}" .format(num))
- else:
- try:
- print(progress[buf_num % len(progress)], end = '\b', flush = True)
- except KeyError as e:
- # можно также присвоить значение по умолчанию вместо бросания исключения
- raise ValueError('Undefined unit: {}'.format(e.args[0]))
- done = True
- num += num_bytes_read
- CheckDataListResult, data_last = CheckDataList(data_list, buf_num)
- buf_num += 1
- if not CheckDataListResult:
- done = True
- if msvcrt.kbhit():
- print("\nYou pressed ", msvcrt.getch(),", so now the process will quit")
- done = True
- num_bytes_read, addr_list, data_list = ReadBuf(data_last)
- ser.close()
- miea1_data_file.close()
- if not ser.is_open:
- print ("Port is closed")
- # ThisPCLocation = HOME
- # Port COM4 is opened with baudrate = 19200 (possibly aliased)
- # Com port reading and checking in progress and will be stopped
- # in case any error detected. Print any key to interrupt.
- # At buf_num = 1850, num = 765 failure detected: data_previous = 0x3373 data_next = 0x8536
- # Port is closed
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement