【问题标题】:Infrared command with pocket pc带掌上电脑的红外线指令
【发布时间】:2013-02-21 16:04:56
【问题描述】:

我有一个红外线遥控器,我想用我的 PPC 替换它。 我的 Mitac P550 有一个红外串口,但我不知道如何检索和重新发送字节序列..... 这个pourpose可以用.net的SerialPort组件获取数据吗?

谢谢

【问题讨论】:

    标签: c# windows-mobile-6 pocketpc infrared


    【解决方案1】:

    您需要编写两 (2) 个方法并将它们安装在通信的每一端:SendReceive

    一个通用的Send例程会发送一个message到一些host监听给定的port号。这是一个简单的例子:

    public static void Send(string message, string host, int port) {
      if (!String.IsNullOrEmpty(message)) {
        if (port < 80) {
          port = DEF_PORT;
        }
        Byte[] data = Encoding.ASCII.GetBytes(message);
        using (var client = new TcpClient(host, port)) {
          var stream = client.GetStream();
          stream.Write(data, 0, data.Length);
          stream.Close();
          client.Close();
        }
      }
    }
    

    一个通用的Receive例程需要知道要监听的端口号,并且应该返回它接收到的data。这是一个简单的例子:

    public static string Receive(int port) {
      string data = null;
      listener = new TcpListener(IPAddress.Any, port);
      listener.Start();
      using (var client = listener.AcceptTcpClient()) { // waits until data is avaiable
        int MAX = client.ReceiveBufferSize;
        var stream = client.GetStream();
        Byte[] buffer = new Byte[MAX];
        int len = stream.Read(buffer, 0, MAX);
        if (0 < len) {
          data = Encoding.UTF8.GetString(buffer, 0, len);
        }
        stream.Close();
        client.Close();
      }
      return data;
    }
    

    这是我用于此的完整类代码:

    using System;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using System.Windows.Forms;
    
    namespace AcpMobile5 {
    
      class TestClass1 : Form {
    
        public const int DEF_PORT = 8000;
        private static TcpListener listener;
    
        public static string Receive(int port) {
          string data = null;
          listener = new TcpListener(IPAddress.Any, port);
          listener.Start();
          using (var client = listener.AcceptTcpClient()) { // waits until data is avaiable
            int MAX = client.ReceiveBufferSize;
            var stream = client.GetStream();
            Byte[] buffer = new Byte[MAX];
            int len = stream.Read(buffer, 0, MAX);
            if (0 < len) {
              data = Encoding.UTF8.GetString(buffer, 0, len);
            }
            stream.Close();
            client.Close();
          }
          return data;
        }
    
        public static void Send(string message, string host, int port) {
          if (!String.IsNullOrEmpty(message)) {
            if (port < 80) {
              port = DEF_PORT;
            }
            Byte[] data = Encoding.ASCII.GetBytes(message);
            using (var client = new TcpClient(host, port)) {
              var stream = client.GetStream();
              stream.Write(data, 0, data.Length);
              stream.Close();
              client.Close();
            }
          }
        }
    
      }
    
    }
    

    【讨论】:

      猜你喜欢
      • 2013-02-11
      • 2011-01-02
      • 1970-01-01
      • 1970-01-01
      • 2017-05-23
      • 1970-01-01
      • 1970-01-01
      • 2018-04-08
      • 2014-04-13
      相关资源
      最近更新 更多