0
我有一個紅外遙控器,我想用我的PPC替換它。 我的神達P550有一個紅外串行端口,但我不知道如何檢索和重新發送字節序列..... 這可能獲得與.net的SerialPort組件的數據這傾斜?掌上電腦的紅外命令
感謝
我有一個紅外遙控器,我想用我的PPC替換它。 我的神達P550有一個紅外串行端口,但我不知道如何檢索和重新發送字節序列..... 這可能獲得與.net的SerialPort組件的數據這傾斜?掌上電腦的紅外命令
感謝
你需要寫兩(2)方法和對您的通信的每一端安裝:一個發送和接收。
的通用發送程序將發出一個消息一些主機監聽特定端口號。下面是一個簡單的例子:
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();
}
}
}
的通用接收日常需要知道端口號上偵聽,並應其接收返回數據。下面是它的一個簡單的例子:
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();
}
}
}
}
}