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
  ModuleNotFoundError: No module named 'tkcalendar', Thonny, Windows 10 Edward_ 4 6,422 Apr-06-2025, 07:56 PM
Last Post: Edward_
  Building specific Python version on Raspberry PI 5 (Raspbian) andrewk 2 2,075 Feb-03-2025, 11:41 AM
Last Post: iterate
  search API calls in different format pythonnewbieee 4 2,155 Sep-23-2024, 04:14 AM
Last Post: Pedroski55
Question Sonar Code in Thonny for Pi Pico iansmiler 1 1,643 Nov-22-2023, 12:27 PM
Last Post: deanhystad
  Calls to Attributes of a Class SKarimi 3 5,354 Apr-22-2021, 04:18 PM
Last Post: SKarimi
  Difference between os.system("clear") and os.system("cls") chmsrohit 7 22,286 Jan-11-2021, 06:30 PM
Last Post: ykumar34
  Minimal python install for thonny ide DanielRossinsky 0 2,978 Jun-11-2020, 02:31 PM
Last Post: DanielRossinsky
  Does "import xlrd" work in Thonny? cnutakor 3 5,066 Apr-30-2020, 12:41 AM
Last Post: Larz60+
Question Difference between Python's os.system and Perl's system command Agile741 13 13,026 Dec-02-2019, 04:41 PM
Last Post: Agile741
  Need help with a function that calls other functions. skurrtboi 4 4,311 Sep-30-2019, 09:28 PM
Last Post: stullis

Forum Jump:

User Panel Messages

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