Page 1 of 1

Executing a file

Posted: Tue Feb 13, 2018 9:41 pm
by codGmer

I want to be able to execute a file called 'sendmidi' in a python script with parameters. This is the normal command:

Code: Select all

sendmidi dev 0 pb (variable)
where variable needs to get a variable ofcourse. The Path of the file is /home/pi/SendMIDI

I have tried using subprocess.Popen() but i always get errors doing that.

The variable is called 'pot1'

Re: Executing a file

Posted: Wed Feb 14, 2018 7:44 am
by jahboater
Try os.system() instead of popen().

You can just use "+" to concatenate strings.

Re: Executing a file

Posted: Wed Feb 14, 2018 12:14 pm
by pcmanbob
How did you do the call in subprocess.Popen() ?

This is an example of using subprocess.Popen() to call nmap I have added lots of comments explaining each line it might help you solve your problem.

Code: Select all

## get subprocess module 
import subprocess
## call Nmap command ##
p = subprocess.Popen("sudo nmap -sP -PI -PT | grep -i hosts", stdout=subprocess.PIPE, shell=True)
## Talk with Nmap command i.e. read data from stdout and stderr. Store this info in tuple ##
##Interact with process: Send data to stdin. Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate. The optional input argument should be a string to be sent to the child process, or None, if no data should be sent to the child.##
(output, err) = p.communicate()
## Wait for nmap to terminate. Get return returncode ##

## This line gets the command exit status and saves it as status this will be 0 if all is well ##
status = p.wait()

## This line saves the output of the namp command as resultline ##
resultline = output
Note the actual line to call nmap is enclosed with " "

Re: Executing a file

Posted: Wed Feb 14, 2018 7:19 pm
by codGmer
Thanks ppl, i got it working but a lot of the times when i run the code its running slowly.

Im sending simple midi signals to the pc.

Code: Select all

# -*- coding: utf-8 -*- Simple example of reading the MCP3008 analog 
# input channels and printing them all out. Author: Tony DiCola License: 
# Public Domain
import time
import os
import Adafruit_GPIO.SPI as SPI
import Adafruit_MCP3008

mcp = Adafruit_MCP3008.MCP3008(spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE))
while True:
 pot0 = str(mcp.read_adc(0) * 16)
 #if pot0c != pot0:
 os.system("./sendmidi dev f_midi pb " + pot0)
So i want to read the MCP3008 A/D Converter and pass it trough the file 'sendmidi'.

I want to be able to control a knob in a software called ableton.

Here's the slow running code: ... 4c7973.mp4

here's the fast running code: ... 937a64.mp4

I think it has something to do with the infinite loop not properly executing. Can you guys help to make it run smooth every time?