Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python-osc communication
#1
I'm trying to send some values to a csound instrument using the following code
its from an example found here: https://pypi.org/project/python-osc/

I'm getting an error for:
args = parser.parse_args()

im not sure what this means. But i am aware that i'm confused about the strings in the double quotes. for example where the code says "The ip of the osc server" is it meant to be what it is ? OR do I replace the text with the ip of the osc server if so, how do I get the ip of the osc server?


"""Small example OSC client

This program sends 10 random values between 0.0 and 1.0 to the /filter address,
waiting for 1 seconds between each value.
"""
import argparse
import random
import time

from pythonosc import osc_message_builder
from pythonosc import udp_client


if __name__ == "__main__":
  parser = argparse.ArgumentParser()
  parser.add_argument("--ip", default="127.0.0.1",
      help="The ip of the OSC server")
  parser.add_argument("--port", type=int, default=5005,
      help="The port the OSC server is listening on")
  args = parser.parse_args()

  client = udp_client.SimpleUDPClient(args.ip, args.port)

  for x in range(10):
    client.send_message("/filter", random.random())
    time.sleep(1) 
Reply
#2
please, post full traceback you get in error tags as well show us how you run it from command line. the script expects to supply ip of the OSC server as well as port. Default values are 127.0.0.1 for ip and 5005 for port. that is - OSC server is running on your computer (localhost). Is that the case?
Note that what you show is the client code. In the example there is also server part. Does it (or something like it) run somewhere?
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
#3
I ran the program like this;

$python3 sendosc.py

error tags like so?
Error:
File "sendosc.py", line 20 args = parser.parse_args() SyntaxError: invalid syntax
I have to write it out because i'm running the program on raspbian lite, but im on the forum here with OSX?

The port i want to communcate too in csound is on port 9999 so guess i make that the same for python? should my python script look more like this?

"""Small example OSC client
 
This program sends 10 random values between 0.0 and 1.0 to the /filter address,
waiting for 1 seconds between each value.
"""
import argparse
import random
import time
 
from pythonosc import osc_message_builder
from pythonosc import udp_client
 
 
if __name__ == "__main__":
  parser = argparse.ArgumentParser()
  parser.add_argument("--ip", default="127.0.0.1",
      help="127.0.0.1")
  parser.add_argument("--port", type=int, default=5005,
      help="9999")
  args = parser.parse_args()
 
  client = udp_client.SimpleUDPClient(args.ip, args.port)
 
  for x in range(10):
    client.send_message("/filter", random.random())
    time.sleep(1) 
...
The OSC server is running the same computer (localhost)

what does it mean when you say what i show is the client code, sorry?

the server part is another script that should be running at the same time? how do I do that?

Many many thanks!

...edit. i have ignored this part of the setup? is it necessary ?

$ python setup.py test
$ python setup.py install


a further edit sorry.
I just wanted share a video here for some more context. Im trying to make happen exactly what Pure Date is functioning as for csound here: https://www.youtube.com/watch?v=JX1C3TqP_9Y&t=191s

This video however doesn't mention about client and servers (if I can get an explanation?)

thanks again
Reply
#4
I tested your code and it works (I didn't test the osc part, just the argparse). Given that you say you had to write the error down, I guess that the code you post here is not copy/paste from the code you actually run. If you get SynatxError, sometimes the error is on the line before the one in the traceback. If I have to guess - you miss closing parenthesis or something on the line(s) before that. check carefully that you have exactly this:
parser.add_argument("--port", type=int, default=5005,
      help="The port the OSC server is listening on")
if you want, substitute 5005 with 9999 - if that is your port. this way you don't have to supply any command line arguments when running your script.
you say you have the server running on localhost and port 9999 so ignore the server part from my previous post.
(Jul-23-2018, 05:29 PM)fauveboy Wrote: i have ignored this part of the setup? is it necessary ?

$ python setup.py test
$ python setup.py install

if you installed it with pip (obviously you did), this is not necessary.
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
#5
(Jul-23-2018, 05:29 PM)fauveboy Wrote: I ran the program like this;

$python3 sendosc.py

error tags like so?
Error:
File "sendosc.py", line 20 args = parser.parse_args() SyntaxError: invalid syntax
I have to write it out because i'm running the program on raspbian lite, but im on the forum here with OSX?

Ok, so what's the arrow pointing to? It should let you know exactly what the error is. If not, then the error is probably on the line before (maybe you opened a parentheses but didn't close it?)
Like this, the arrow showing exactly where the issue is:
Error:
>>> 4 + ) File "<stdin>", line 1 4 + ) ^ SyntaxError: invalid syntax >>>
Reply
#6
Thank you there was a bracket missing, sorry for the poor debugging, however. It was in line before as what the arrow was pointing at didn't indicating anything out of the ordinary on the line it was on
I've now got the error:

$python3 simpleclient.py
Error:
Traceback (most recent call last): File "simpleclient.py", line 5, in <module> from pythonosc import osc_message_builder ImportError: No module named 'pythonosc'
Reply
#7
Unlike the previous error, there's no hidden meaning here. It can't find the module, which means it wasn't installed. What output do you get from running pip install python-osc?
Reply
#8
I thought so... the output reads the following:

$pip install python-osc
Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: python-osc in ./.local/lib/python2.7/site-packages (1.6.8)
Reply
#9
you have both python2 and python3 installed on your raspberry pi.
Currently using pip install python-osc the python-osc package is installed for python2, but you try to run the script with python3
try pip3 install python-osc
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
#10
that worked Smile the new error however...:

$python3 simpleclient.py 127.0.0.1 9999

Error:
Traceback (most recent call last): File "simpleclient.py", line 16, in <module> client = ump_client.SimpleUDPCClient(args.ip, args.port) AttributeError: 'Namespace' object has no attribute 'ip'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Good Example for bi-directional Communication between Python and Windows (C#) raybowman 0 1,600 Nov-14-2020, 06:44 PM
Last Post: raybowman
  packet radio communication EmpireAndrew 1 2,184 Nov-01-2019, 06:35 PM
Last Post: micseydel
  WiFi communication Milad 2 2,470 Sep-07-2019, 11:53 AM
Last Post: ndc85430
  Simple inter-process communication: Switching from Perl to Python Pappy 1 2,899 May-10-2019, 12:29 AM
Last Post: Pappy
  Serial communication with raspberry pi 3B and Xbee kj7 0 2,170 Mar-25-2019, 03:39 AM
Last Post: kj7
  Serial Communication Error AlphyOuseph 1 3,537 Feb-07-2019, 07:16 AM
Last Post: DeaD_EyE
  pi3 serial communication tony1812 0 2,151 Sep-23-2018, 10:29 PM
Last Post: tony1812
  Serial communication with a board Bokka 2 5,347 Dec-07-2017, 07:34 AM
Last Post: Bokka
  code pattern for process communication Skaperen 12 8,470 Oct-08-2017, 03:14 AM
Last Post: Skaperen
  Serial port communication FatimaSameer 1 3,222 Oct-03-2017, 09:47 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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