C# 49.串口调用自定义serial DLL

1. 代码
namespace serial_Test
{
    public partial class Form1 : Form
	{
  		SerialRxCallBackDelegate d1;
        Serial scom;
        public Form1()
        {
            InitializeComponent();
            d1 = new SerialRxCallBackDelegate(SerialRxCallBackFunc);
            scom = new Serial(9600,10000,d1);

        }

        public void SerialRxCallBackFunc(byte[] buf, int len)
        {
            Invoke((EventHandler)(delegate
            {
                textBoxOutput.Text = 
				System.Text.Encoding.Default.GetString (buf); 
            }
              )
            );
        }

        private void buttonSerialOpen_Click(object sender, EventArgs e)
        {
            int ret = scom.OnOff(comboBoxComm.Text,9600);
            if(ret == 1)
            {
                buttonSerialOpen.Text = "关闭";
                buttonSerialOpen.BackColor = Color.LightCoral;
            }
            else
            {
                buttonSerialOpen.Text = "打开";
                buttonSerialOpen.BackColor = Color.LimeGreen;
            }
        }


        private void comboBoxComm_DropDown(object sender, EventArgs e)
        {
            //获取电脑当前可用串口并添加到选项列表中
            comboBoxComm.Items.Clear(); // 清除串口
            comboBoxComm.Items.AddRange(scom.GetAllScomName());
        }

        private void buttonSend_Click(object sender, EventArgs e)
        {
            scom.Send(textBoxOutput1.Text);
        }
	}
}
2, 效果

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
namespace 串口实例 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void btnSend_Click(object sender, EventArgs e) { //手动发送 SendMsg(); } private void SendMsg() { this.serialPort1.Write(this.txtSend.Text); } #region 控制输入数字 private void ComboBox_TextUpdate(object sender, EventArgs e) { ComboBox cbo = sender as ComboBox; int n; if (!int.TryParse(cbo.Text, out n)) { cbo.Text = cbo.Text.Substring(0, cbo.Text.Length - 1); } else { if (n <= 0) { cbo.Text = cbo.Text.Substring(0, cbo.Text.Length - 1); } } } private void txtTime_TextChanged(object sender, EventArgs e) { TextBox text = sender as TextBox; int n; if (!int.TryParse(text.Text, out n)) { text.Text = text.Text.Substring(0, text.Text.Length - 1); } else { if (n <= 0) { text.Text = text.Text.Substring(0, text.Text.Length - 1); } else { this.timer1.Interval = n; } } } #endregion /// <summary> /// 清空显示文本框 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnClear_Click(object sender, EventArgs e) { this.txtRecive.Clear(); } private void timer1_Tick(object sender, EventArgs e) { SendMsg(); } private void cbtSend_CheckedChanged(object sender, EventArgs e) { CheckBox check = sender as CheckBox; if (check.Checked) { this.timer1.Start(); } else { this.timer1.Stop(); } } /// <summary> /// 接收返回的数据 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) { string a=""; if (this.cbkShow16.Checked) { //十六进制显示 a = this.serialPort1.ReadExisting(); } else { //字符串显示 byte[] by = new byte[this.serialPort1.BytesToRead]; this.serialPort1.Read(by, 0, this.serialPort1.BytesToRead); a = Encoding.ASCII.GetString(by); } this.txtRecive.AppendText(a); this.txtRecive.ScrollToCaret(); } private void Form1_Load(object sender, EventArgs e) { //加载 this.serialPort1.RtsEnable = true; button1_Click(this.btnOpen, null); } private void button1_Click(object sender, EventArgs e) { Button btn = sender as Button; if (btn.Text == "打开端口") { try { this.serialPort1.PortName = this.cboPortName.Text; this.serialPort1.BaudRate = Convert.ToInt32(this.cboRaudRate.Text); this.serialPort1.DataBits = Convert.ToInt32(this.cboDataBits.Text); this.serialPort1.ReceivedBytesThreshold = Convert.ToInt32(this.cboReceivedBytesThreshold.Text); this.serialPort1.Open(); btn.Text = "关闭端口"; } catch { MessageBox.Show("打开端口失败,请检查端口是否被占用."); } } else { this.serialPort1.Close(); btn.Text = "打开端口"; } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { try { this.serialPort1.Close(); } catch { } } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值