C# 用掌上电脑实现红外命令
我有一个红外遥控器,我想把它换成我的PPC。 我的Mitac P550有一个红外串行端口,但我不知道如何检索和重新发送字节序列。。。。。 是否可以使用.net的SerialPort组件获取此端口的数据 谢谢您需要编写两(2)种方法,并将它们安装在通信的每一端:发送和接收 一般的发送例程将向某个主机发送一条消息,该主机在给定的端口上侦听。下面是一个简单的例子:C# 用掌上电脑实现红外命令,c#,windows-mobile-6,pocketpc,infrared,C#,Windows Mobile 6,Pocketpc,Infrared,我有一个红外遥控器,我想把它换成我的PPC。 我的Mitac P550有一个红外串行端口,但我不知道如何检索和重新发送字节序列。。。。。 是否可以使用.net的SerialPort组件获取此端口的数据 谢谢您需要编写两(2)种方法,并将它们安装在通信的每一端:发送和接收 一般的发送例程将向某个主机发送一条消息,该主机在给定的端口上侦听。下面是一个简单的例子: public static void Send(string message, string host, int port) { if
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();
}
}
}
publicstaticvoidsend(字符串消息、字符串主机、int端口){
如果(!String.IsNullOrEmpty(消息)){
如果(端口<80){
端口=DEF_端口;
}
字节[]数据=Encoding.ASCII.GetBytes(消息);
使用(var客户端=新的TcpClient(主机、端口)){
var stream=client.GetStream();
stream.Write(数据,0,数据长度);
stream.Close();
client.Close();
}
}
}
一般的接收例程需要知道要监听的端口号,并且应该返回它接收的数据。下面是一个简单的例子:
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;
}
公共静态字符串接收(int端口){
字符串数据=null;
侦听器=新的TcpListener(IPAddress.Any,端口);
listener.Start();
使用(var client=listener.AcceptTcpClient()){//等待数据可用
int MAX=client.ReceiveBufferSize;
var stream=client.GetStream();
字节[]缓冲区=新字节[MAX];
int len=stream.Read(缓冲区,0,最大值);
如果(0
下面是我用于此操作的完整类代码:
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();
}
}
}
}
}
使用系统;
Net系统;
使用System.Net.Sockets;
使用系统文本;
使用System.Windows.Forms;
名称空间AcpMobile5{
类TestClass1:表单{
公共const int DEF_PORT=8000;
专用静态TcpListener侦听器;
公共静态字符串接收(int端口){
字符串数据=null;
侦听器=新的TcpListener(IPAddress.Any,端口);
listener.Start();
使用(var client=listener.AcceptTcpClient()){//等待数据可用
int MAX=client.ReceiveBufferSize;
var stream=client.GetStream();
字节[]缓冲区=新字节[MAX];
int len=stream.Read(缓冲区,0,最大值);
如果(0