扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:彭朝威 来源:vckbase 2007年11月11日
关键字:

using System;
using System.Text;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace complc
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
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;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public comForm()
{
// Windows 窗体设计器支持所必需的
InitializeComponent();
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
/// Windows 窗体设计器生成的代码
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[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; // 输入如:%01#RDD9001590016或%01#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 = "9600,n,8,1";
}
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();
}
}
} 五、结论如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。