# This is a sample Python script.
import io
import os
import re
import sys
import time
import serial
import openpyxl
# for sheet with timestamp
from datetime import datetime
from openpyxl.styles import colors
from openpyxl.styles import Color, PatternFill, Font, Border
print(sys.argv[0]) # prints python_script.py
# Serial Port Init Related
ser = serial.Serial('/dev/ttyUSB1', 115200)
print("Serial Instance Configuration: ", {ser})
# opening the source excel file
#filename = "/home/user/PycharmProjects/readsepthread/Automation.xlsx"
filename = sys.argv[1]
#print("xls_fileName", {filename})
# workbook instance
wb1 = openpyxl.load_workbook(filename, data_only=True)
# worksheet1 from the above workbook
ws1 = wb1.active
# calculate total number of rows and columns in source excel file
mr = ws1.max_row
mc = ws1.max_column
print('total no of rows ', {mr}, 'total no of cols ', {mc})
# utility Methods
def _readline(self):
eol = b'\r'
leneol = len(eol)
line = bytearray()
while True:
c = ser.read(1)
if c:
line += c
if line[-leneol:] == eol:
break
else:
break
return bytes(line)
# utility Methods
def copytoNewSheet():
print("copytoNewSheet Method")
now = datetime.now() # current date and time
year = now.strftime("%Y")
month = now.strftime("%m")
day = now.strftime("%d")
time = now.strftime("%H %M %S")
# print("time:", time)
date_time_sheet = now.strftime("%d %b %Y " + time)
target = wb1.copy_worksheet(ws1)
target.title = str(date_time_sheet)
# saving the destination excel file
wb1.save(str(filename))
wb1.close()
# serial port Methods
def close_port():
ser.close()
# serial port Methods
def open_port():
if ser.isOpen():
ser.close()
ser.open()
time.sleep(1)
print('SA8295P_v2.1_ft0_ADP_Air_v1.0.1_UFS_NORMAL')
def rd_frm_xl_wrt_to_ser():
ser.flush()
# Writing to port
for row in ws1.iter_rows(min_row=2, max_row=mr, min_col=2, max_col=2, values_only=True):
for cell in row:
# print(cell)
ser.write(str.encode(cell))
ser.write(str.encode("\r"))
time.sleep(1)
time.sleep(1)
def rd_frm_ser_wrt_xls():
# Reading from a serial port
expected_output_LIST = []
mr = ws1.max_row
index_value = 1
pairsofdata = " "
MAINCOLLECTOR = []
a = None
b = None
while True:
try:
print(f"Entering Iteration - {index_value}")
if ser.inWaiting() >= 0:
ourStr = ser.readline().decode('utf-8').strip()
print("ourStr: ", ourStr)
if not pairsofdata.endswith(" ~~ "):
pairsofdata = pairsofdata + ourStr + " ~~ "
continue
pairsofdata = pairsofdata + ourStr
print("This is the pair of data")
print(pairsofdata)
count = pairsofdata.count(" ~~ ")
print("Count of Tildes:", count)
if count == 1:
MAINLIST = pairsofdata.split(" ~~ ")
print("MAINLIST :", MAINLIST)
print()
if len(MAINLIST) == 2:
####### Extracting the second element from MAINLIST
expectedOutput = ws1.cell(row=index_value + 1, column=3).value
print("This is expected value:")
print(expectedOutput)
if MAINLIST[1] == expectedOutput:
print(f"I have found the data {MAINLIST[1]}")
Res = ws1.cell(row=index_value + 1, column=4)
Res.value = 'Pass'
my_green = openpyxl.styles.colors.Color(rgb='00FF00')
my_fill = openpyxl.styles.fills.PatternFill(patternType='solid', fgColor=my_green)
Res.fill = my_fill
Reason = ws1.cell(row=index_value + 1, column=5)
Reason.value = 'Both the Actual output and Expected Output Matches,Hence TC is Pass.'
else:
Res = ws1.cell(row=index_value + 1, column=4)
Res.value = 'Fail'
my_red = openpyxl.styles.colors.Color(rgb='00FF0000')
my_fill = openpyxl.styles.fills.PatternFill(patternType='solid', fgColor=my_red)
Res.fill = my_fill
Reason = ws1.cell(row=index_value + 1, column=5)
reason_str = ('ExpectedOutput is: ', str(expectedOutput), 'Actual Output is: ', str(ourStr))
Reason.value = str(reason_str)
####### Post Processing and clearing data
pairsofdata = " "
index_value += 1
if index_value == mr:
break
continue
continue
if index_value == mr + 15:
break
index_value += 1
except Exception as e:
print(" Interrupt Error is here :-- ")
print(e)
break
wb1.save(filename)
wb1.close()
def rd_ln_by_ln():
index_value = 1
remoteIndexing = 0
pairsofdata = " "
MAINCOLLECTOR = []
a = None
b = None
while True:
print("index_value: ", index_value)
CURRENT_COMMAND = ws1.cell(row=index_value + 1, column=2).value
print("CURRENT_COMMAND:", CURRENT_COMMAND)
NEXT_COMMAND = ws1.cell(row=index_value + 2, column=2).value
print("NEXT_COMMAND:", NEXT_COMMAND)
if ser.inWaiting() >= 0:
ourStr = ser.readline().decode('utf-8').strip()
print("ourStr: ", ourStr)
if ourStr == CURRENT_COMMAND:
print("SAME")
index_value += 1
continue
if ourStr != NEXT_COMMAND:
MAINCOLLECTOR.append(ourStr + "\n")
remoteIndexing += 1
index_value += 1
ROW_NUMBER = index_value + 1 - remoteIndexing
if __name__ == '__main__':
open_port()
rd_frm_xl_wrt_to_ser()
rd_frm_ser_wrt_xls()
copytoNewSheet()
close_port()
Wednesday, March 29, 2023
Read Write Serial Data (PySerial) Qualcomm QNX OS Board
Friday, August 7, 2020
recursion x^y ( X power Y) where X and y are input.
/*A recursive algorithm is an algorithm that calls itself.
* A recursive algorithm has Base case:
* output computed directly on small inputs
Recursive algorithms can be used to solve some problems such as Towers of Hanoi (TOH),
Inorder/Preorder/Postorder
Tree
Traversals,
DFS
of
Graph,
easily.
Recursion techniques can be also divided into following types:
1. Linear Recursion
2. Binary Recursion
3. Tail Recursion
4. Mutual Recursion
5. Nested Recursion
*
* Given Problem Statement.
*Write a program to calculate using recursion x^y ( X power y) . X and y are input.
Calculate time complexity of written code.
Write a better solution in terms of time complexity.
Calculate time complexity again for a better solution
*/
////////* Solution *//////
//As the output depends on every element of the input. O(n) or O(n*log(n)) are the best results of time complexities in this case.
//For small number of inputs the O(n log n) < O(n) ? Let n = 10 then 10*log(10) =10*1=10 which is obviously same value.
//Let n = 5 then 5*log5 = 3.49 which is obviously smaller than 5,which is better than O(5).
//Aiming for O(1) which is constant time but for the recursion problems it is difficult to achieve.
/*
*
* one big concern is the recursion depth (how many times the algorithm calls itself) .
* If the depth is small, recursive algorithms are often a good solution,and O(n*log(n)) Time complexity holds good.
* If the recursion depth is large, then the better time complexity tends to O(n),when the input is large number.
* If the recursion depth is huge, then running out of stack memory becomes a real concern,
* hence Iterative solutions (using loops) can be preferred to recursive algorithms.
*
*
*/
////////* Solution *//////
#include <chrono>
#include <sstream> // for std::stringstream
#include <iostream>
using namespace std;
using namespace std::chrono;
typedef high_resolution_clock
Clock;
typedef Clock::time_point ClockTime;
//Iterative Solution Non-Recursive [Time-Complexity O(log(n))]
long pow(int x, int n)
{
long pow = 1;
while ( n )
{
if ( n & 1 )
{
pow = pow * x;
--n;
}
x = x*x;
n = n/2;
}
return pow;
}
// Iterative solution to calculate pow(x, n) using binary operators [Time-Complexity O(log(n))]
int powB(int x, unsigned n)
{
// initialize result by 1
int pow = 1;
// do till n is not zero
while (n)
{
// if n is odd, multiply result by x
if (n & 1)
pow *= x;
// divide n by 2
n = n >> 1;
// multiply x by itself
x = x * x;
}
// return result
return pow;
}
//Tail Recursion [Time-Complexity O(n)]
float power(int x, unsigned n)
{
if (x==0)
{
return 0;
}
else if(n==0)
{
return 1;
}
else if (n>0)
{
return( x* power(x,n-1));
}
else
{
return ((1/x)*power(x,n+1));
}
}
void printExecutionTime(ClockTime start_time, ClockTime end_time);
int main(int argc , char*argv[])
{
////////////////////////////////////////////////////////
//X and y are inputs.//reading x,y input from Commandline.
cout << "Program name " <<argv[0];
std::stringstream convertx{ argv[1]};
std::stringstream convertn{ argv[2]};
int x;
unsigned int n;
if (!(convertx >> x) && !(convertn >> n)) {// do the conversion
x = 0; // if conversion fails, set x,n to a default value
n = 0;
}
////////////////////////////////////////////////////////
ClockTime start_time = Clock::now();
// power(x,n);
// pow(x,n);
powB(10,10);
////////////////////////////////////////////////////////
ClockTime end_time = Clock::now();
printExecutionTime(start_time, end_time);
}
void printExecutionTime(ClockTime start_time, ClockTime end_time)
{
auto execution_time_ns
= duration_cast<nanoseconds>(end_time - start_time).count();
auto execution_time_ms
= duration_cast<microseconds>(end_time - start_time).count();
auto execution_time_sec
= duration_cast<seconds>(end_time - start_time).count();
auto execution_time_min
= duration_cast<minutes>(end_time - start_time).count();
auto execution_time_hour
= duration_cast<hours>(end_time - start_time).count();
cout << "\nExecution Time: ";
if(execution_time_hour > 0)
cout << "" << execution_time_hour
<<
"
Hours,
";
if(execution_time_min > 0)
cout << "" << execution_time_min
% 60 << " Minutes, ";
if(execution_time_sec > 0)
cout << "" << execution_time_sec
% 60 << " Seconds, ";
if(execution_time_ms > 0)
cout << "" << execution_time_ms
% long(1E+3) << " MicroSeconds, ";
if(execution_time_ns > 0)
cout << "" << execution_time_ns
% long(1E+6) << " NanoSeconds, ";
}
//Understanding
The iterative solution
performs much better than the recursive one, the recursive solution need more memory for function call stacks so
slow.