| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1660 人关注过本帖
标题:C#与电脑的红外通信
只看楼主 加入收藏
wenkiller
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2006-5-9
收藏
得分:0 
接上面
/// <summary>
/// The main entry point for the application.
/// </summary>

static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
isRunning = true;
this.thServ = new Thread (new ThreadStart(this.SrvRoutine));
this.thServ.Start();
}

private void Form1_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
isRunning = false;
Thread.Sleep(550);
}

private void menuExit_Click(object sender, System.EventArgs e)
{
this.Close();
}

private void btnBrowse_Click(object sender, System.EventArgs e)
{
DialogResult dr = this.openFileDialog1.ShowDialog();
if (dr != DialogResult.OK)
return;
this.textFileName.Text = openFileDialog1.FileName;
}

private void inputPanel1_EnabledChanged(object sender,
System.EventArgs e)
{
// Adjust the listbox to avoid being covered by the SIP.
if (m_fPocketPC)
{
int height = this.inputPanel1.VisibleDesktop.Height -
this.listOut.Top;
if (!this.inputPanel1.Enabled)
height -= 26;
this.listOut.Size = new Size (this.listOut.Width,
height);
}
}

private void btnGo_Click(object sender, System.EventArgs e)
{
string strFileName = this.textFileName.Text;

if (strFileName.Length > 0)
SendFileToIR (strFileName);
}
private void StringOut (string str)
{
this.listOut.Items.Add (str);
return;
}
/// <summary>
/// Sends a file to the other device
/// </summary>
/// <param name="strFileName"></param>
private void SendFileToIR (string strFileName)
{
Stream s;
FileStream fs;
int rc;
IrDAClient irClient;

try
{
fs = new FileStream (strFileName, FileMode.Open,
FileAccess.Read);
}
catch (IOException ex)
{
StringOut (string.Format("Error opening file {0}",
ex.Message));
return;
}
StringOut ("File opened");

irClient = new IrDAClient ();
//
// Connect to service
//
StringOut ("Waiting at connect");
try
{
irClient.Connect("MySquirt");

}
catch (SocketException ex)
{
StringOut (string.Format ("Sock connect exception {0}",
ex.ErrorCode));
fs.Close();
return;
}
StringOut ("Connected");

//
// Start transfer
//
try
{
s = irClient.GetStream();
}
catch (SocketException ex)
{
StringOut (string.Format ("Sock GetStream exception {0}",
ex.ErrorCode));
fs.Close();
irClient.Close();
return;
}
// Parse path to get only the file name and extension
char[] parse = "\\".ToCharArray();
string[] fnParts = strFileName.Split (parse);
string strNameOnly = fnParts[fnParts.Length-1];
int nLen = strNameOnly.Length;

// Allocate transfer buffer
byte[] buff = new byte[4096];

// Send name length
StringOut ("Sending file name");
if (!SendDWord (s, nLen+1))
{
StringOut (string.Format ("Error sending name length"));
return;
}
// Send name
UTF8Encoding UTF8enc = new UTF8Encoding();
UTF8enc.GetBytes (strNameOnly, 0, nLen, buff, 0);
buff[nLen] = 0;
try
{
s.Write (buff, 0, nLen+1);
}
catch (SocketException ex)
{
StringOut (string.Format ("Sock Write exception {0}",
ex.ErrorCode));
}
StringOut ("Sending file");
// Send file length
nLen = (int)fs.Length;
if (!SendDWord (s, nLen))
{
StringOut ("Error sending file list");
}

// Read back file open return code
StringOut ("Reading file create ack");
RecvDWord (s, out rc);
if (rc != 0)
{
StringOut (string.Format ("Bad Ack code {0}", rc));
fs.Close();
irClient.Close();
s.Close();
return;
}
StringOut ("ack received");

// Send file data
while (nLen > 0)
{
int j = -1;
try
{
j = (nLen > buff.Length) ? buff.Length : nLen;
StringOut (string.Format("Sending {0} bytes", j));
fs.Read (buff, 0, j);
s.Write (buff, 0, j);
nLen -= j;

if (!RecvDWord (s, out j))
break;
if (j != 0)
{
StringOut ("Error ack");
break;
}
}
catch (SocketException socex)
{
StringOut (string.Format ("5 Sock Err {0} ({1},{2}",
socex.ErrorCode, nLen, j));
break;
}
catch (IOException ioex)
{
StringOut (string.Format ("File Error {0}",
ioex.Message));
break;
}
// Allow other events to happen during loop
Application.DoEvents();
}
StringOut (string.Format("File sent"));

s.Close(); // Close the stream
irClient.Close(); // Close the socket
fs.Close(); // Close the file
return;
}
/// <summary>
/// Sends a DWORD to the other device
/// </summary>
/// <param name="s"></param>
/// <param name="i"></param>
/// <returns></returns>
bool SendDWord (Stream s, int i)
{
byte[] b = BitConverter.GetBytes (i);
try
{
s.Write (b, 0, 4);
}
catch (SocketException ex)
{
StringOut (string.Format ("Err {0} writing dword",
ex.ErrorCode));
return false;
}
return true;
}
/// <summary>
/// Receiveds a DWORD from the other device
/// </summary>
/// <param name="s"></param>
/// <param name="i"></param>
/// <returns></returns>
bool RecvDWord (Stream s, out int i)
{
byte[] b = new byte[4];
try
{
s.Read (b, 0, 4);
}
catch (SocketException ex)
{
StringOut (string.Format ("Err {0} reading dword",
ex.ErrorCode));
i = 0;
return false;
}
i = BitConverter.ToInt32 (b, 0);
return true;
}
/// <summary>
/// Server thread
/// </summary>
public void SrvRoutine()
{
IrDAListener irListen;
FileStream fs;
string strFileName;
int nLen;
IrDAClient irClientSrv;
Stream s;
byte[] buff = new byte[4096];

try
{
irListen = new IrDAListener("MySquirt");
irListen.Start();
}
catch (SocketException ex)
{
StringOut (string.Format("Err {0} creating IrDAListener",
ex.ErrorCode));
return;
}
StringOut ("IrdaListener created");

while (isRunning)
{
if (irListen.Pending())
{
try
{
StringOut ("Calling AcceptIrDAClient");
irClientSrv = irListen.AcceptIrDAClient();
StringOut ("AcceptIrDAClient returned");
s = irClientSrv.GetStream();
}
catch (SocketException ex)
{
StringOut (string.Format ("Sock exception {0}",
ex.ErrorCode));
continue;
}

// Get name length
StringOut ("Getting file name");
if (!RecvDWord (s, out nLen))
{
StringOut ("Error getting name length");
s.Close();
continue;
}
// Read name
try
{
s.Read (buff, 0, nLen);
}
catch (SocketException ex)
{
StringOut (string.Format ("Read exception {0}",
ex.ErrorCode));
s.Close();
continue;
}
UTF8Encoding UTF8enc = new UTF8Encoding();
//Trim terminating zero
char[] ch = UTF8enc.GetChars (buff, 0, nLen-1);
strFileName = new string (ch);
StringOut ("Receiving file " + strFileName);

// Get file length
if (!RecvDWord (s, out nLen))
{
StringOut ("Error getting file length");
}
StringOut (string.Format ("File len: {0}", nLen));
try
{
fs = new FileStream (strFileName,
FileMode.Create,
FileAccess.Read|FileAccess.Write);
}
catch (IOException ioex)
{
StringOut (string.Format("Error opening file"));
StringOut (ioex.Message);
SendDWord (s, -3);
s.Close();
continue;
}
StringOut ("File opened");

// Send file open return code
StringOut ("Send file create ack");
if (!SendDWord (s, 0))
{
StringOut ("fail sending ack code");
fs.Close();
s.Close();
break;
}
int nTotal = 0;
// Send file data
while (nLen > 0)
{
int BlkSize = -1;
try
{
BlkSize = (nLen > buff.Length) ?
buff.Length : nLen;
int k = 0, BytesRead = 0;
while (BlkSize > k)
{
// Wait for data
if (!((NetworkStream)s).DataAvailable)
Thread.Sleep(100);
// Read it
BytesRead = s.Read (buff, k, BlkSize-k);
StringOut (string.Format ("Bytes: {0}",
BytesRead));
k += BytesRead;
}
fs.Write (buff, 0, BlkSize);
StringOut ("Send Ack");
if (!SendDWord (s, 0))
{
StringOut ("Error sending ack");
break;
}
nLen -= BlkSize;
nTotal += BlkSize;
}
catch (SocketException socex)
{
StringOut (string.Format ("Sock Err {0}",
socex.ErrorCode));
break;
}
catch (IOException ioex)
{
StringOut (string.Format ("File Err {0}",
ioex.Message));
StringOut (ioex.Message);
break;
}
}
StringOut (string.Format("File received {0} bytes.",
nTotal));
RecvDWord (s, out nLen);
fs.Close();
s.Close();
}
if (isRunning)
Thread.Sleep (500);
}
irListen.Stop();
return;
}
}
}

我现在正在读研究生,有热爱学习的朋友愿意交流请加我。360187438
2006-09-27 17:39
wenkiller
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2006-5-9
收藏
得分:0 
完毕。上面是关于windows ce的一段红外通信的代码。如果有学习的C#的朋友愿意一起交流,请加我的QQ360187438

我现在正在读研究生,有热爱学习的朋友愿意交流请加我。360187438
2006-09-27 17:40
wenkiller
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2006-5-9
收藏
得分:0 
自己给自己顶啊

我现在正在读研究生,有热爱学习的朋友愿意交流请加我。360187438
2006-09-27 23:30
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 
帮你顶!
2006-09-28 08:56
wohemachen
Rank: 1
等 级:新手上路
威 望:2
帖 子:641
专家分:0
注 册:2006-9-21
收藏
得分:0 
关注一下

[glow=255,red,2]桃花坞里桃花庵,桃花庵里桃花仙;桃花仙人种桃树,又摘桃花换酒钱。[/glow]
2006-09-28 09:13
leisky
Rank: 1
等 级:新手上路
帖 子:253
专家分:0
注 册:2006-5-22
收藏
得分:0 

帮顶了..请问你找到的只是代码了还是有源程序了..你这些源程序在我这里面运行出错....呵呵 要是有最好把整个都发上.


2006-09-28 09:23
wenkiller
Rank: 1
等 级:新手上路
帖 子:15
专家分:0
注 册:2006-5-9
收藏
得分:0 

楼上的兄弟,给的程序肯定不可能在每个机子上都一下子就运行阿。调试一下阿。


我现在正在读研究生,有热爱学习的朋友愿意交流请加我。360187438
2006-09-28 12:34
快速回复:C#与电脑的红外通信
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019631 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved