[C#] 纯文本查看 复制代码using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Configuration;

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 dotNet实现与扫码枪USB通讯

{

public partial class FrmMain : Form

{

public FrmMain()

{

InitializeComponent();

this.Load += FrmMain_Load;

}

private void FrmMain_Load(object sender, EventArgs e)

{

}

NewLandSerial newland;

private string portNo;

private void tsb_Connect_Click(object sender, EventArgs e)

{

newland = new NewLandSerial();

//4、绑定方法

newland.myShowMsg += ShowMsg;

portNo = ConfigurationManager.AppSettings["Port"].ToString();

if (newland.OpenMyComm(9600, portNo, 8, Parity.None, StopBits.One))

{

MessageBox.Show("连接成功", "建立连接");

this.tsb_Connect.Enabled = false;

this.tsb_DisConnect.Enabled = true;

}

else

{

MessageBox.Show("连接失败", "建立连接");

}

}

private void tsb_ParaSet_Click(object sender, EventArgs e)

{

new FrmSet().ShowDialog();

}

private void tsb_DisConnect_Click(object sender, EventArgs e)

{

if (newland.CloseMyComm())

{

MessageBox.Show("断开连接成功", "断开连接");

this.tsb_Connect.Enabled = true;

this.tsb_DisConnect.Enabled = false;

}

else

{

MessageBox.Show("断开连接失败", "断开连接");

}

}

//3、根据委托类型创建方法

private void ShowMsg(string Msg)

{

Invoke(new Action(() =>

{

this.txt_Info.AppendText(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + " " + Msg + Environment.NewLine);

}));

}

}

}

using System;

using System.Collections.Generic;

using System.IO.Ports;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

namespace dotNet实现与扫码枪USB通讯

{

//1、创建委托

public delegate void ShowMsgDelegate(string Msg);

public class NewLandSerial

{

//创建串口对象

private SerialPort MyComm;

//2、创建委托对象

public ShowMsgDelegate myShowMsg;

public NewLandSerial()

{

MyComm = new SerialPort();

}

//打开串口方法

public bool OpenMyComm(int iBaudRate,string PortNo,int DataBits,Parity iParity,StopBits istopbit)

{

try

{

if (MyComm.IsOpen)

{

MyComm.Close();

}

MyComm.BaudRate = iBaudRate;

MyComm.PortName = PortNo;

MyComm.DataBits = DataBits;

MyComm.Parity = iParity;

MyComm.StopBits = istopbit;

MyComm.ReceivedBytesThreshold = 1; //每次只读一个字节

MyComm.DataReceived += MyComm_DataReceived;

MyComm.Open();

return true;

}

catch (System.Exception ex)

{

return false;

}

}

byte mReceivedByte;

byte[] bData = new byte[1024];

int mReceivedByteCount = 0;

private void MyComm_DataReceived(object sender, SerialDataReceivedEventArgs e)

{

//接收数据

while (MyComm.BytesToRead>0)

{

mReceivedByte = (byte)MyComm.ReadByte();

bData[mReceivedByteCount] = mReceivedByte;

mReceivedByteCount += 1;

//异常处理,一次性字节太多

if (mReceivedByteCount >= 1024)

{

mReceivedByteCount = 0;

//清除输入缓冲区

MyComm.DiscardInBuffer();

return;

}

}

//正常处理

if (mReceivedByteCount > 0)

{

byte[] b = GetByteArray(bData, 0, mReceivedByteCount);

//5、调用委托

myShowMsg(Encoding.ASCII.GetString(b));

}

}

///

/// 自定义截取字节数组

///

///

///

///

///

private byte[] GetByteArray(byte[] byteArr,int start,int length)

{

byte[] Res = new byte[length];

if (byteArr != null && byteArr.Length >= length)

{

for (int i = 0;i

{

Res[i] = byteArr[i + start];

}

}

return Res;

}

public bool CloseMyComm()

{

if (MyComm.IsOpen)

{

MyComm.Close();

}

return true;

}

}

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐