WorldViz User Forum

WorldViz User Forum (https://forum.worldviz.com/index.php)
-   Vizard (https://forum.worldviz.com/forumdisplay.php?f=17)
-   -   Vizard Matlab interface - no data printing (https://forum.worldviz.com/showthread.php?t=5301)

shall6 03-10-2015 10:38 AM

Vizard Matlab interface - no data printing
 
Hi,

I've been trying to set up a way for matlab to talk to Vizard. Eventually, I want to be able to take sensor data that is real-time processed in matlab and use it in Vizard. Right now I'm just trying to set up a simple UDP connection. This is what I have:

MATLAB:
ip = 'LocalHost'; port = 4950;
u = udp(ip,port);
fopen(u)

fprintf(u, 'testdata')

Vizard:
import viz
import viznet
dataNetwork = viz.addNetwork('LocalHost');
def onNetwork(e):
if isinstance(e,viz.RawNetworkEvent):
print e.raw_data

viz.callback(viz.NETWORK_EVENT,onNetwork)

Nothing prints in Vizard. It just loads. I tried running the matlab code and the Vizard code in different orders. I've sent the matlab "fprintf" command again, then I run the Vizard code again. Matlab claims something sent, but I don't see it.

Eventually, I want to change "print e.raw_data" to "var = e.raw_data" and save the data as a variable, "var", then use the value of "var" as a command, the same way I would use a keyboard button-push.

What am I missing, and will saving as a variable work when it gets going?

Thank you,
Sherrie

farshizzo 03-10-2015 10:45 AM

Can you post the entire Vizard script and use the [code][/code] tags so the indentation is properly preserved?

shall6 03-10-2015 12:05 PM

Hi,

Sorry about that.
Code:

import viz
import viznet

dataNetwork = viz.addNetwork('LocalHost');
#viz.net.addPort(4960)
def onNetwork(e):
        if isinstance(e,viz.RawNetworkEvent):
                print e.raw_data

viz.callback(viz.NETWORK_EVENT,onNetwork)

It's just a test code to see if I can make this work, so that is all there is right now.
The matlab has no indents.

What I hope to do later:
Code:

dataNetwork = viz.addNetwork('LocalHost');
#viz.net.addPort(4960)
def onNetwork(e):
        if isinstance(e,viz.RawNetworkEvent):
                print e.raw_data
                if e.raw_data == 'command'
                        SetBrakeFlag(not BrakeFlag)

viz.callback(viz.NETWORK_EVENT,onNetwork)


farshizzo 03-10-2015 12:51 PM

The script is not creating the graphics window using viz.go(). Since no window is created, the script will immediately exit after reaching the end of the file. Try running the following code:

Code:

import viz
import viznet

viz.go()

dataNetwork = viz.addNetwork('LocalHost');
#viz.net.addPort(4960)
def onNetwork(e):
        if isinstance(e,viz.RawNetworkEvent):
                print e.raw_data

viz.callback(viz.NETWORK_EVENT,onNetwork)


shall6 03-12-2015 09:27 AM

Thanks for the assist - can't believe I missed that. Now I'm on to the hard part (been hunting through old threads but no answer).

When I run with the if statement, I can't get it to work. It has to do with the data coming through as e.raw_data. I tried converting it to a string but I still can't compare it. This is what I have after trying to convert BOTH my data and my if object (already a string) to strings. It still doesn't work. Of course it will recognize them as NOT equal... but that doesn't help me much. Why can't I actually turn this raw data into an object?

Code:

dataNetwork = viz.addNetwork('OEM-PC')
def onNetwork(e):
    if isinstance(e,viz.RawNetworkEvent):
        print e.raw_data
        v = str(e.raw_data)
        if v == str('hi'):
            print v

viz.callback(viz.NETWORK_EVENT,onNetwork)

Thanks!

farshizzo 03-12-2015 09:46 AM

Try using the following code to see what the contents of the raw_data attribute is:
Code:

def onNetwork(e):
    if isinstance(e,viz.RawNetworkEvent):
        print repr(e.raw_data)

Keep in mind that the raw data can contain trailing NULL (\x00) byte values, so you might need to account for this if you are expecting to receive a string.

shall6 03-12-2015 09:52 AM

That's exactly it

I got 'hi\n'

Any chance there's a simple way to remove the trailing parts?

farshizzo 03-12-2015 09:55 AM

You can use the following to strip trailing newline, space, and null byte characters:

Code:

v = e.raw_data.rstrip('\n\0 ')


All times are GMT -7. The time now is 02:23 AM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Copyright 2002-2023 WorldViz LLC