C#和232串口通信方法(SerialPort控件) 下载本文

本例程主要讲解使用C#,实现与232串口通信。达到采集串口数据,监控,可视化处理等。

一.概述

在Visual Studio 6.0中编写串口通讯程序,一般都使用

Microsoft Communication Control(简称MSComm)的通讯控件,只要通 过对此控件的属性和事件进行相应编程操作,就可以轻松地实现串口通讯。但在Microsoft.Net技术广泛应用的今天,Visual Studio.Net没有将此控件加入控件库,所以人们采用了许多方法在Visual Studio.Net来编写串口通讯程序:第一种方法是通过采用Visual Studio 6.0中原来的MSComm控件这是最简单的,最方便的方法,但需要注册;第二种方法是采用微软在.NET推出了一个串口控件,基于.NET的P/Invoke调用方法实现;第三种方法是自己用API写串口通信,虽然难度高,但可以方便实现自己想要的各种功能。

现在微软推出了最新版本的Visual Studio 2005开发工具,可以不再采用第三方控件的方法来设计串口通讯程序。NET Framework 2.0 类库包含了SerialPort 类,方便地实现了所需要串口通讯的多种功能,为了使MSComm编程方法快速转换到以SerialPort类为核心的串口通讯的设计方法,本文着重讨论了Visual Studio 6.0的MSComm控件和SerialPort 类设计方法的异同点。

二.SerialPort常用属性、方法和事件

1.命名空间

System.IO.Ports命名空间包含了控制串口重要的SerialPort类,该类提供了同步 I/O 和事件驱动的 I/O、对管脚和中断状态的访问以及对串行驱动程序属性的访问,所以在程序代码起始位置需加入Using System.IO.Ports。

2.串口的通讯参数

串口通讯最常用的参数就是通讯端口号及通讯格式(波特率、数据位、停止位和校验位),在MSComm中相关的属性是CommPort和Settings。SerialPort类与MSComm有一些区别:

a.通讯端口号

[PortName]属性获取或设置通信端口,包括但不限于所有可用的 COM 端口,请注意该属性返回类型为String,不是Mscomm.CommPort的short类型。通常情况下,PortName正常返回的值为COM1、COM2……,SerialPort类最大支持的端口数突破了CommPort控件中CommPort属性不能超过16的限止,大大方便了用户串口设备的配置。

b. 通讯格式

SerialPort类对分别用[BaudRate]、[Parity]、[DataBits]、[StopBits]属性设置通讯格式中的波特率、数据位、停止位和校验位,其中[Parity]和[StopBits]分别是枚举类型Parity、StopBits,Parity类型中枚举了Odd(奇)、Even(偶)、Mark、None、Space,Parity枚举了None、One、OnePointFive、Two。

SerialPort类提供了七个重载的构造函数,既可以对已经实例化的SerialPort对象设置上述相关属性的值,也可以使用指定的端口名称、波特率和奇偶校验位数据位和停止位直接初始化 SerialPort 类的新实例。

3.串口的打开和关闭

SerialPort类没有采用MSComm.PortOpen=True/False设置属性值打开关闭串口,相应的是调用类的Open()和Close()方法。

4. 数据的发送和读取

Serial类调用重载的Write和WriteLine方法发送数据,其中WriteLine可发送字符串并在字符串末尾加入换行符,读取串口缓冲区的方法有许多,其中除了ReadExisting和ReadTo,其余的方法都是同步调用,线程被阻塞直到缓冲区有相应的数据或大于ReadTimeOut属性设定的时间值后,引发ReadExisting异常。

5.DataReceived事件

该事件类似于MSComm控件中的OnComm事件,DataReceived事件在接收到了[ReceivedBytesThreshold]设置的字符个数或接收到了文件结束字符并将其放入了输入缓冲区时被触发。其中[ReceivedBytesThreshold]相当于MSComm控件的[Rthreshold]属性,该事件的用法与MsComm控件的

OnComm事件在CommEvent为comEvSend和comEvEof时是一致的。

三.SerialPort的使用

对于熟悉MSComm控件的程序设计者,SerialPort类是相当容易上手的。在进行串口通讯时,一般的流程是设置通讯端口号及波特率、数据位、停止位和校验位,再打开端口连接,发送数据,接收数据,最后关闭端口连接这样几个步骤。

数据接收的设计方法在这里比较重要,采用轮询的方法比较浪费时间,在Visual Basic中的延时方法中一般会调用API并用DOEvents方法来处理,但程序不易控制,建议采用DataReceived事件触发的方法,合理的设置ReceivedBytesThreshold的值,若接收的是定长的数据,则将

ReceivedBytesThreshold设为接收数据的长度,若接收数据的结尾是固定的字符或字符串则可采用ReadTo的方法或在DataReceived事件中判断接收的字符是否满足条件。

SerialPort类读取数据的许多方法是同步阻塞调用,尽量避免在主线程中调用,可以使用异步处理或线程间处理调用这些读取数据的方法。

由于DataReceived事件在辅线程被引发,当收到完整的一条数据,返回主线程处理或在窗体上显示时,请注意跨线程的处理,C#可采用控件异步委托的方法Control.BeginInvoke及同步委托的方法Invoke。

四.结束语

在.NET平台下熟练使用SerialPort 类,可以很好地开发出串口通讯类程序,对于过去使用MSComm控件设计了一些通讯程序,也可以将MSComm控件替换为SerialPort类,当然为了避免对以前的项目做大的改动,可以使用SerialPort类设计一些与MSComm控件具有相同接口的类,在今后工业控制中,SerialPort类将广泛地应用于串口通讯程序的设计中,发挥着与MSComm控件一样的作用。

C# SerialPort运行方式

SerialPort中串口数据的读取与写入有较大的不同。由于串口不知道数据何时到达,因此有两种方法可以实现串口数据的读取。一、线程实时读串口;二、事件触发方式实现。

由于线程实时读串口的效率不是十分高效,因此比较好的方法是事件触发的方式。在SerialPort类中有DataReceived事件,当串口的读缓存有数据到达时则触发DataReceived事件,其中SerialPort.ReceivedBytesThreshold属性决定了当串口读缓存中数据多少个时才触发DataReceived事件,默认为1。

另外,SerialPort.DataReceived事件运行比较特殊,其运行在辅线程,不能与主线程中的显示数据控件直接进行数据传输,必须用间接的方式实现。如下:

SerialPort spSend; //spSend,spReceive用虚拟串口连接,它们之间可以相互传输数据。spSend发送数据

SerialPort spReceive; //spReceive接受数据 TextBox txtSend; //发送区 TextBox txtReceive; //接受区 Button btnSend; //数据发送按钮

delegate void HandleInterfaceUpdateDelegate(string text); //委托,此为重点 HandleInterfaceUpdateDelegate interfaceUpdateHandle;

public void InitClient() //窗体控件已在初始化 {

interfaceUpdateHandle = new HandleInterfaceUpdateDelegate(UpdateTextBox); //实例化委托对象

spSend.Open(); //SerialPort对象在程序结束前必须关闭,在此说明

spReceive.DataReceived += Ports.SerialDataReceivedEventHandler(spReceive_DataReceived);

spReceive.ReceivedBytesThreshold = 1; spReceive.Open(); }

public void btnSend_Click(object sender,EventArgs e) {

spSend.WriteLine(txtSend.Text); }

public void spReceive_DataReceived(object sender,Ports.SerialDataReceivedEventArgs e) {

byte[] readBuffer = new byte[spReceive.ReadBufferSize]; spReceive.Read(readBuffer, 0, readBuffer.Length);

this.Invoke(interfaceUpdateHandle, new string[] { Encoding.Unicode.GetString(readBuffer) }); }

private void UpdateTextBox(string text)

{

txtReceive.Text = text; }

点评:这个例子包括了这个控件几乎所有的操作,非常有参考价值.serialPort是在.net framework2.0中才有的东西,感觉这个东西和MSCOMM很相似.这里给出的例子是基于vb.net和CSHAPE 的,相应的可以在Cshape和c++中使用,基本上都是一样的.

Imports System

Imports System.IO.Ports Imports System.Threading

Public Class PortChat

Shared _continue As Boolean

Shared _serialPort As SerialPort

Public Shared Sub Main() Dim name As String Dim message As String

Dim sComparer As StringComparer = StringComparer.OrdinalIgnoreCase

Dim readThread As Thread = New Thread(AddressOf Read)

' Create a new SerialPort object with default settings.

_serialPort = New SerialPort()

' Allow the user to set the appropriate properties.

_serialPort.PortName = SetPortName(_serialPort.PortName)

_serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate) _serialPort.Parity = SetPortParity(_serialPort.Parity)

_serialPort.DataBits = SetPortDataBits(_serialPort.DataBits) _serialPort.StopBits = SetPortStopBits(_serialPort.StopBits) _serialPort.Handshake = SetPortHandshake(_serialPort.Handshake)

' Set the read/write timeouts _serialPort.ReadTimeout = 500 _serialPort.WriteTimeout = 500

_serialPort.Open() _continue = True readThread.Start()

Console.Write(\) name = Console.ReadLine()

Console.WriteLine(\)

While (_continue)

message = Console.ReadLine()

If sComparer.Equals(\, message) Then

_continue = False Else

_serialPort.WriteLine( _ String.Format(\{1}\, name, message))

End If end while

readThread.Join() _serialPort.Close() End Sub

Public Shared Sub Read() While (_continue) Try

Dim message As String = _serialPort.ReadLine()

Console.WriteLine(message) Catch ex As TimeoutException ' Do nothing End Try End While End Sub

Public Shared Function SetPortName(ByVal defaultPortName As String) As String

Dim newPortName As String

Console.WriteLine(\)

Dim s As String

For Each s In SerialPort.GetPortNames() Console.WriteLine(\, s) Next s

Console.Write(\, defaultPortName)

newPortName = Console.ReadLine()

If newPortName = \ Then

newPortName = defaultPortName End If

Return newPortName End Function

Public Shared Function SetPortBaudRate(ByVal defaultPortBaudRate As Integer) As Integer Dim newBaudRate As String

Console.Write(\, defaultPortBaudRate)

newBaudRate = Console.ReadLine()

If newBaudRate = \ Then newBaudRate = defaultPortBaudRate.ToString() End If

Return Integer.Parse(newBaudRate)

End Function

Public Shared Function SetPortParity(ByVal defaultPortParity As Parity) As Parity Dim newParity As String

Console.WriteLine(\options:\)

Dim s As String

For Each s In [Enum].GetNames(GetType(Parity)) Console.WriteLine(\, s) Next s

Console.Write(\, defaultPortParity.ToString())

newparity = Console.ReadLine()

If newparity = \ Then newparity = defaultPortParity.ToString() End If

Return CType([Enum].Parse(GetType(Parity), newParity), Parity) End Function

Public Shared Function SetPortDataBits(ByVal defaultPortDataBits As Integer) As Integer Dim newDataBits As String

Console.Write(\, defaultPortDataBits)

newDataBits = Console.ReadLine()

If newDataBits = \ Then newDataBits = defaultPortDataBits.ToString() End If

Return Integer.Parse(newDataBits) End Function

Public Shared Function SetPortStopBits(ByVal defaultPortStopBits As StopBits) As StopBits Dim newStopBits As String

Console.WriteLine(\options:\)

Dim s As String For Each s In [Enum].GetNames(GetType(StopBits))

Console.WriteLine(\, s) Next s

Console.Write(\, defaultPortStopBits.ToString())

newStopBits = Console.ReadLine()

If newStopBits = \ Then newStopBits = defaultPortStopBits.ToString() End If

Return

CType([Enum].Parse(GetType(StopBits), newStopBits), StopBits) End Function

Public Shared Function SetPortHandshake(ByVal defaultPortHandshake As Handshake) As Handshake Dim newHandshake As String

Console.WriteLine(\options:\)

Dim s As String For Each s In [Enum].GetNames(GetType(Handshake))

Console.WriteLine(\, s) Next s

Console.Write(\, defaultPortHandshake.ToString())

newHandshake = Console.ReadLine()

If newHandshake = \ Then newHandshake = defaultPortHandshake.ToString() End If

Return CType([Enum].Parse(GetType(Handshake), newHandshake), Handshake) End Function End Class using System; using System.IO.Ports; using System.Threading; public class PortChat { static bool _continue; static SerialPort _serialPort; public static void Main() { string name; string message; StringComparer stringComparer = StringComparer.OrdinalIgnoreCase; Thread readThread = new Thread(Read); // Create a new SerialPort object with default settings. _serialPort = new SerialPort(); // Allow the user to set the appropriate properties.

_serialPort.PortName = SetPortName(_serialPort.PortName); _serialPort.BaudRate = SetPortBaudRate(_serialPort.BaudRate); _serialPort.Parity = SetPortParity(_serialPort.Parity); _serialPort.DataBits = SetPortDataBits(_serialPort.DataBits); _serialPort.StopBits = SetPortStopBits(_serialPort.StopBits); _serialPort.Handshake = SetPortHandshake(_serialPort.Handshake);

// Set the read/write timeouts

_serialPort.ReadTimeout = 500; _serialPort.WriteTimeout = 500;

_serialPort.Open(); _continue = true; readThread.Start();

Console.Write(\); name = Console.ReadLine();

Console.WriteLine(\);

while (_continue)

{

message = Console.ReadLine();

if (stringComparer.Equals(\, message))

{

_continue = false; } else {

_serialPort.WriteLine(

String.Format(\{1}\, name, message) ); } }

readThread.Join(); _serialPort.Close(); }

public static void Read() {

while (_continue) {

try {

string message = _serialPort.ReadLine();

Console.WriteLine(message);

}

catch (TimeoutException) { } } }

public static string SetPortName(string defaultPortName) {

string portName;

Console.WriteLine(\);

foreach (string s in SerialPort.GetPortNames()) {

Console.WriteLine(\, s); }

Console.Write(\, defaultPortName);

portName = Console.ReadLine();

if (portName == \) {

portName = defaultPortName; }

return portName; }

public static int SetPortBaudRate(int defaultPortBaudRate)

{

string baudRate;

Console.Write(\, defaultPortBaudRate);

baudRate = Console.ReadLine();

if (baudRate == \) {

baudRate = defaultPortBaudRate.ToString(); }

return int.Parse(baudRate); }

public static Parity SetPortParity(Parity defaultPortParity) {

string parity;

Console.WriteLine(\options:\);

foreach (string s in Enum.GetNames(typeof(Parity))) {

Console.WriteLine(\, s); }

Console.Write(\, defaultPortParity.ToString());

parity = Console.ReadLine();

if (parity == \) {

parity = defaultPortParity.ToString(); }

return (Parity)Enum.Parse(typeof(Parity), parity); }

public static int SetPortDataBits(int defaultPortDataBits) {

string dataBits;

Console.Write(\, defaultPortDataBits);

dataBits = Console.ReadLine();

if (dataBits == \) {

dataBits = defaultPortDataBits.ToString(); }

return int.Parse(dataBits);

}

public static StopBits SetPortStopBits(StopBits defaultPortStopBits) {

string stopBits;

Console.WriteLine(\options:\);

foreach (string s in Enum.GetNames(typeof(StopBits))) {

Console.WriteLine(\, s); }

Console.Write(\, defaultPortStopBits.ToString());

stopBits = Console.ReadLine();

if (stopBits == \) {

stopBits = defaultPortStopBits.ToString(); }

return (StopBits)Enum.Parse(typeof(StopBits), stopBits); }

public static Handshake SetPortHandshake(Handshake defaultPortHandshake) {

string handshake;

Console.WriteLine(\options:\);

foreach (string s in Enum.GetNames(typeof(Handshake))) {

Console.WriteLine(\, s); }

Console.Write(\, defaultPortHandshake.ToString());

handshake = Console.ReadLine();

if (handshake == \) {

handshake = defaultPortHandshake.ToString(); }

return

(Handshake)Enum.Parse(typeof(Handshake), handshake); } }

SerialPort类的源代码已经放在下面,SerialPort类是由Remon Spekreijse提供的免费串口类。 CSerialPort支持线连接的串口编程,而且是基于多线程的,工作流程:

1.设置串口参数。 函数原型:

BOOL CSerialPort::InitPort(CWnd* pPortOwner, // the owner (CWnd) of the port (receives message) UINT portnr, // portnumber (1..4) UINT baud, // baudrate char parity, // parity UINT databits, // databits UINT stopbits, // stopbits DWORD dwCommEvents, // EV_RXCHAR, EV_CTS etc UINT writebuffersize) // size to the writebuffer

2.串口监测线程。

BOOL CSerialPort::StartMonitoring() BOOL CSerialPort::RestartMonitoring() BOOL CSerialPort::StopMonitoring() void CSerialPort::WriteChar(CSerialPort* port) void CSerialPort::ReceiveChar(CSerialPort* port, COMSTAT comstat) void CSerialPort::WriteToPort(char* string) 3.监测线程接收事件信息,再进行消息处理即可。 SerialPort类的应用

基于对话框的串口程序开发:

1.将SerialPort类添加进工程; 2.进行消息的映射;

(注意:在SerialPort类的头文件中的:

#define WM_COMM_RXCHAR WM_USER+7 需要手动进行映射)

在工程的主对话框头文件中,格式如下:

afx_msg LONG OnComm(WPARAM ch,LPARAM port);

在工程的主对话框的CPP文件中,格式如下:

BEGIN_MESSAGE_MAP(CMy20040889SerialPortTestDlg, CDialog)

//{{AFX_MSG_MAP(CMy20040889SerialPortTestDlg)

ON_MESSAGE(WM_COMM_RXCHAR,OnComm) ...

//}}AFX_MSG_MAP

END_MESSAGE_MAP()

然后在CPP中添加消息处理函数,本例中为OnComm。 例如:

LONG CTestDlg::OnComm(WPARAM ch, LPARAM port) {

m_strEditReceiveMsg += ch;

UpdateData(FALSE); return 0; }

//m_strEditReceiveMsg 为EDIT控件(接收框)的变量。

//下面的m_strEditSendMsg同样处理。

3.实现串口的初始化,打开/关闭串口按钮的响应函数,最后是发送信息按钮的函数实现。

void CTestDlg::OnButtonOpen() { int nPort=m_ctrlComboComPort.GetCurSel()+1;

if(m_SerialPort.InitPort(this, nPort, 9600,'N',8,1,EV_RXFLAG | EV_RXCHAR,512)) {

m_SerialPort.StartMonitoring();

m_bSerialPortOpened=TRUE; } else {

AfxMessageBox(\没有发现此串口或被占用\

m_bSerialPortOpened=FALSE; }

GetDlgItem(IDC_BUTTON_OPEN)->EnableWindow(!m_bSerialPortOpened);

GetDlgItem(IDC_BUTTON_CLOSE)->EnableWindow(m_bSerialPortOpened); }

void CTestDlg::OnButtonClose() {

m_SerialPort.ClosePort();

m_bSerialPortOpened=FALSE;

GetDlgItem(IDC_BUTTON_OPEN)->EnableWindow(!m_bSerialPortOpened);

GetDlgItem(IDC_BUTTON_CLOSE)->EnableWindow(m_bSerialPortOpened); }

void CTestDlg::OnButtonSend() {

if(!m_bSerialPortOpened) return; UpdateData(TRUE); //读入编辑框中的数据

m_SerialPort.WriteToPort((LPCTSTR)m_strEditSendMsg);//发送数据 }

传感器(c#2.0)serialPort串口通讯 2007年10月12日 星期五 15:44 using System;

using System.Collections.Generic; using System.ComponentModel; using System.Data;

using System.Data.SqlClient; using System.Drawing; using System.Text;

using System.Windows.Forms; using System.Configuration; using System.IO;

using System.IO.Ports;

namespace WindowsApplication2 {

public partial class Form1 : Form {

public Form1() {

InitializeComponent(); }

int iCount; int numbers; byte rlenth; bool bRLenth; int bRStart; bool bRParam; byte Rchk; int LParam; byte[] RParam; bool brcmd; byte RCmd; bool bRchk; bool bfinish;

private void Form1_Load(object sender, EventArgs e) {

//Form1 Form1 = new Form1(); try {

if (this.serialPort1.IsOpen) {

serialPort1.Close(); } else {

serialPort1.PortName = \//选择串口COM1

serialPort1.BaudRate = 9600; //设置通信口参数

serialPort1.DataBits = 8; //数据位

serialPort1.Parity = System.IO.Ports.Parity.None;//校验位 无校验

serialPort1.StopBits = System.IO.Ports.StopBits.One;//停止位1位

serialPort1.ReadBufferSize = 1024; //接收缓冲区大小

serialPort1.WriteBufferSize = 1024; //发送缓冲区大小 serialPort1.Open();

serialPort1.ReadExisting(); //设置Input从接收缓冲读取全部数据

serialPort1.ReceivedBytesThreshold = 1; //设置引发OnComm事件的字节长度

serialPort1.DiscardInBuffer() ; //清除接收缓冲区

serialPort1.DiscardOutBuffer() ; //清除发送缓冲区

}

string connstr =

System.Configuration.ConfigurationManager.AppSettings[\ring\

SqlConnection conn = new SqlConnection(connstr);

时间,price,card from test01\ SqlDataAdapter(sql, conn);

conn.Open();

string sql = \ name as SqlDataAdapter da = new DataSet ds = new DataSet(); da.Fill(ds, \

ultraChart1.DataSource = ds.Tables[0].DefaultView;

dataGridView1.DataSource = ds.Tables[0].DefaultView;

ultraChart1.Axis.Y.LogBase = 1000; ultraChart1.Axis.Y.RangeMax = 30000; ultraChart1.Axis.Y.RangeMin = 0; Application.EnableVisualStyles(); Application.DoEvents(); }

catch (Exception ex) {

MessageBox.Show(\ageBoxButtons.OK,MessageBoxIcon.Error); }

//ultraChart1.

}

///

/// 取前后存储器两个值对比,应付电脑异常退出 ///

/// ///

private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e) { try {

//Byte[] dataread = new Byte[8] ;

//serialPort1.Read(dataread, 0, dataread.Length);

//int productline=dataread[0]; //接口号

//iCount=dataread[5]; string connstr =

System.Configuration.ConfigurationManager.AppSettings[\ring\

SqlConnection conn = new SqlConnection(connstr);

conn.Open();

//if (dataread[0] == 0xAA) //{

// //

MessageBox.Show(productline.ToString(), \MessageBoxIcon.Error);

// numbers = 0; // numbers =

Convert.ToInt32(dataread[4]) * 256 ^ 1 + Convert.ToInt32(dataread[5]); // numbers =

Convert.ToInt32(dataread[4]) * Convert.ToInt32(System.Math.Pow(256, 1)) + Convert.ToInt32(dataread[5]);

// SqlCommand cmd = new SqlCommand(\

// cmd.CommandType = CommandType.StoredProcedure;

// cmd.Parameters.Add(\tity\

// cmd.Parameters[\\

// cmd.Parameters.Add(\s\

// cmd.Parameters[\Value = dataread[3];

// cmd.ExecuteNonQuery(); //}

byte[] DataRead; int bytData ; int bytnum;

//bytnum = serialPort1.ReadByte; ;

// TODO: NotImplemented statement: ICSharpCode.SharpRefactory.Parser.AST.VB.OnErrorStatement if (serialPort1.ReceivedBytesThreshold == serialPort1.ReceivedBytesThreshold) {

bytnum = serialPort1.BytesToRead;

DataRead = new byte[bytnum]; // TODO: NotImplemented statement: ICSharpCode.SharpRefactory.Parser.AST.VB.ReDimStatement for (int k = 0; k <= (bytnum - 1); k++)

{

bytData= serialPort1.ReadByte();

DataRead[k] =(byte) bytData;

bfinish = RFJ(DataRead[k]);

if (bfinish) {

//System.Convert.ToInt32(RCmd)为端口号

iCount = 0; for (int j = 0; j <= 1; j++)

{

iCount += System.Convert.ToInt32(RParam[j]) * Convert.ToInt32(Math.Pow(256, j));

} cmd = new SqlCommand(\ ype = CommandType.StoredProcedure;

rs.Add(\

rs[\

rs.Add(\

SqlCommand cmd.CommandT cmd.Paramete cmd.Paramete cmd.Paramete

cmd.Parameters[\

cmd.ExecuteNonQuery();

} } }

}

catch (IOException ex) {

throw (ex); }

}

private void timer1_Tick(object sender, EventArgs e) {

//this.Opacity -= -0.1; //if (this.Opacity==0) //{

// timer1.Enabled = false; // this.Close(); //} }

private void Form1_FormClosing(object sender, FormClosingEventArgs e) {

Win32.AnimateWindow(this.Handle, 2500, Win32.AW_SLIDE | Win32.AW_HIDE | Win32.AW_BLEND); // e.Cancel = true; //timer1.Enabled = true; }

private void Btn_OK_Click(object sender, EventArgs e) {

popupNotifier1.Popup(); }

private bool RFJ(byte Data)

{

bool result=false; if (bRStart == 0) {

if (Data == 0xAA) {

bRStart = 1; } }

else if (bRStart == 1) {

if (Data == 0x55) {

bRStart = 2; }

else if (Data == 0xAA) {

bRStart = 1; } else {

bRStart = 0; } }

else if (bRLenth == false) {

if (Data < 20) {

bRLenth = true; rlenth = Data; Rchk = Data;

LParam = (int)(rlenth) - 3; RParam = new byte[2]; } else {

bRStart = 0; } }

else if (brcmd == false) {

brcmd = true; RCmd = Data;

Rchk = (byte)(Rchk ^ Data); }

else if (bRParam == false) {

if (LParam >= 0) {

RParam[LParam] = Data;

Rchk = (byte)(Rchk ^ Data); LParam = LParam - 1; }

if (LParam < 0) {

bRParam = true; } }

else if (bRchk == false) {

if (Rchk == Data) {

result = true; } else {

bRStart = 0; bRLenth = false; brcmd = false; bRParam = false; } }

return result; }

} }

使用SerialPort类设计串口通讯程序 2007-12-10 14:11

一.概述

在Visual Studio 6.0中编写串口通讯程序,一般都使用Microsoft Communication Control(简称MSComm)的通讯控件,只要通 过对此控件的属性和事件进行相应编程操作,就可以轻松地实现串口通讯。但在Microsoft.Net技术广泛应用的今天,Visual Studio.Net没有将此控件加入控件库,所以人们采用了许多方法在Visual Studio.Net来编写串口通讯程序:第一种方法是通过采用Visual Studio 6.0中原来的MSComm控件这是最简单的,最方便的方法,但需要注册;第二种方法是采用微软在.NET推出了一个串口控件,基于.NET的P/Invoke调用方法实现;第三种方法是自己用API写串口通信,虽然难度高,但可以方便实现自己想要的各种功能。

现在微软推出了最新版本的Visual Studio 2005开发工具,可以不再采用第三方控件的方法来设计串口通讯程序。NET Framework 2.0 类库包含了SerialPort 类,方便地实现了所需要串口通讯的多种功能,为了使MSComm编程方法快速转换到以SerialPort类为核心的串口通讯的设计方法,本文着重讨论了Visual Studio 6.0的MSComm控件和SerialPort 类设计方法的异同点。

二.SerialPort常用属性、方法和事件 1.命名空间

System.IO.Ports命名空间包含了控制串口重要的SerialPort类,该类提供了同步 I/O 和事件驱动的 I/O、对管脚和中断状态的访问以及对串行驱动程序属性的访问,所以在程序代码起始位置需加入Using System.IO.Ports。 2.串口的通讯参数

串口通讯最常用的参数就是通讯端口号及通讯格式(波特率、数据位、停止位和校验位),在MSComm中相关的属性是CommPort和Settings。SerialPort类与MSComm有一些区别: a.通讯端口号

[PortName]属性获取或设置通信端口,包括但不限于所有可用的 COM 端口,请注意该属性返回类型为String,不是Mscomm.CommPort的short类型。通常情况下,PortName正常返回的值为COM1、COM2……,SerialPort类最大支持的端口数突破了CommPort控件中CommPort属性不能超过16的限止,大大方便了用户串口设备的配置。 b. 通讯格式

SerialPort类对分别用[BaudRate]、[Parity]、[DataBits]、[StopBits]属性设置通讯格式中的波特率、数据位、停止位和校验位,其中[Parity]和[StopBits]分别是枚举类型Parity、StopBits,Parity类型中枚举了Odd(奇)、Even(偶)、Mark、None、Space,Parity枚举了None、One、OnePointFive、Two。

SerialPort类提供了七个重载的构造函数,既可以对已经实例化的SerialPort对象设置上述相关属性的值,也可以使用指定的端口名称、波特率和奇偶校验位数据位和停止位直接初始化 SerialPort 类的新实例。 3.串口的打开和关闭

SerialPort类没有采用MSComm.PortOpen=True/False设置属性值打开关闭串口,相应的是调用类的Open()和Close()方法。

4. 数据的发送和读取

Serial类调用重载的Write和WriteLine方法发送数据,其中

WriteLine可发送字符串并在字符串末尾加入换行符,读取串口缓冲区的方法有许多,其中除了ReadExisting和ReadTo,其余的方法都是同步调用,线程被阻塞直到缓冲区有相应的数据或大于ReadTimeOut属性设定的时间值后,引发ReadExisting异常。

5.DataReceived事件

该事件类似于MSComm控件中的OnComm事件,DataReceived事件在接收到了[ReceivedBytesThreshold]设置的字符个数或接收到了文件结束字符并将其放入了输入缓冲区时被触发。其中[ReceivedBytesThreshold]相当于

MSComm控件的[Rthreshold]属性,该事件的用法与MsComm控件的OnComm事件在CommEvent为comEvSend和comEvEof时是一致的。

三.SerialPort的使用

对于熟悉MSComm控件的程序设计者,SerialPort类是相当容易上手的。在进行串口通讯时,一般的流程是设置通讯端口号及波特率、数据位、停止位和校验位,再打开端口连接,发送数据,接收数据,最后关闭端口连接这样几个步骤。

数据接收的设计方法在这里比较重要,采用轮询的方法比较浪费时间,在Visual Basic中的延时方法中一般会调用API并用DOEvents方法来处理,但程序不易控制,建议采用DataReceived事件触发的方法,合理的设置ReceivedBytesThreshold的值,若接收的是定长的数据,则将

ReceivedBytesThreshold设为接收数据的长度,若接收数据的结尾是固定的字符或字符串则可采用ReadTo的方法或在DataReceived事件中判断接收的字符是否满足条件。

SerialPort类读取数据的许多方法是同步阻塞调用,尽量避免在主线程中调用,可以使用异步处理或线程间处理调用这些读取数据的方法。 由于DataReceived事件在辅线程被引发,当收到完整的一条数据,返回主线程处理或在窗体上显示时,请注意跨线程的处理,C#可采用控件异步委托的方法Control.BeginInvoke及同步委托的方法Invoke。

四.结束语

在.NET平台下熟练使用SerialPort 类,可以很好地开发出串口通讯类程序,对于过去使用MSComm控件设计了一些通讯程序,也可以将MSComm控件替换为SerialPort类,当然为了避免对以前的项目做大的改动,可以使用

SerialPort类设计一些与MSComm控件具有相同接口的类,在今后工业控制中,SerialPort类将广泛地应用于串口通讯程序的设计中,发挥着与MSComm控件一样的作用

C#与松下FP∑可编程控制器的通信

作者:广西崇左市交警支队 彭朝威

摘要

本文介绍了通过引进ActiveX控件MSComm,利用C#语言编程,实现上位机与松下FPG-C24R2 PLC的通信,并给出了工程实例。该方法简单可靠、便于移植、实用性强,在工业控制中有着广泛的用途。 关键词:C# 串口通信 实时监控 ActiveX 控件

Abstract:A method of serial communication between Host-computer and NAIS FPG-C24R2 PLC via introducing ActiveX component MSComm by C# programming has introduced in this paper,and also presents practical

project.This method is simple、reliable and easy to transplant,and its has high application value.

Key words: C# serial communication real-time monitoring ActiveX component 一、前言

C#语言是.NET技术的核心开发语言,是一种简单、现代、面向对象和类型安全的编程语言,它实现了快速应用程序开发、跨平台部署,能够访问平台固有的资源,支持COM和.NET技术,具有C++语言的强大功能、Java语言的跨平台特性和Delphi语言的方便快捷等众多的优秀品质。FP∑是日本松下电工株式会社生产的小型可编程序控制器,它有许多规格,具有体积小、重量轻、功能齐全、编程简单、价格便宜等优点,在工业控制中应用十分广泛。 本文在Windows xp下用Visual Studio .NET 2003编制一个简单的通信程序,探讨使用MSComm控件对FPG-C24R2 PLC进行串行通信的实现方法。 二、ActiveX控件的引入

你必须有Mscomm.srg, Mscomm32.ocx,Mscomm32.dep文件在你的Windows的System目录下(注意WinNT下是System32),而且它必须正确的注册。你可以装VB6.0来获得,安装VB.6.0后,MSComm控件就自动在你的计算机上注册了,这比手工注册控件省事多了,Visual.Studio.NET2003在项目工程中(Solution)插入MSComm控件的具体步骤:新建程序后,点击Tools(工具)-->Add/Remove Toolbox items(添加/移除工具箱项)就打开了Customize Toolbox(自定义工具箱)对话框,再选择COM Components(COM组件)项,并在出现控件中就可以找到Microsoft Communications Control,version 6.0,选上该

项,再点击\,就会在Toolbox控件工具箱中看到MSComm控件的电话图标了,将它拖到窗体(Form)就可以了。 三、通信协议

FP∑系列PLC通信系统的基本协议是松下电工的专用通信协议-MEWTOCOL;PLC与计算机的通信协议是MEWTOCOL-COM。该协议采用异步通信方式,其波特率有300bps、600bps、1200bps、2400bps、4800bps、9600bps、19200bps、38400bps、57600bps、115200bps等多种可选,且报文长度可变可固定。该方式通信协议如下:

图1和图2分别为上位机发送的上位机链接命令帧读DM区数据的命令格式和由PLC返回的应答帧格式。当PLC接收到从上位机发来的ASCII码命令时自动返回ASCII码应答。

其中,%为起始符,标记每一帧报文的开始,CR为结束符,标记每一帧报文的结束,BCC为两字节的帧校验码FCS,它是从开始符\到正文结束的所有字符的ASCII码按位异或的结果,HL为PLC的站地址,为两位16进制数,如00则表示第一台PLC。#、$标注该帧报文为何种类型,上位机的命令帧由不固定的字节数组成,针对不同的识别码有不同的帧长度。但基本格式大体一致。

四、编程实现

启动Visual.Studio.NET2003,便可进入Visual C#.NET窗口环境,建立Windows应用程序,建立项目名称(complc),生成项目窗体(comForm)。在窗体上添加通信按钮button1、退出按钮button2,并在工具箱Windows窗体控件栏选中Microsoft Communications Control,version 6.0控件,如图3。

代码如下:

using System;

using System.Text; using System.Drawing;

using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace complc { ///

/// Form1 的摘要说明。 ///

public class comForm : System.Windows.Forms.Form { private AxMSCommLib.AxMSComm axMSComm1; private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox2; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button button2; private System.Windows.Forms.Label label1;

private System.Windows.Forms.Label label2;

///

/// 必需的设计器变量。 /// private System.ComponentModel.Container components = null; public comForm() {

// Windows 窗体设计器支持所必需的

InitializeComponent(); // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 } ///

/// 清理所有正在使用的资源。 /// protected override void Dispose( bool disposing ) { if( disposing )

{ if (components != null) { components.Dispose(); } } base.Dispose( disposing ); }

/// Windows 窗体设计器生成的代码

///

/// 应用程序的主入口点。 /// [STAThread] static void Main() { Application.Run(new comForm()); } private void button1_Click(object sender, System.EventArgs e)

串口

{

string ms,rd = \int i;

if (!axMSComm1.PortOpen) axMSComm1.PortOpen = true; //打开axMSComm1.InputLen = 0; //清除接收缓冲区 axMSComm1.DTREnable = true; //置DTR有效

axMSComm1.RTSEnable = true; //置RTS有效

axMSComm1.InputMode =

MSCommLib.InputModeConstants.comInputModeText; //置为二进制输入方式 axMSComm1.RThreshold = 1; //设置为接收缓冲区每接收一个字符将引发一次OnComm事件 ms=textBox1.Text; // 输入如:#RDD9001590016或#RDD0100601036 axMSComm1.Output = ms+tobcc(ms)+(char)13; // sleep(30); rd += axMSComm1.Input; textBox2.Text = rd; } private void comForm_Load(object sender, System.EventArgs e) { axMSComm1.CommPort = System.Convert.ToInt16(1); //设为com1 axMSComm1.Settings = \ }

public string tobcc(string s) //帧校验函数FCS { int t = 0; char[] chars = s.ToCharArray(); for(int i = 1;i <= s.Length-1;i++) { t = t^=(char)chars[i]; } return t.ToString().Substring(1,2); }

private void button2_Click(object sender, System.EventArgs e) { Application.Exit(); } } }

五、结论

本文所有程序均在Windows XP, Visual.Studio.NET2003环境中调试通过,

该通信方式简单,通信十分稳定可靠,从而在工业控制的小型监控系统中有着广阔的应用前景。读者可在本文的基础上,参考松下公司的MEWTOCOL-COM协议,便可轻松实现PC与松下FP∑系列PLC的通信,以完成上位机对PLC的监视与控制。