昨天晚上,突然接到让我做一个红外通信的小东西,没法子,只好留下来加个班了!当然,首先考虑到的是c#.net,毕竟时间短,想找直接利用它的类不就ok了!可惜啊,捣鼓了半天,发现IrDAClient类只在1。1中支持,而我用的已经是2。0,我只能望而生叹了!当然,既然提到了,就给大家简单说下,以下是来自msdn的代码:
// Align the infrared ports of the devices.
// Click the Receive button first, then click Send.
private void SendButton_Click(object sender, System.EventArgs e)
{
IrDAClient irClient = new IrDAClient();
string irServiceName = "IrDATest";
IrDADeviceInfo[] irDevices;
int buffersize = 256;
// Create a collection of devices to discover.
irDevices = irClient.DiscoverDevices(2);
// Show the name of the first device found.
if ((irDevices.Length == 0))
{
MessageBox.Show("No remote infrared devices found.");
return;
}
try
{
IrDAEndPoint irEndP =
new IrDAEndPoint(irDevices[0].DeviceID, irServiceName);
IrDAListener irListen = new IrDAListener(irEndP);
irListen.Start();
irClient = irListen.AcceptIrDAClient();
MessageBox.Show("Connected!");
}
catch (SocketException exSoc)
{
MessageBox.Show(("Couldn\'t listen on service "
+ (irServiceName + (": " + exSoc.ErrorCode))));
}
// Open a file to send and get its stream.
Stream fs;
try
{
fs = new FileStream(".\\My Documents\\send.txt", FileMode.Open);
}
catch (Exception exFile)
{
MessageBox.Show(("Cannot open " + exFile.ToString()));
return;
}
// Get the underlying stream of the client.
Stream baseStream = irClient.GetStream();
// Get the size of the file to send
// and write its size to the stream.
byte[] length = BitConverter.GetBytes(fs.Length);
baseStream.Write(length, 0, length.Length);
// Create a buffer for reading the file.
byte[] buffer = new byte[buffersize];
int fileLength = (int) fs.Length;
try
{
// Read the file stream into the base stream.
while ((fileLength > 0))
{
Int64 numRead = fs.Read(buffer, 0, buffer.Length);
baseStream.Write(buffer, 0, Convert.ToInt32(numRead));
fileLength = (fileLength - Convert.ToInt32(numRead));
}
MessageBox.Show("File sent");
}
catch (Exception exSend)
{
MessageBox.Show(exSend.Message);
}
fs.Close();
baseStream.Close();
irClient.Close();
}
而面对这样的问题,我只能一步步慢慢了解,用c++写下啦,关于用c++的代码,我不打算都提,因为我现在用了自己封好的类,弄起来也不少。我就介绍下思想。并且贴出简单的sample。
要实现红外通信的话,内部的原理是通过socket进行通信,当然,如果你要用串口红外设备的话,那还得考虑串口的东西。而用到的是红外自己的协议AF_IRDA。现在就是如何获得红外设备了。DEVICELIST这个结果体是用来存放获取的红外设备的信息的!同时我们用getsockopt来获取这些设备的DEVICELIST。
Sample:
typedef struct _PACKET //接收发送数据包结构
{
DWORD dLength; //接收发送数据长度
CHAR cString[500];//接收数据
} PACKET;
SOCKET c_Socket; //客户端Socket
//IrDA地址
SOCKADDR_IRDA address = {AF_IRDA, 0, 0, 0, 0, "IrSend"};
DEVICELIST dLst;
int dLstLen = sizeof(dLst),num = 0,i=0;
c_Socket = socket(AF_IRDA, SOCK_STREAM, 0);
dLst.numDevice = 0;
PACKET Packet;
//试6次,以便找到服务器端SOCKET
while ((dLst.numDevice == 0) && (num <= 6))
{
getsockopt(c_Socket, SOL_IRLMP, IRLMP_ENUMDEVICES,
(char *)&dLst, &dLstLen);
if (dLst.numDevice!=0) break;
num++;
Sleep(600);
}
if (num > 6)
return ;
else //找到服务器端SOCKET
{
for (i = 0; i <= 3; i++)
address.irdaDeviceID[i]=dLst.Device[0].irdaDeviceID[i];
//连结服务器端SOCKET
connect(c_Socket , (struct sockaddr *)&address,
sizeof(SOCKADDR_IRDA));
memset(Packet.cString,0,sizeof(Packet.cString));
Packet.dLength = len;
memcpy( Packet.cString, Buffer, len);
//发送数据
send(c_Socket,(CHAR FAR *)&Packet,len+sizeof(DWORD), 0 );
closesocket(c_Socket);
}
return ;
以上便是我的理解,如果有什么不对,请指教。
// Align the infrared ports of the devices.
// Click the Receive button first, then click Send.
private void SendButton_Click(object sender, System.EventArgs e)
{
IrDAClient irClient = new IrDAClient();
string irServiceName = "IrDATest";
IrDADeviceInfo[] irDevices;
int buffersize = 256;
// Create a collection of devices to discover.
irDevices = irClient.DiscoverDevices(2);
// Show the name of the first device found.
if ((irDevices.Length == 0))
{
MessageBox.Show("No remote infrared devices found.");
return;
}
try
{
IrDAEndPoint irEndP =
new IrDAEndPoint(irDevices[0].DeviceID, irServiceName);
IrDAListener irListen = new IrDAListener(irEndP);
irListen.Start();
irClient = irListen.AcceptIrDAClient();
MessageBox.Show("Connected!");
}
catch (SocketException exSoc)
{
MessageBox.Show(("Couldn\'t listen on service "
+ (irServiceName + (": " + exSoc.ErrorCode))));
}
// Open a file to send and get its stream.
Stream fs;
try
{
fs = new FileStream(".\\My Documents\\send.txt", FileMode.Open);
}
catch (Exception exFile)
{
MessageBox.Show(("Cannot open " + exFile.ToString()));
return;
}
// Get the underlying stream of the client.
Stream baseStream = irClient.GetStream();
// Get the size of the file to send
// and write its size to the stream.
byte[] length = BitConverter.GetBytes(fs.Length);
baseStream.Write(length, 0, length.Length);
// Create a buffer for reading the file.
byte[] buffer = new byte[buffersize];
int fileLength = (int) fs.Length;
try
{
// Read the file stream into the base stream.
while ((fileLength > 0))
{
Int64 numRead = fs.Read(buffer, 0, buffer.Length);
baseStream.Write(buffer, 0, Convert.ToInt32(numRead));
fileLength = (fileLength - Convert.ToInt32(numRead));
}
MessageBox.Show("File sent");
}
catch (Exception exSend)
{
MessageBox.Show(exSend.Message);
}
fs.Close();
baseStream.Close();
irClient.Close();
}
而面对这样的问题,我只能一步步慢慢了解,用c++写下啦,关于用c++的代码,我不打算都提,因为我现在用了自己封好的类,弄起来也不少。我就介绍下思想。并且贴出简单的sample。
要实现红外通信的话,内部的原理是通过socket进行通信,当然,如果你要用串口红外设备的话,那还得考虑串口的东西。而用到的是红外自己的协议AF_IRDA。现在就是如何获得红外设备了。DEVICELIST这个结果体是用来存放获取的红外设备的信息的!同时我们用getsockopt来获取这些设备的DEVICELIST。
Sample:
typedef struct _PACKET //接收发送数据包结构
{
DWORD dLength; //接收发送数据长度
CHAR cString[500];//接收数据
} PACKET;
SOCKET c_Socket; //客户端Socket
//IrDA地址
SOCKADDR_IRDA address = {AF_IRDA, 0, 0, 0, 0, "IrSend"};
DEVICELIST dLst;
int dLstLen = sizeof(dLst),num = 0,i=0;
c_Socket = socket(AF_IRDA, SOCK_STREAM, 0);
dLst.numDevice = 0;
PACKET Packet;
//试6次,以便找到服务器端SOCKET
while ((dLst.numDevice == 0) && (num <= 6))
{
getsockopt(c_Socket, SOL_IRLMP, IRLMP_ENUMDEVICES,
(char *)&dLst, &dLstLen);
if (dLst.numDevice!=0) break;
num++;
Sleep(600);
}
if (num > 6)
return ;
else //找到服务器端SOCKET
{
for (i = 0; i <= 3; i++)
address.irdaDeviceID[i]=dLst.Device[0].irdaDeviceID[i];
//连结服务器端SOCKET
connect(c_Socket , (struct sockaddr *)&address,
sizeof(SOCKADDR_IRDA));
memset(Packet.cString,0,sizeof(Packet.cString));
Packet.dLength = len;
memcpy( Packet.cString, Buffer, len);
//发送数据
send(c_Socket,(CHAR FAR *)&Packet,len+sizeof(DWORD), 0 );
closesocket(c_Socket);
}
return ;
以上便是我的理解,如果有什么不对,请指教。
浙公网安备 33010602011771号