窗体代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace COM_TEST
{
public partial class Form1 : Form
{
CommBar commBar;
bool buttonBool = false;
public Form1()
{
InitializeComponent();
commBar = new CommBar();
CheckForIllegalCrossThreadCalls = false;
}
private void Form1_Load(object sender, EventArgs e)
{
this.comboBox_Com.Items.Clear();
this.comboBox_Com.Items.Add("请选择COM口");
string[] comNames = commBar.GetComName();
for (int i = 0; i < comNames.Length; i++)
{
this.comboBox_Com.Items.Add(comNames[i]);
}
this.comboBox_Com.SelectedIndex = 0;
}
private void button_Test_Click(object sender, EventArgs e)
{
if (buttonBool)
{
buttonBool = false;
commBar.Close();
this.button_Test.Text = "点击测试";
}
else if (!buttonBool)
{
buttonBool = true;
this.button_Test.Text = "正在测试……";
commBar.SerialPortValue(this.comboBox_Com.Text, Convert.ToInt32(this.comboBox_comPl.Text));
if (commBar.Open())
commBar.serialPort.DataReceived += new System.IO.Ports.SerialDataReceivedEventHandler(serialPort_DataReceived);
}
}
private void CodeText(CommBar commBar)
{
this.textBox_TestTxt.Text = "";
this.textBox_TestTxt.Text = commBar.Code;
}
private delegate void ModifyButton_dg(CommBar commBar);
void serialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
System.Threading.Thread.Sleep(100);
byte[] m_recvBytes = new byte[commBar.serialPort.BytesToRead];
int result = commBar.serialPort.Read(m_recvBytes, 0, m_recvBytes.Length);
if (result <= 0)
return;
commBar.Code = Encoding.ASCII.GetString(m_recvBytes, 0, m_recvBytes.Length);
this.Invoke(new ModifyButton_dg(CodeText), commBar);
commBar.serialPort.DiscardInBuffer();
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
commBar.Close();
commBar.serialPort.Dispose();
}
}
}
扫描枪工作类代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Threading;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace COM_TEST
{
class CommBar
{
public SerialPort serialPort;
public string Code { get; set; }
public CommBar()
{
serialPort = new SerialPort();
}
public bool IsOpen
{
get
{
return serialPort.IsOpen;
}
}
public bool Open()
{
if (serialPort.IsOpen)
{
Close();
}
serialPort.Open();
if (serialPort.IsOpen)
{
return true;
}
else
{
MessageBox.Show("串口打开失败!");
return false;
}
}
public void Close()
{
serialPort.Close();
}
public void WritePort(byte[] send, int offSet, int count)
{
if (IsOpen)
{
serialPort.Write(send, offSet, count);
}
}
public string[] GetComName()
{
string[] names = null;
try
{
names = SerialPort.GetPortNames();
}
catch (Exception)
{
System.Windows.Forms.MessageBox.Show("找不到串口");
}
return names;
}
public void SerialPortValue(string portName, int baudRate)
{
serialPort.PortName = portName;
serialPort.BaudRate = baudRate;
serialPort.DataBits = 8;
serialPort.StopBits = System.IO.Ports.StopBits.One;
serialPort.Parity = System.IO.Ports.Parity.None;
serialPort.ReadTimeout = 100;
}
}
}