Python Forum
Grabbing a value from one python script into another
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Grabbing a value from one python script into another
#3
That's great and works a treat but the revsPC data is generated from a value inside a UDP packet where the connection is established at the start of the script and the the revsPC variable is inside a loop. I know my syntax is not great or neat but it does do the basic job of determining my revsPC value.
import sys
import socket
import time

UDP_IP = ""
UDP_PORT1 = 40002
sock1 = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock1.bind((UDP_IP, UDP_PORT1))

#definition of the function for mapping a range values to another
def translate(value, inputMin, inputMax, outputMin, outputMax):
    
	# Figure out how 'wide' each range is
	inputSpan = inputMax - inputMin
	outputSpan = outputMax - outputMin

	# Convert the input range into a 0-1 range (float)
	valueScaled = float(value - inputMin) / float(inputSpan)

	# Convert the 0-1 range into a value in the output range.	
	return outputMin + (valueScaled * outputSpan)

counter = 0
while True:

	#Beginning of the program
	tmps1=time.time()

	data, addr = sock1.recvfrom(1024) # length of the buffer : 1024 bytes
	hiValRPM = data1[0]
	loValRPM = data1[1]

	print("--------------------------")
	print("Data Received : " + str(hiValRPM) + "/" + str(loValRPM))
	print(" ")

	#Revs : Mapping from 0-1023 to 0-6000
	if(hiValRPM == 0):
		revsPC = translate(loValRPM, 0, 255, 0, 1503)
	elif(hiValRPM == 1):
		revsPC = translate(loValRPM, 0, 255, 1504, 3004)
	elif(hiValRPM == 2):
		revsPC = translate(loValRPM, 0, 255, 3005, 4503)
	elif(hiValRPM == 3):
		revsPC = translate(loValRPM, 0, 255, 4504, 6000)

	print("Revs = " + str(int(revsPC)))

	#Calculation of the program execution time
	tmps2=(time.time()-tmps1)*1000
	print ("Execution time = %f ms" %tmps2)

	counter += 1
	
	print("Packet number : " + str(counter))
Reply


Messages In This Thread
RE: Grabbing a value from one python script into another - by nickayres - May-24-2019, 03:57 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,738 Jun-29-2023, 11:57 AM
Last Post: gologica
  How to kill a bash script running as root from a python script? jc_lafleur 4 6,168 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 2,421 May-28-2020, 05:27 PM
Last Post: micseydel
  Grabbing comma separed values from SQLite and putting them in a list PythonNPC 8 4,252 Apr-10-2020, 02:39 PM
Last Post: buran
  Grabbing a Subset of a String acemurdoc 3 2,713 Jun-18-2019, 04:57 PM
Last Post: perfringo
  Package python script which has different libraries as a single executable or script tej7gandhi 1 2,717 May-11-2019, 08:12 PM
Last Post: keames
  Grabbing questions from a quiz app CodingUnicorn 0 2,033 Mar-05-2019, 11:26 PM
Last Post: CodingUnicorn
  How to run python script which has dependent python script in another folder? PrateekG 1 3,242 May-23-2018, 04:50 PM
Last Post: snippsat
  How to call one python script and use its output in another python script lravikumarvsp 3 32,623 May-16-2018, 02:08 AM
Last Post: lravikumarvsp
  Check Python version from inside script? Run Pythons script in v2 compatibility mode? pstein 2 9,949 Jul-07-2017, 08:59 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020