Basically I'm supposed to test modbus functionality right now and for whatever reason modbus fails to read the registers every second call, ex:
Functions:
# Modbus functions
def read_system_uptime(c, ssh):
try:
# Get uptime with ubus
ssh_result = str(exec_ssh("ubus call system info", ssh))
ssh_obj = json.loads(ssh_result)
uptime = str(ssh_obj['uptime'])
regs = c.read_holding_registers(1, 2)
if (regs != None and regs[0] == 0):
result = check_match(uptime, regs[1])
logger("System uptime", uptime, regs[1], result)
elif (regs != None and regs[0] != 0):
#Check results
temp = regs[0] * 65536 + regs[1]
result = check_match(uptime, temp)
logger("System uptime", uptime, temp, result)
else:
error_logger("System hostname", "Could not read register value")
except Exception as error:
error_logger("system uptime", error)
def read_mobile_strength(c, ssh):
try:
regs = c.read_holding_registers(3, 2)
if regs != None:
bits = decimal_To_Binary(regs[1])
a = str(binary_To_Complement(bits)).strip()
#Check results
gsmctl = str(exec_ssh("gsmctl -q", ssh)).strip()
result = check_match(gsmctl, a)
logger("Mobile signal strength", gsmctl, a, result)
else:
error_logger("Mobile signal strength", "Could not read register value")
except Exception as error:
error_logger("Mobile signal strength", error)
def read_system_hostname(c, ssh):
try:
regs = c.read_holding_registers(7, 16)
if regs != None:
a = str(dec_to_text(regs)).strip()
#Check results
name = str(exec_ssh("uci get system.system.hostname", ssh)).strip()
result = check_match(name, a)
logger("System hostname", name, a, result)
else:
error_logger("System hostname", "Could not read register value")
except Exception as error:
error_logger("System hostname", error)
Main:
# Main fuction
def main():
#router_ip, router_port, router_username, router_password = run_args(sys.argv[1:])
ssh = paramiko.SSHClient()
if ssh_connect(ssh, router_ip, router_port, router_username, router_password) != False:
modbus = modbus_start(router_ip, modbus_port, modbus_id)
if modbus != False:
read_system_hostname(modbus, ssh)
read_system_uptime(modbus, ssh)
read_mobile_strength(modbus, ssh)
# Run main()
if __name__ == '__main__':
main()
If I run the code like this, I won't get value for system uptime.
if modbus != False:
read_system_hostname(modbus, ssh)
read_mobile_strength(modbus, ssh)
read_system_uptime(modbus, ssh)
And Now I won't get mobile strength value, this happens with every second function, I can move those functions around, like move them to first position and it will work, so it's not functions fault, my guess it's pyModbusTCP or maybe even the devices that I'm working with fault, but I am not sure. Anybody knows how to deal with this? Because I never worked with this library before. Do I have to clear out the data I read or something like that? Currently I have one stupid solution of just placing a "Fake" function that reads the register and does nothing with it and place it in between others, so important ones would work, but I would like to avoid that if possible.
EDIT:
Modbus start function:
def modbus_start(ip, port, device):
try:
client = ModbusClient(host=ip, port=port, unit_id=device, auto_open=True)
print("Modbus client start: Success")
logging.info("Started Modbus client")
return client
except Exception as error:
print("ERROR: Failed to start Modbus client: ")
print(error)
logging.error("Failed to start Modbus client: %s", error)
return False
I am struggling to click over an element. I am not able to click on the contact element from the attach icon in whatsapp chat. i am using url= "web.whatsapp.com".
Also is there any other option of doing the task?
I get "NoSuchElementException" error when i run the below code. Please help me to select it correctly.
try:
chat_button = driver.find_element_by_xpath("//a[#id = 'action-button']").click()
time.sleep(2)
element_presence(By.XPATH,'//*[#id="main"]/footer/div[1]/div[2]/div/div[2]',30)
msg_box=driver.find_element(By.XPATH , '//*[#id="main"]/footer/div[1]/div[2]/div/div[2]')
msg_box.send_keys(message + Keys.ENTER)
time.sleep(2)
attach_icon = driver.find_element_by_xpath("//div[#title = 'Attach']").click()
# issue starts from here...
contact_icon = driver.find_element_by_xpath("//svg[#id = 'contact-Layer_1']")
contact_icon.click()
time.sleep(2)
search_contact = driver.find_element_by_xpath("//input[#title = 'search']")
search_contact.click()
search_contact.send_keys("pravin tcs")
driver.find_element_by_xpath("//div[#class = '_1kfc8_2uQfJ']").click()
driver.find_element_by_xpath("//span[#data-icon = 'send-light']").click()
except Exception as e:
print("Invalid phone no :"+str(phone_no))
I am getting input url's and trying to load it in the browser (Firefox - Updated version). The script has been working for all the url's. Except few URLs return this error
"No modal dialog is currently open"
I tried adding driver.switch_to_alert().accept() but no help. I usually end up closing browser and restart it all over again.
Please find the code :
capabilities = DesiredCapabilities.FIREFOX.copy()
capabilities['marionette'] = True
capabilities['acceptSslCerts'] = True
driver = webdriver.Firefox(capabilities=capabilities)
driver.implicitly_wait(3)
print ("Number of rows to be processed is:" + str(ws1.max_row))
for row in range(2,ws1.max_row+1):
try:
region = ws1["E"+str(row)].value
# Reading Row 'row'
print("Reading row #"+str(row))
url = ws1["C"+str(row)].value
regex = df[df['Country'] == region]['Regex'].values[0]
#Load mainpage or input URL
i = 0
while i < 3:
try:
driver.get("http://"+url)
driver.switch_to_alert().accept()
time.sleep(5)
break
except (TimeoutException,WebDriverException,NoSuchElementException) as e:
print(e,'Retrying...',i+1)
i += 1
Sample URLs :
afilias.info
www.nuwavenow.com
www.IntoTomorrow.com
www.picobrew.com
I am creating a tool that gives an overview of hundredths of test results. This tool access a log file, checks for Pass and Fail verdicts. When it is a fail, I need to go back to previous lines of the log to capture the cause of failure.
The linecache.getline works in my workspace (Python Run via eclipse). But after I created a windows installer (.exe file) and installed the application in my computer, the linecache.getline returns nothing. Is there something I need to add into my setup.py file to fix this or is it my code issue?
Tool Code
precon:
from wx.FileDialog, access the log file
self.result_path = dlg.GetPath()
try:
with open(self.result_path, 'r') as file:
self.checkLog(self.result_path, file)
def checkLog(self, path, f):
line_no = 1
index = 0
for line in f:
n = re.search("FAIL", line, re.IGNORECASE) or re.search("PASS", line, re.IGNORECASE)
if n:
currentline = re.sub('\s+', ' ', line.rstrip())
finalresult = currentline
self.list_ctrl.InsertStringItem(index, finaltestname)
self.list_ctrl.SetStringItem(index, 1, finalresult)
if currentline == "FAIL":
fail_line1 = linecache.getline(path, int(line_no - 3)) #Get reason of failure
fail_line2 = linecache.getline(path, int(line_no - 2)) #Get reason of failure
cause = fail_line1.strip() + " " + fail_line2.strip()
self.list_ctrl.SetStringItem(index, 2, cause)
index += 1
line_no += 1
The issue was resolved by doing the get_line function from this link:
Python: linecache not working as expected?
I'm getting the following error when updating text boxes:
load glyph failed err=23 face=0x0, glyph=19
All of the lines responsible for updating the text boxes look like this the par:
self.lnePer1_temp.setText(str(values_s[0]))# + " °C")
The method which aggregates these methods is called like this:
def timer_handler(self):
self.pressedSearchDevices()
if self.grbKeys1.isEnabled():
self.pressedKey1_bat()
if self.grbKeys2.isEnabled():
self.pressedKey2_bat()
if self.grbPer1.isEnabled():
self.pressedPer1_read_all()
if self.grbPer2.isEnabled():
self.pressedPer2_read_all()
threading.Timer(60, lambda: self.timer_handler()).start()
Basically it checks periodically whether some devices are available and updates the textboxes accordingly. When commented out - everything works fine. The full method responsible for updating the textboxes is:
def pressedPer2_read_all(self):
call('./reset_ble.sh', shell=True)
cmd = "sudo node Per_read_all.js " + Per_2.lower()
output = 'None'
with Popen(cmd, shell=True, stdout=PIPE, preexec_fn=os.setsid) as process:
try:
output = process.communicate(timeout=5)[0]
except TimeoutExpired:
os.killpg(process.pid, signal.SIGINT) # send signal to the process group
output = process.communicate()[0]
print(output)
output_s = output.decode("utf-8")
values_s = output_s.split('\n')
for s in values_s: print(s + " °C")
if len(values_s) == 7:
self.lnePer2_temp.setText(str(values_s[0]))# + " °C")
self.lnePer2_dig.setText(str(values_s[1]))
self.lnePer2_light.setText(str(values_s[2]))# + " %")
self.lnePer2_hum.setText(str(values_s[3]))# + " %")
self.lnePer2_alert.setText(str(values_s[4]))
self.lnePer2_but.setText(str(values_s[5]))
else:
self.lnePer2_temp.setText(" °C")
self.lnePer2_dig.setText(" ")
self.lnePer2_light.setText(" %")
self.lnePer2_hum.setText(" %")
self.lnePer2_alert.setText(" ")
self.lnePer2_but.setText(" ")
When replacing the if-else statement with only the else statement - everything works fine, so I'm assuming that the error lies in the change of values or something like that. Similar error messages link to Qt, so I'm also assuming that the fault lies somewhere there.
The script is running on a Raspberry Pi Zero, the Python Version used is Python 3.4.2, the PyQt5 version is 5.3.2.