C#串口

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; using System.Threading; namespace TestSerialPort { public partial class frmTESTSerialPort : Form { public frmTESTSerialPort() { InitializeComponent(); Control.CheckForIllegalCrossThreadCalls = false; } private Button button1; private TextBox txtSend; private TextBox txtReceive; private Label label1; private Label label2; /// /// 必需的设计器变量。 /// private System.ComponentModel.IContainer components = null; /// /// 清理所有正在使用的资源。 /// /// 如果应释放托管资源,为 true;否则为 false。 protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗体设计器生成的代码 /// /// 设计器支持所需的方法 - 不要 /// 使用代码编辑器修改此方法的内容。 /// private void InitializeComponent() { this.button1 = new System.Windows.Forms.Button(); this.txtSend = new System.Windows.Forms.TextBox(); this.txtReceive = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // button1 // this.button1.Location = new System.Drawing.Point(440, 379); this.button1.Name = "button1 "; this.button1.Size = new System.Drawing.Size(75, 23); this.button1.TabIndex = 0; this.button1.Text = "发送 "; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click); // // txtSend // this.txtSend.Location = new System.Drawing.Point(59, 12); this.txtSend.Multiline = true; this.txtSend.Name = "txtSend "; this.txtSend.Size = new System.Drawing.Size(456, 164); this.txtSend.TabIndex = 2; // // txtReceive // this.txtReceive.Location = new System.Drawing.Point(59, 200); this.txtReceive.Multiline = true; this.txtReceive.Name = "txtReceive "; this.txtReceive.Size = new System.Drawing.Size(456, 164); this.txtReceive.TabIndex = 2; // // label1 // this.label1.Location = new System.Drawing.Point(13, 15); this.label1.Name = "label1 "; this.label1.Size = new System.Drawing.Size(41, 12); this.label1.TabIndex = 0; this.label1.Text = "发送 "; // // label2 // this.label2.Location = new System.Drawing.Point(13, 213); this.label2.Name = "label2 "; this.label2.Size = new System.Drawing.Size(41, 12); this.label2.TabIndex = 0; this.label2.Text = "接收 "; // // frmTESTSerialPort // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(546, 434); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.txtReceive); this.Controls.Add(this.txtSend); this.Controls.Add(this.button1); this.Name = "frmTESTSerialPort "; this.Text = "串口试验 "; this.ResumeLayout(false); this.PerformLayout(); } #endregion private void button1_Click(object sender, EventArgs e) { //实例化串口对象(默认:COMM1,9600,e,8,1) SerialPort serialPort1 = new SerialPort(); //更改参数 serialPort1.PortName = "COM1 "; serialPort1.BaudRate = 19200; serialPort1.Parity = Parity.Odd; serialPort1.StopBits = StopBits.Two; //上述步骤可以用在实例化时调用SerialPort类的重载构造函数 //SerialPort serialPort = new SerialPort( "COM1 ", 19200, Parity.Odd, StopBits.Two); //打开串口(打开串口后不能修改端口名,波特率等参数,修改参数要在串口关闭后修改) serialPort1.Open(); //发送数据 SendStringData(serialPort1); //也可用字节的形式发送数据 //SendBytesData(serialPort1); //开启接收数据线程 ReceiveData(serialPort1); } //发送字符串数据 private void SendStringData(SerialPort serialPort) { serialPort.Write(txtSend.Text); } /// /// 开启接收数据线程 /// private void ReceiveData(SerialPort serialPort) { //同步阻塞接收数据线程 Thread threadReceive=new Thread(new ParameterizedThreadStart(SynReceiveData)); threadReceive.Start(serialPort); //也可用异步接收数据线程 //Thread threadReceiveSub = new Thread(new ParameterizedThreadStart(AsyReceiveData)); //threadReceiveSub.Start(serialPort); } //发送二进制数据 private void SendBytesData(SerialPort serialPort) { byte[] bytesSend=System.Text.Encoding.Default.GetBytes(txtSend.Text ); serialPort.Write(bytesSend, 0, bytesSend.Length); } //同步阻塞读取 private void SynReceiveData(object serialPortobj) { SerialPort serialPort = (SerialPort)serialPortobj; System.Threading.Thread.Sleep(0); serialPort.ReadTimeout = 1000; try { //阻塞到读取数据或超时(这里为2秒) byte firstByte=Convert.ToByte(serialPort.ReadByte()); int bytesRead=serialPort.BytesToRead ; byte[] bytesData=new byte[bytesRead+1]; bytesData[0] = firstByte; for (int i = 1; i <=bytesRead; i++) bytesData[i] = Convert.ToByte( serialPort.ReadByte()); txtReceive.Text = System.Text.Encoding.Default.GetString(bytesData); } catch(Exception e) { MessageBox.Show(e.Message); //处理超时错误 } serialPort.Close(); } //异步读取 private void AsyReceiveData(object serialPortobj) { SerialPort serialPort = (SerialPort)serialPortobj; System.Threading.Thread.Sleep(500); try { txtReceive.Text = serialPort.ReadExisting(); } catch (Exception e) { MessageBox.Show(e.Message); //处理错误 } serialPort.Close(); } } static class Program { /// /// 应用程序的主入口点。 /// [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new frmTESTSerialPort()); } } }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#串口介绍以及简单串口通信程序设计实现 源代码和串口程序介绍连接:https://www.cnblogs.com/JiYF/p/6618696.html 本站积分太贵,自己变得。。直接到连接地址下载代码 周末,没事干,写个简单的串口通信工具,也算是本周末曾来过,废话不多,直接到主题 串口介绍   串行接口简称串口,也称串行通信接口或串行通讯接口(通常指COM接口),是采用串行通信方式的扩展接口。(至于再详细,自己百度) 串口应用:   工业领域使用较多,比如:数据采集,设备控制等等,好多都是用串口通信来实现!你要是细心的话,你会发现,目前家用国网智能电能表就具备RS485通信总线(串行总线的一种)与RS232可以相互转化(当然一般,非专业的谁也不会闲的蛋疼,趴电表上瞎看,最多也就看看走了多少度电) RS232 DB9介绍: 1.示意图 2.针脚介绍: 载波检测(DCD) 接受数据(RXD) 发出数据(TXD) 数据终端准备好(DTR) 信号地线(SG) 数据准备好(DSR) 请求发送(RTS) 清除发送(CTS) 振铃指示(RI) 3.实物图: 以下是我购买XX公司的一个usb转串口线:这个头就是一个公头,另一端是一个usb口 笨小孩串口工具运行图: 1.开启程序 2.发送一行字符串HelloBenXH,直接将针脚的发送和接收链接起来就可以测试了(针脚2 接受数据(RXD) 和3 发出数据(TXD))直接链接, C#代码实现:采用SerialPort 1.实例化一个SerialPort [csharp] view plain copy 在CODE上查看代码片派生到我的代码片 private SerialPort ComDevice = new SerialPort(); 2.初始化参数绑定接收数据事件 [csharp] view plain copy 在CODE上查看代码片派生到我的代码片 public void init() { btnSend.Enabled = false; cbbComList.Items.AddRange(SerialPort.GetPortNames()); if (cbbComList.Items.Count > 0) { cbbComList.SelectedIndex = 0; } cbbBaudRate.SelectedIndex = 5; cbbDataBits.SelectedIndex = 0; cbbParity.SelectedIndex = 0; cbbStopBits.SelectedIndex = 0; pictureBox1.BackgroundImage = Properties.Resources.red; ComDevice.DataReceived += new SerialDataReceivedEventHandler(Com_DataReceived);//绑定事件 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值