Python Forum
Values into system calls (RPi 4 Raspbian Buster Thonny)
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Values into system calls (RPi 4 Raspbian Buster Thonny)
#1
import os
import time
os.system ("sudo modprobe snd-virmidi")
os.system ("aconnect 20:00 128:00")
b0=str(hex(12*12))[2:4]
b1=str(hex(5*12))[2:4]
b2=str(hex(100+27))[2:4]
while True:
  print (b0)
  print (b1)
  print (b2)
  os.system ("amidi -p hw:1,0 -S 'b0 b1 b2'")
  time.sleep(1)
This prints out the right values (90 3c 7f)
But the midi doesn't respond
If I replace b0 b1 b2 (next to last line) with 90 3c 7f
the synth plays ........... ??????????
Reply
#2
you need to construct the command string with the values of b0, b1 and b2

>>> b0 = hex(12*12)[2:4]
>>> b1 = hex(5*12)[2:4]
>>> b2 = hex(100+27)[2:4]
>>> cmd = ' '.join(["amidi -p hw:1,0 -S", b0, b1, b2])
>>> cmd
'amidi -p hw:1,0 -S 90 3c 7f'
>>> cmd = f"amidi -p hw:1,0 -S {b0} {b1} {b2}"
>>> cmd
'amidi -p hw:1,0 -S 90 3c 7f'
>>> b0 = 0x90
>>> b1 = 0x3c
>>> b2 = 0x7f
>>> cmd = f"amidi -p hw:1,0 -S {b0:x} {b1:x} {b2:x}"
>>> cmd
'amidi -p hw:1,0 -S 90 3c 7f'
>>>
also note you don't need to convert the result from hex() to str, it's already str.
by the way it's better to use subprocess module, e.g. subprocess.run(), as mentioned in the docs for os.system():

Quote:The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function. See the Replacing Older Functions with the subprocess Module section in the subprocess documentation for some helpful recipes.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question Sonar Code in Thonny for Pi Pico iansmiler 1 373 Nov-22-2023, 12:27 PM
Last Post: deanhystad
  Calls to Attributes of a Class SKarimi 3 3,411 Apr-22-2021, 04:18 PM
Last Post: SKarimi
  Difference between os.system("clear") and os.system("cls") chmsrohit 7 16,670 Jan-11-2021, 06:30 PM
Last Post: ykumar34
  Minimal python install for thonny ide DanielRossinsky 0 1,519 Jun-11-2020, 02:31 PM
Last Post: DanielRossinsky
  Does "import xlrd" work in Thonny? cnutakor 3 3,026 Apr-30-2020, 12:41 AM
Last Post: Larz60+
Question Difference between Python's os.system and Perl's system command Agile741 13 6,876 Dec-02-2019, 04:41 PM
Last Post: Agile741
  Need help with a function that calls other functions. skurrtboi 4 2,556 Sep-30-2019, 09:28 PM
Last Post: stullis
  Deluge (Python 2.7) on Raspbian with YARSS2 - certificate_transparency error Matt872000 0 1,544 Apr-21-2019, 02:39 AM
Last Post: Matt872000
  Testing function calls jenselme 1 2,695 Jul-25-2018, 10:33 AM
Last Post: Larz60+
  function state between calls Skaperen 5 5,160 Feb-08-2018, 02:20 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

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