C#实现扫码枪串口通信(serialPort控件方式)

 

  1 using System;
  2 using System.Collections.Generic;
  3 using System.ComponentModel;
  4 using System.Data;
  5 using System.Drawing;
  6 using System.IO.Ports;
  7 using System.Linq;
  8 using System.Text;
  9 using System.Threading.Tasks;
 10 using System.Windows.Forms;
 11 
 12 namespace SerialPort_Test
 13 {
 14     public partial class Form1 : Form
 15     {
 16 
 17         public Form1()
 18         {
 19             InitializeComponent();
 20         }
 21 
 22         private void timer1_Tick(object sender, EventArgs e)
 23         {
 24             lbl_Systime.Text = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");
 25         }
 26 
 27         private void Form1_Load(object sender, EventArgs e)
 28         {
 29             timer1.Interval = 1000;
 30             timer1.Start();
 31 
 32             cbox_Ports.DataSource = SerialPort.GetPortNames();
 33 
 34             cbox_Bauds.SelectedIndex = 2;
 35             cbox_DataBits.SelectedIndex = 3;
 36             cbox_StopBits.SelectedIndex = 0;
 37             cbox_Paritys.SelectedIndex = 0;
 38 
 39             serialPort1.DataReceived += serialPort1_Received;
 40         }
 41 
 42         private void serialPort1_Received(object sender, SerialDataReceivedEventArgs e)
 43         {
 44             try
 45             {
 46                 this.Invoke((EventHandler)(delegate
 47                 {
 48                     tbox_RcvData.Text = string.Empty;
 49                     tbox_RcvData.AppendText(serialPort1.ReadExisting());
 50                     Log("接收数据:"+ tbox_RcvData.Text);
 51                 }));
 52             }
 53             catch (Exception)
 54             {
 55 
 56                 Log("接收失败");
 57             }
 58         }
 59 
 60         private void btn_RefreshPorts_Click(object sender, EventArgs e)
 61         {
 62             cbox_Ports.DataSource = SerialPort.GetPortNames();
 63         }
 64 
 65         private void btn_SerialConnect_Click(object sender, EventArgs e)
 66         {
 67 
 68             if (btn_SerialConnect.Text=="连接")
 69             {
 70                 if (serialPort1.IsOpen)
 71                 {
 72                     serialPort1.Close();
 73                 }
 74                 try
 75                 {
 76                     serialPort1.PortName = cbox_Ports.Text.ToString().Trim();
 77                     serialPort1.BaudRate = Convert.ToInt32(cbox_Bauds.Text.ToString().Trim());
 78                     serialPort1.DataBits = Convert.ToInt32(cbox_DataBits.Text.ToString().Trim());
 79                     serialPort1.StopBits = (StopBits)Enum.Parse(typeof(StopBits), cbox_StopBits.Text.ToString().Trim(), true);
 80                     serialPort1.Parity = (Parity)Enum.Parse(typeof(Parity), cbox_Paritys.Text.ToString().Trim(), true);
 81                     serialPort1.Open();
 82                     Log("连接成功!");
 83                     groupBox1.Enabled = false;
 84                     btn_SerialConnect.Text = "断开";
 85                 }
 86                 catch (Exception ex)
 87                 {
 88 
 89                     Log("连接失败," + ex);
 90                 }
 91             }
 92             else if (btn_SerialConnect.Text == "断开")
 93             {
 94                 serialPort1.Close();
 95                 Log("已断开");
 96                 groupBox1.Enabled = true;
 97                 btn_SerialConnect.Text = "连接";
 98 
 99             }
100  
101 
102         }
103 
104         private void Log(string Info)
105         {
106             Invoke(new Action(() =>
107             {
108                 ListViewItem lst = new ListViewItem(DateTime.Now.ToString());
109                 lst.SubItems.Add(Info);
110                 listView1.Items.Insert(0, lst);
111             }));
112         }
113 
114         private void btn_Send_Click(object sender, EventArgs e)
115         {
116             if (serialPort1.IsOpen)
117             {
118                 if (tbox_SendData.Text!=string.Empty)
119                 {
120                     serialPort1.WriteLine(tbox_SendData.Text);
121                     Log("发送数据:" + tbox_SendData.Text);
122                 }
123                 else
124                 {
125                     Log("发送数据不能为空");
126                 }
127             }
128             else
129             {
130                 Log("端口未打开");
131             }
132         }
133     }
134 }

 

posted @ 2024-09-17 22:57  CSF践行  阅读(70)  评论(0编辑  收藏  举报