程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python socket uses UDP control relay for hardware product cycle on-off test

編輯:Python

   One : explain

In the hardware acquisition system test , Switching on and off the machine is also a very important test item . This test can evaluate the stability of the product under abnormal hardware switching . Hardware environment construction reference : Build network products 、 Internet of things hardware products automatic on-off test environment _ Li Xi's blog -CSDN Blog

Two : Tools used

         Hardware :

                 PC The computer 、WIFI Relay 、 wireless router (2.4G)

         Software :

                  Network assistant 、python Script

Two : Test topology

3、 ... and : Conceive code logic

1) Judge the identifier according to different products , For example, network products can ping Pass indicates that the device startup is completed , You can use ping Test the startup of the equipment , testing OK After that, power off and power on .

2) There are three kinds of delay options for power on after power off ,(1): Power on after a fixed time delay after power failure .(2) After power failure, power on at random within the delay range .(3) : Start a delay and step , After power failure, power on by means of increasing delay .

According to the above judgment, you can start the base code , The environment is used python3.6 Environment .

import datetime
import socket
import time
import os
import random
count = 1
delay_time = open_time = 0
relay_mac = "xxxxxxxxxxxx"
# A prelude to the beginning of the program , De link UDP And the identifier of the subsequent disconnection relay ( Here is to ping through DUT To judge )
def go_start(interface_addr, relay_addr, dut_addr, up_type, relay_num):
global delay_time, count, open_time
# to udp Local connection
try:
udpSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
udpSocket.bind(interface_addr)
# Exit after exception
except Exception as e:
print(" Binding failure , Please check the local IP Or check out IP Is it bound !",e)
exit(-1)
# Relay operation first open the relay before operation , Commands are fixed format , for example a1xxxxxxxxxx yes a1 + xxxxxxxx
#a1 It is the first circuit of the relay ,bcff4d2b66eb It's a relay mac Address
command = "a" + relay_num + relay_mac
result = relay_control(udpSocket, relay_addr, command)
if result == False:
print(" Command sending failed three times , sign out !")
exit(-2)
else:
recv_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
open_time = round(time.time())
print(recv_time,"----> Control relay opened successfully .")
# Do business processes
try:
# Make a circular judgment ping operation , If ping The impasse will go through four cycles ping, The relay will not be operated
while True:
pingResult = os.popen('ping -n 2 -w 1 %s'%dut_addr).read()
#print(pingResult)
# This refers specifically to the use of Chinese windows System , At present, there is no understanding of English windows The operating system and linux System adaptation
if ' request timeout ' not in pingResult and ' Unable to access the target network ' not in pingResult:
#ping Disconnect the power supply after connecting the tested equipment
command = "b" + relay_num + relay_mac
result = relay_control(udpSocket, relay_addr, command)
# The returned result is True After that, power-off operation will be carried out
if result:
end_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
close_time = round(time.time())
time_t = close_time - open_time
print(end_time, "---->ping Pass the tested equipment , Relay de energized successfully ! The number of tests is :%d,"
" Total time from Startup to power failure %s"%(count, time_t))
sec = down_time(up_type)
time.sleep(sec)
count += 1
delay_time = sec
# Open relay command
command = "a" + relay_num + relay_mac
result = relay_control(udpSocket, relay_addr, command)
if result == False:
print(" Command sending failed three times , sign out !")
exit(-2)
start_time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
open_time = round(time.time())
print(start_time, "----> The relay is turned on successfully ! The delayed startup time :%d" % sec)
else:
time.sleep(2)
#pass
udpSocket.close()
except KeyboardInterrupt:
exit(0)
# adopt UDP Control data transmission
def relay_control(udpSocket, relay_addr, command):
send_count = 0
while send_count < 3:
try:
#UDP Send destination address and port
send_dest_addr = (relay_addr, 8111)
# Set up UDP Timeout for
udpSocket.settimeout(2)
"""
UDP Send and receive of , Because of the network and so on , If the sender does not receive a reply, it will be stuck
recvData Inside , Here is the exception that sets the timeout time and captures the timeout , After the exception, it will try to send again .
exceed 3 The relay is judged not to be online if all the times of transmission are timed out , sign out .
"""
udpSocket.sendto(command.encode('utf-8'), send_dest_addr)
recvData = udpSocket.recvfrom(1024)
return True
except socket.timeout:
send_count += 1
return False
# Filter according to the selected startup type , It corresponds to fixed delay startup or random or ascending order
def down_time(up_type):
global delay_time
# Start type selection 1 It's a fixed pattern , The default is after power failure 5s Power up
if up_type == 1:
sec_t = 5
return sec_t
# Start type selection 2 Is incremental mode , By default, the maximum limit is 3600s. Incremental is 1s.
elif up_type == 2:
if delay_time > 3600:
delay_time = 0
step = 1
sec_t = delay_time + step
return sec_t
# Start type selection 3 It's a random pattern , The default is that the delay is after each power failure 5~15s Randomly select a second for power-off operation .
elif up_type == 3:
sec_t = random.randint(5, 15)
return sec_t
else:
print(" Stop type setting error , sign out !")
exit(-3)
if __name__ == '__main__':
# Configure the test computer IP And port , The port is fixed
interface_addr = ("xxx.xxx.xxx.xxx",8112)
# Control relay IP Address
relay_addr = "xxx.xxx.xxx.xxx"
# Configure the device under test IP Address
dut_addr = "xxx.xxx.xxx.xxx"
#dut_addr = "127.0.0.1"
# Configure how long to turn it on again after it is turned off 1: Fixed intervals 2: Increment interval 3: Random interval
up_type = 3
# The circuit used by the relay corresponds to the circuit of the relay 1~4 road
relay_num = "4"
# Program entrance
go_start(interface_addr, relay_addr, dut_addr, up_type, relay_num)

  The above code is in the form of ping As a judgment mark, conduct power-off operation , The results are as follows :


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved