using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO.Ports; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WindowsFormsApp2 { public partial class frm_SerianTest : Form { /// <summary> /// frm_SerianTest构造函数 /// </summary> public frm_SerianTest() { InitializeComponent();//部件初始化 } private SerialPort SerialPort_0 = new SerialPort();//实例化串口设备 //private Encoding Encoding_0 = Encoding.Default;//选用默认编码模式 /// <summary> /// 定时器事件:实现系统时间获取 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void timer1_Tick(object sender, EventArgs e) { //实现系统时间显示 Lbl_SysTime.Text = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"); } /// <summary> /// 窗口加载事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void frm_SerianTest_Load(object sender, EventArgs e) { timer1.Interval = 1000;//时间间隔1000 单位:ms timer1.Start();//启用间隔时间事件 timer1.Tick += timer1_Tick; //获取可用端口号 string[] Ports = SerialPort.GetPortNames(); if (Ports.Length>0)//获取数组长度大于0 { cmb_Port.DataSource = Ports;//获取到的端口号赋值给Cmb_Port } //波特率设置 int[] Bauds = { 1200, 2400, 4800, 9600, 12800, 115200 }; cmb_BaudRate.DataSource = Bauds; cmb_BaudRate.SelectedIndex = 3;//开机默认9600 //校验位设置(typeof:可以取得变量的类型,或者表达式的类型) cmb_ParityBit.DataSource = Enum.GetNames(typeof(Parity));//获取Parity枚举的各名称; //数据位设置 int[] DataBits = { 5, 6, 7, 8 }; cmb_DateBit.DataSource = DataBits; cmb_DateBit.SelectedIndex = 3; //停止位设置 cmb_StopBit.DataSource = Enum.GetNames(typeof(StopBits));//获取Parity枚举的各名称; cmb_StopBit.SelectedIndex = 1; } /// <summary> /// 刷新可用端口事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btn_Refresh_Click(object sender, EventArgs e) { string[] Ports = SerialPort.GetPortNames();//获取电脑中可用的端口 if (Ports.Length > 0) { cmb_Port.DataSource = Ports; } } /// <summary> /// 与虚拟串口设备建立连接事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btn_ConnectReader_Click(object sender, EventArgs e) { if (btn_ConnectReader.Text == "连接") { if (SerialPort_0.IsOpen)//如果已经建立连接则先断开 { SerialPort_0.Close(); } try //(*Trim()是去空格*) { SerialPort_0.PortName = cmb_Port.Text.Trim(); SerialPort_0.BaudRate = Convert.ToInt32(cmb_BaudRate.Text.Trim()); SerialPort_0.DataBits = Convert.ToInt32(cmb_DateBit.Text.Trim()); SerialPort_0.Parity = (Parity)Enum.Parse(typeof(Parity), cmb_ParityBit.Text.Trim(), true);//将comboBox中的字符串转换为Parity枚举类型 SerialPort_0.StopBits = (StopBits)Enum.Parse(typeof(StopBits), cmb_StopBit.Text.Trim(), true);//将comboBox中的字符串转换为StopBits枚举类型 SerialPort_0.Encoding = Encoding.Default;//选用默认编码方式 SerialPort_0.ReceivedBytesThreshold = 1;//默认缓存区为1个字节 SerialPort_0.DataReceived += SerialPort_0_DataReceived; SerialPort_0.Open();//打开连接 //MessageBox.Show("连接成功"); Log("连接成功!" ); //禁用众多通信参数配置选项 btn_Refresh.Enabled = false; btn_ConnectReader.Text = "断开"; cmb_Port.Enabled = false; cmb_BaudRate.Enabled = false; cmb_DateBit.Enabled = false; cmb_StopBit.Enabled = false; cmb_ParityBit.Enabled = false; } catch (Exception ex) { //MessageBox.Show("连接失败"+ex.Message); Log("连接失败!" + ex.Message); } } else if (btn_ConnectReader.Text == "断开") { SerialPort_0.Close(); //MessageBox.Show("断开成功"); Log("已断开!"); //启用通信参数配置项 btn_Refresh.Enabled = true; btn_ConnectReader.Text = "连接"; cmb_Port.Enabled = true; cmb_BaudRate.Enabled = true; cmb_DateBit.Enabled = true; cmb_StopBit.Enabled = true; cmb_ParityBit.Enabled = true; } } private void SerialPort_0_DataReceived(object sender, SerialDataReceivedEventArgs e) { try { //因为要访问UI资源,所以需要使用invoke方式同步ui this.Invoke((EventHandler)(delegate { string result = string.Empty; result = SerialPort_0.ReadExisting(); Log("扫码结果:" + result); }) ); } catch (Exception ex) { //响铃并显示异常给用户 //System.Media.SystemSounds.Beep.Play(); Log(ex.Message); } } //通过ListView将信息显示 private void Log(string info) { Invoke(new Action(() => { string time = DateTime.Now.ToString(); ListViewItem Lst = new ListViewItem(time); Lst.SubItems.Add(info); listView1.Items.Insert(0, Lst); })); } private void btn_ManulCode_Click(object sender, EventArgs e) { if (tbox_ManulCode.Text==string.Empty) { Log("条码信息不能为空!"); } else { Log("手动输入条码结果:" + tbox_ManulCode.Text); tbox_ManulCode.Text = ""; } } private void btn_Send_Click(object sender, EventArgs e) { if (SerialPort_0.IsOpen) { if (tbox_SendData.Text != string.Empty) { try { SerialPort_0.Write(tbox_SendData.Text.Trim()); } catch (Exception ex) { Log("发送失败," + ex); } Log("发送内容:" + tbox_SendData.Text); } else { Log("没有发送内容!"); } } else { Log("串口未打开!"); } } } }
标签:System,扫码,Log,C#,Text,Enabled,cmb,SerialPort,串口 From: https://www.cnblogs.com/csflyw/p/18417377