0

I have this script which runs an infinite loop. I got it from the github page of the company that makes these devices. It reads from a serial port. The device connected to that port is a data acquisition device. I'm interested in reading voltage.

import serial
import keyboard
import time
import sys
import struct

#PSerial 3.5 is in use for this example
#
#Press key 'x' to EXIT

CONST_SER_PORT = 'COM11'   #get the com port from device manger and enter it here

serDataq = serial.Serial(
    port = CONST_SER_PORT,
    timeout= 0.5
    )

serDataq.write(b"stop\r")        #stop in case device was left scanning
serDataq.write(b"encode 0\r")    #set up the device for binary mode
serDataq.write(b"slist 0 0\r")   #scan list position 0 channel 0 thru channel 7
serDataq.write(b"slist 1 1\r")   #scan list position 0 channel 0 thru channel 7
# serDataq.write(b"slist 2 2\r")   #scan list position 0 channel 0 thru channel 7
serDataq.write(b"srate 6000\r") 
serDataq.write(b"dec 250\r") 
serDataq.write(b"deca 1\r")
serDataq.write(b"ps 0\r")        #if you modify sample rate, you need modify this accordingly


time.sleep(0.5)
while True:
    try:
        i= serDataq.in_waiting
        if i>0:
            response = serDataq.read(i)
            break
    except:
        pass
 
serDataq.reset_input_buffer()
serDataq.write(b"start\r")       #start scanning

numofchannel=1 #if you modify the slist, you need modify this accordingly
numofbyteperscan=2*numofchannel

while True:
    try:
        if keyboard.is_pressed('x'):    #if key 'x' is pressed, stop the scanning and terminate the program
            serDataq.write(b"stop\r")
            time.sleep(1)           
            serDataq.close()
            print("Good-Bye")
            break
        if keyboard.is_pressed('p'):    #if key 'p' is pressed, pause the scanning
            serDataq.write(b"stop\r")
            time.sleep(1)           
            print("Paused")
            
        if keyboard.is_pressed('s'):    #if key 's' is pressed, start the scanning
            serDataq.reset_input_buffer()
            serDataq.write(b"start\r")
            time.sleep(1)           
            print("Start")
            
        else:
            i= serDataq.in_waiting
            if (i//numofbyteperscan)>0:
                #we always read in scans
                response = serDataq.read(i - i%numofbyteperscan)
                
                count=(i - i%numofbyteperscan)//2
                
                response2=bytearray(response)
                
                Channel=struct.unpack("<"+"h"*count, response2)

                #print all
                print (round((Channel[0]/32768*10), 3), end='\r') #'    ', round((Channel[1]/32768*10), 3), '    ', end='\r')
            pass
    except:
        pass

I want to run this script simultaneously with another one. The other script sometimes needs the value that this one prints. How can I make the other script communicate with this one to get that value?

1 Answers1

0

This is what I've been able to come up with so far thanks to the direction @jarmod pointed me to. I will incorporate this concept.

Script 1:

import time
from multiprocessing.connection import Listener
import threading

   
def loop(a):
        while True:  # the loop that reads from the device will be here
            a['value'] = a['value'] + 1
            print(a['value'], end='\r')

            time.sleep(1)

a = {'value': 5}
address = ('localhost', 6000)  

t = threading.Thread(target=loop, args=(a,))
t.start()

while True:
    with Listener(address, authkey=b'secret password') as listener:
        conn =  listener.accept()
        print('connection accepted from', listener.last_accepted)
        conn.send(a['value'])
        conn.close()
        listener.close()


    time.sleep(0.5)

Script 2:

from multiprocessing.connection import Client

address = ('localhost', 6000)

with Client(address, authkey=b'secret password') as conn:
    print(conn.recv())