首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >Hololens TCP套接字- Python客户端到Hololens服务器

Hololens TCP套接字- Python客户端到Hololens服务器
EN

Stack Overflow用户
提问于 2018-07-18 22:26:28
回答 1查看 2.7K关注 0票数 4

在经历了几周的挫折之后,我终于能够将字符串从Python客户端发送到Hololens服务器。代码如下所示,运行良好。但是,我想知道是否有人经验丰富的套接字可以帮助我修改这段代码,以发送一个openCV网络摄像头帧(所以基本上只是发送一个图像)从Python到全息。我一直在尝试,但C#脚本中似乎没有接收到图像数据。

非常感谢。

Hololens服务器代码:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

#if !UNITY_EDITOR
    using Windows.Networking;
    using Windows.Networking.Sockets;
    using Windows.Storage.Streams;
#endif

//Able to act as a reciever 
public class UniversalSampleTutorial : MonoBehaviour
{
    public String _input = "Waiting";

#if !UNITY_EDITOR
        StreamSocket socket;
        StreamSocketListener listener;
        String port;
        String message;
#endif

    // Use this for initialization
    void Start()
    {
#if !UNITY_EDITOR
        listener = new StreamSocketListener();
        port = "9090";
        listener.ConnectionReceived += Listener_ConnectionReceived;
        listener.Control.KeepAlive = false;

        Listener_Start();
#endif
    }

#if !UNITY_EDITOR
    private async void Listener_Start()
    {
        Debug.Log("Listener started");
        try
        {
            await listener.BindServiceNameAsync(port);
        }
        catch (Exception e)
        {
            Debug.Log("Error: " + e.Message);
        }

        Debug.Log("Listening");
    }

    private async void Listener_ConnectionReceived(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
    {
        Debug.Log("Connection received");

        try
        {
            while (true) {

                    using (var dw = new DataWriter(args.Socket.OutputStream))
                    {
                        dw.WriteString("Hello There");
                        await dw.StoreAsync();
                        dw.DetachStream();
                    }  

                    using (var dr = new DataReader(args.Socket.InputStream))
                    {
                        dr.InputStreamOptions = InputStreamOptions.Partial;
                        await dr.LoadAsync(12);
                        var input = dr.ReadString(12);
                        Debug.Log("received: " + input);
                        _input = input;

                    }
            }
        }
        catch (Exception e)
        {
            Debug.Log("disconnected!!!!!!!! " + e);
        }

    }

#endif

    void Update()
    {
        this.GetComponent<TextMesh>().text = _input;
    }
}

Python客户端(需要从这里发送图像,而不是字符串)

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
import asyncio

#THIS CODE WORKS SENDING STRING MESSAGE TO HOLOLENS

async def tcp_echo_client(message, loop):
    reader, writer = await asyncio.open_connection('192.168.1.110', 9090, loop=loop)

    print('Send: %r' % message)
    writer.write(message.encode())

    data = await reader.read(100)
    print('Received: %r' % data.decode())

    print('Close the socket')
    writer.close()


message = 'hello there frend'
loop = asyncio.get_event_loop()
loop.run_until_complete(tcp_echo_client(message, loop))
loop.close()

编辑:这是我一直在编写的图像代码。它还不能工作,我不确定我是否正确地接收了字节数组:https://gist.github.com/jryebread/3961e890375fcc8a64c8ac3d279ec9fa

EN

回答 1

Stack Overflow用户

发布于 2018-08-09 19:49:48

我已经知道如何发送图像了。我最终从python客户端以字符串的形式发送了日期,在hololens服务器脚本上对该字符串进行了base64编码和解码。

https://gist.github.com/jryebread/2bdf148313f40781f1f36d38ada85d47

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51411832

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文