com口通信问题

smaworm 2007-11-14 02:28:31
计算机上com1接口接一个外部设备,我的C#编写的软件程序需要读取com1接口传来的数据,应该怎么做?
求教高手,最好有注释。
邮件我也行 smaworm@sohu.com
...全文
148 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
gaobosuzhou 2012-08-17
  • 打赏
  • 举报
回复
长见识了
leeyunjiao 2007-12-25
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;


namespace RS232
{
public partial class fclsRS232Tester : Form
{
string InputData = String.Empty;

// This delegate enables asynchronous calls for setting
// the text property on a TextBox control:
delegate void SetTextCallback(string text);

public fclsRS232Tester()
{
InitializeComponent();

// Nice methods to browse all available ports:
string[] ports = SerialPort.GetPortNames();

// Add all port names to the combo box:
foreach (string port in ports)
{
cmbComSelect.Items.Add(port);
}
}

private void cmbComSelect_SelectionChangeCommitted(object sender, EventArgs e)
{
if (port.IsOpen) port.Close();
port.PortName = cmbComSelect.SelectedItem.ToString();
stsStatus.Text = port.PortName + ": 9600,8N1";

// try to open the selected port:
try
{
port.Open();
}
// give a message, if the port is not available:
catch
{
MessageBox.Show("Serial port " + port.PortName + " cannot be opened!", "RS232 tester", MessageBoxButtons.OK, MessageBoxIcon.Warning);
cmbComSelect.SelectedText = "";
stsStatus.Text = "Select serial port!";
}
}

private void btnSend_Click(object sender, EventArgs e)
{
if (port.IsOpen) port.WriteLine(txtOut.Text);
else MessageBox.Show("Serial port is closed!", "RS232 tester", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtOut.Clear();
}

private void btnClear_Click(object sender, EventArgs e)
{
txtIn.Clear();
}

private void port_DataReceived_1(object sender, SerialDataReceivedEventArgs e)
{
InputData = port.ReadExisting();
if (InputData != String.Empty)
{
// txtIn.Text = InputData; // because of different threads this does not work properly !!
SetText(InputData);
}
}

/*
To make a thread-safe call a Windows Forms control:

1. Query the control's InvokeRequired property.
2. If InvokeRequired returns true, call Invoke with a delegate that makes the actual call to the control.
3. If InvokeRequired returns false, call the control directly.

In the following code example, this logic is implemented in a utility method called SetText.
A delegate type named SetTextDelegate encapsulates the SetText method.
When the TextBox control's InvokeRequired returns true, the SetText method creates an instance
of SetTextDelegate and calls the form's Invoke method.
This causes the SetText method to be called on the thread that created the TextBox control,
and in this thread context the Text property is set directly

also see: http://msdn2.microsoft.com/en-us/library/ms171728(VS.80).aspx
*/

// This method demonstrates a pattern for making thread-safe
// calls on a Windows Forms control.
//
// If the calling thread is different from the thread that
// created the TextBox control, this method creates a
// SetTextCallback and calls itself asynchronously using the
// Invoke method.
//
// If the calling thread is the same as the thread that created
// the TextBox control, the Text property is set directly.

private void SetText(string text)
{
// InvokeRequired required compares the thread ID of the
// calling thread to the thread ID of the creating thread.
// If these threads are different, it returns true.
if (this.txtIn.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else this.txtIn.Text += text;
}

}
}
wenbin 2007-11-14
  • 打赏
  • 举报
回复
要看你用的IDE是VS2003还是更高级的如VS2005,如果是VS2003的话,那需要去下载JUSTIO类,若为VS2005的话,就不需要了,
多去看看VC方面关于这些的东西,了解一下对串口的操作,做起来要容易些吧
ETstudio 2007-11-14
  • 打赏
  • 举报
回复
我也不是搞这个的
你看看这个文章http://blog.csdn.net/kangsoft/articles/304948.aspx
smaworm 2007-11-14
  • 打赏
  • 举报
回复
小弟刚刚接触这方面的东西,哪个大侠能给段比较完整的示例代码?
ETstudio 2007-11-14
  • 打赏
  • 举报
回复
SerialPort 类

此类用于控制串行端口文件资源。此类提供同步 I/O 和事件驱动的 I/O、对管脚和中断状态的访问以及对串行驱动程序属性的访问。另外,此类的功能可以包装在内部 Stream 对象中,可通过 BaseStream 属性访问,并且可以传递给包装或使用流的类。

SerialPort 类支持以下编码:ASCIIEncoding、UTF8Encoding、UnicodeEncoding、UTF32Encoding 以及 mscorlib.dll 中定义的、代码页小于 50000 或者为 54936 的所有编码。您可以使用其他编码,但必须使用 ReadByte 或 Write 方法并自己执行编码。
ETstudio 2007-11-14
  • 打赏
  • 举报
回复
System.IO.Ports 命名空间包含用于控制串行端口的类。最重要的类 SerialPort 为同步和事件驱动 I/O 提供框架,提供对插针和中断状态的访问以及对串行驱动程序属性的访问。它可用于包装 Stream 对象,允许使用流的类访问串行端口。
zfwdf 2007-11-14
  • 打赏
  • 举报
回复
ding

110,533

社区成员

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

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

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