求助C#中关于串口的操作

s32702 2009-08-24 09:10:04
代码如下:

Program.cs

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO.Ports;
using System.Configuration;
using WYC.Common.OU;
using System.Data;
using System.Diagnostics;
using System.Threading;
using System.Web;

namespace WYC.WinApp
{

internal static class Program
{
/// <summary>
/// 读卡
/// </summary>
/// <returns></returns>
internal delegate void ReadCardHandler(CardInfo obj);

internal static event ReadCardHandler ReadCard = null;

/// <summary>
/// 串口通信对象
/// </summary>
private static SerialPort c_Com = null;

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);

AppInit();

Application.Run(new frmMain());

if (c_Com.IsOpen)
c_Com.Close();

}


//异常
static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
{
MessageBox.Show(e.Exception.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
//throw new Exception("The method or operation is not implemented.");
}

/// <summary>
/// 应用初始化
/// </summary>
static void AppInit()
{
try
{
//设置读卡器串口
c_Com = new SerialPort();
c_Com.PortName = "com1";
c_Com.BaudRate = int.Parse(ConfigurationManager.AppSettings.Get("baudRate"));

c_Com.DataReceived+=new SerialDataReceivedEventHandler(c_Com_DataReceived);

}
catch (UnauthorizedAccessException uae)
{
MessageBox.Show(uae.Message.ToString(), "警告1", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (ArgumentException ae)
{
MessageBox.Show(ae.Message.ToString(), "警告2", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (System.IO.IOException ioe)
{
MessageBox.Show(ioe.Message.ToString(), "警告3", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (InvalidOperationException ioec)
{
MessageBox.Show(ioec.Message.ToString(), "警告4", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

}

static void c_Com_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
SerialPort com = sender as SerialPort;

if (ReadCard != null)
{
CardInfo obj = new CardInfo();
ReadCard(obj);
}

com.DiscardInBuffer();
}
}

}





frmMain.cs


using System;
using System.Data;
using System.Windows.Forms;
using System.IO.Ports;
using System.Web;
using System.Configuration;
using WYC.DataAccess.OU;
using WYC.Common.OU;
using System.Diagnostics;
using System.Threading;
namespace WYC.WinApp
{
public partial class frmMain : Form
{
public frmMain()
{

InitializeComponent();

Program.ReadCard += new Program.ReadCardHandler(OnReadCard);
}

public string UrlNum = String.Empty;
public frmLogin fl = new frmLogin();

void OnReadCard(CardInfo obj)
{
ReadCardHandler readcard = new ReadCardHandler(SetInfo);
if (InvokeRequired)
{
try
{
this.Invoke(readcard, new object[] { obj });
}
catch
{ }
}
else
{
SetInfo(obj);
}
}

delegate void ReadCardHandler(CardInfo obj);

void SetInfo(CardInfo obj)
{
}


void c_Com_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
SerialPort com = (sender as SerialPort);

com.ReadTimeout = 1000;
com.ReadLine();
}
}





...全文
124 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
yulien 2009-08-26
  • 打赏
  • 举报
回复
ding
s32702 2009-08-26
  • 打赏
  • 举报
回复
还是无法在DataReceived事件中捕获数据
s32702 2009-08-24
  • 打赏
  • 举报
回复
我写了个Winform程序测试DLL的调用,没有问题
风之影子 2009-08-24
  • 打赏
  • 举报
回复
你确定串口肯定有数据上来吗?

还有你的波特率设的没错吧!


先拿网上的串口调试程序试试看是否有数据上来。
s32702 2009-08-24
  • 打赏
  • 举报
回复
请问为什么定义的委托和事件怎么没调用,是串口的问题吗?
这个项目有条码枪和读卡器两个串口

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧