【C#】NLS_Speed使用说明
系列文章
【C#】单号生成器(编号规则、固定字符、流水号、产生业务单号)
本文链接:https://blog.csdn.net/youcheng_ge/article/details/129129787
【C#】日期范围生成器(构建开始日期、结束日期)
本文链接:https://blog.csdn.net/youcheng_ge/article/details/129040663
【C#】组件化开发,调用dll组件方法
本文链接:https://blog.csdn.net/youcheng_ge/article/details/129492112
【C#】GridControl控件和List数据集双向绑定
本文链接:https://blog.csdn.net/youcheng_ge/article/details/129423755
【C#】数据实体类使用
本文链接:https://blog.csdn.net/youcheng_ge/article/details/128816638
【C#】单据审批流方案
本文链接:https://blog.csdn.net/youcheng_ge/article/details/128972545
【C#】二维码标签制作及打印
本文链接:https://blog.csdn.net/youcheng_ge/article/details/126884228
【C#】最全单据打印源码(设计打印模板、条形码&二维码、标签、字体)
本文链接:https://blog.csdn.net/youcheng_ge/article/details/129415723
【C#】条码管理操作手册
本文链接:https://blog.csdn.net/youcheng_ge/article/details/126589496
【C#】WebAPI发布和IIS部署,及异常处理
本文链接:https://blog.csdn.net/youcheng_ge/article/details/126539836
【C#】【提高编程效率】代码模板生成工具
本文链接:https://blog.csdn.net/youcheng_ge/article/details/126890673
【C#】【提高编程效率】Excel数据批量导入数据库
本文链接:https://blog.csdn.net/youcheng_ge/article/details/126427323
【C#】Windows服务(Service)安装及启停方案
本文链接:https://blog.csdn.net/youcheng_ge/article/details/124053794
【C#】穿透Session隔离,服务调用外部程序(无窗体界面解决)
本文链接:https://blog.csdn.net/youcheng_ge/article/details/124053033
【C#】任务计划实现,使用Quartz类
本文链接:https://blog.csdn.net/youcheng_ge/article/details/123667723
【C#】《周计划管理关于产前准备模块》解决方案20200203
本文链接:https://blog.csdn.net/youcheng_ge/article/details/122919543
【C#】源码解析正则表达式
本文链接:https://blog.csdn.net/youcheng_ge/article/details/118337074
【C#】软件版本和文件MD5记录(XML操作)
本文链接:https://blog.csdn.net/youcheng_ge/article/details/112513871
【C#】测试网络是否连通
本文链接:https://blog.csdn.net/youcheng_ge/article/details/110137288
【C#】程序开发,你使用DataTable还是List?
本文链接:https://blog.csdn.net/youcheng_ge/article/details/129792726
【C#】根据名称获取编码(Dictionary获取key)
本文链接:https://blog.csdn.net/youcheng_ge/article/details/129816701
文章目录
- 系列文章
- 前言
- 一、问题描述
- 二、解决方案
- 三、软件开发(源码展示)
- 3.1 Form1.cs
- 3.2 C_ComPort.cs
前言
我能抽象出整个世界,但是我不能抽象你。 想让你成为私有常量,这样外部函数就无法访问你。 又想让你成为全局常量,这样在我的整个生命周期都可以调用你。 可惜世上没有这样的常量,我也无法定义你,因为你在我心中是那么的具体。
哈喽大家好,本专栏为【项目实战】专栏,有别于【底层库】专栏,本专栏介绍项目开发过程中,遇到问题的解决方案,是我实际项目开发过程,相对成熟、可靠方法的提炼,我将这些问题的处理思路梳理,撰写本文分享给大家,大家遇到类似问题,可按本文方案处理。
本专栏会持续更新,不断完善,大家有任何问题,可以私信我。本专栏之间关联性较弱(文章之间依赖性较弱,没有阅读顺序区分)。如果您对本专栏感兴趣,欢迎关注吧,我将带你用最简洁的代码,实现最复杂的功能。
·提示:本专栏为项目实战篇,未接触项目开发的同学可能理解困难,不推荐阅读。
一、问题描述
NLS_Speed使用说明(C#)
二、解决方案
1.首先确定设备是chip且已经打开虚拟串口通模式连接CP
2.我的电脑右键=》设备管理=》端口号(查询设备相对应的端口号),
如下图端口号为COM10,全文将以COM10为端口号
3.设置端口号和波特率后,可点击打开串口,如下图
4.(1)所示,则虚拟串口打开成功,如果(1)显示为串口打开失败,则因查看端口是否被占用,或则端口号和波特率的正确性。
(2)显示的按钮则是打开串口和关闭串口
(3)为测试按钮,点击可出现扫描光,此时可扫描条码,扫描成功显示在下方编辑框, 超时则不显示。
(4)为连续发送模式,如设置为3000毫秒,则每三秒可扫描一次。打勾则开始,停止再次点击取消打勾。
(5)显示的读码率是:成功读码次数/总读码次数,不成功读码次数为超时
(6)处阅读时间为:手持式按键触发则是用户点击设备上的扫描键
三、软件开发(源码展示)
3.1 Form1.cs
using System;
using System.IO.Ports;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Collections;
using System.Collections.Generic;
using System.Management;
namespace TestSpeed
{
public partial class SpeedTest : Form
{
StringBuilder aBytesStr = new StringBuilder();
DateTime Time1,Time2;
int t=0, a=0;
private Boolean flage = false;
public SpeedTest()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "打开串口")
{
ComPortInfo iComInfo = new ComPortInfo();
iComInfo.PortName =com_box.Text;
iComInfo.ComParity = Parity.None;
iComInfo.ComStopBits = StopBits.One;
iComInfo.InoupSize = 5120;
iComInfo.OutPutSize = 1024;
iComInfo.PortBaudRate = int.Parse(comboBox1.Text);
iComInfo.DataBits =8;
C_ComPort.InitComPort(iComInfo);
if (C_ComPort.OpenCom() == false)
{
label3.Text = "串口打开失败";
return;
}
this.button1.Text = "关闭串口";
label3.Text = "串口打开成功";
time_ret.Start();
}else if (button1.Text == "关闭串口")
{
C_ComPort.ColseCom();
this.button1.Text = "打开串口";
label3.Text = "串口已经关闭";
time_ret.Stop();
}
}
private void time_ret_Tick(object sender, EventArgs e)
{
if (C_ComPort.ByteList.Count <= 0)
return;
byte[] iBytes = (byte[])C_ComPort.ByteList.Dequeue();
OrderStr(iBytes);
}
private void OrderStr(byte[] OrderByte)
{
string str=System.Text.Encoding.Default.GetString(OrderByte);
if (str != "")
{
if (flage)
{
Time2 = DateTime.Now;
TimeSpan ts = Time2 - Time1;
string tim = "a"+ts.TotalMilliseconds.ToString();
int i = tim.IndexOf("a");//找a的位置
int j = tim.IndexOf(".");//找b的位置
tim = (tim.Substring(i + 1)).Substring(0, j - i - 1);//找出a和b之间的字符串
tim = tim.Substring(0, tim.Length - 1);
a++;
if (tim.Length == 2) {
tim = tim + " ";
}
txt_Ret.Text += tim + " " + str + "\r\n";
flage = false;
}
else {
a++;
t++;
txt_Ret.Text += "手持式按键触发 " + str + "\r\n";
}
}
txt_ver.Text = "读码率:" + a + "/" + t;
}
private void button2_Click(object sender, EventArgs e)
{
flage = true;
Time1 = DateTime.Now;
t++;
string iSetStr = "1b 31".ToUpper().Replace(" ", "");
int iSetSum = iSetStr.Length / 2;
byte[] iSetByte = new byte[iSetSum];
for (int i = 0; i < iSetSum; i++)
{
string iByte = iSetStr.Substring(i * 2, 2).PadLeft(2, '0');
iSetByte[i] = byte.Parse(iByte, System.Globalization.NumberStyles.AllowHexSpecifier);
}
if (C_ComPort.ComSetData(iSetByte) == false)
{
MessageBox.Show("发送数据失败!\n" + C_ComPort.ErrMsg, "提示");
t--;
}
}
private void button3_Click(object sender, EventArgs e)
{
txt_Ret.Text = "";
t = 0;
a= 0;
txt_ver.Text = "读码率:" + a + "/" + t;
}
private void btnMore_CheckedChanged(object sender, EventArgs e)
{
if (btnMore.Checked) {
Int32 iTime = 0;
try
{
iTime = Int32.Parse(timebox.Text);
}
catch (Exception Err)
{
MessageBox.Show(Err.Message, "提示");
timebox.Focus();
timebox.SelectAll();
btnMore.Checked = false;
return;
}
timer_AutoSetData.Interval = iTime;
timer_AutoSetData.Start();
}
else
{
timer_AutoSetData.Stop();
}
}
private void timer_AutoSetData_Tick(object sender, EventArgs e)
{
button2_Click(null, null);
}
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.SelectedIndex = 5;
}
}
}
3.2 C_ComPort.cs
using System;
using System.Collections;
using System.IO.Ports;
using System.Windows.Forms;
namespace TestSpeed
{
public static class C_ComPort
{
public static Queue ByteList = Queue.Synchronized(new Queue());
public static int JsByteSum = 0;
public static int FsByteSum = 0;
public static bool IsGroup = false;
public static int OneGroupSum = 16;
public static bool IsOpenCom
{
get
{
if (aSerialPor == null)
return false;
return aSerialPor.IsOpen;
}
}
public static string ErrMsg
{
get { return aErrMsg; }
}
private static SerialPort aSerialPor = null;
private static string aErrMsg = "";
//private static C_Order aBuilderOrder = new C_Order();
private static ComPortInfo aComInfo;
public static void InitComPort(ComPortInfo ComSet)
{
aComInfo = ComSet;
aSerialPor = new SerialPort();
aSerialPor.BaudRate = ComSet.PortBaudRate;
aSerialPor.DataBits = 8;
aSerialPor.StopBits = StopBits.One;
aSerialPor.Parity = Parity.None;
aSerialPor.PortName = ComSet.PortName;
}
private static void InitComPort()
{
if (aSerialPor != null)
ColseCom();
aSerialPor = new SerialPort();
aSerialPor.BaudRate = aComInfo.PortBaudRate;
aSerialPor.DataBits = 8;
aSerialPor.StopBits = StopBits.One;
aSerialPor.Parity = Parity.None;
aSerialPor.PortName = aComInfo.PortName;
}
public static bool OpenCom()
{
if (aSerialPor == null)
{
aErrMsg = "未初始化的COM对象!";
return false;
}
try
{
aSerialPor.DataReceived += new SerialDataReceivedEventHandler(aSerialPor_DataReceived);
aSerialPor.Open();
JsByteSum = 0;
}
catch (Exception Err)
{
aErrMsg = Err.Message;
return false;
}
return true;
}
public static bool ColseCom()
{
ByteList.Clear();
try
{
if (aSerialPor != null)
{
aSerialPor.DiscardInBuffer();
aSerialPor.DiscardOutBuffer();
aSerialPor.Close();
aSerialPor.Dispose();
aSerialPor = null;
}
}
catch (Exception Err)
{
MessageBox.Show(Err.Message, "提示");
}
return true;
}
public static bool ComSetData(byte[] SetData)
{
if (aSerialPor == null)
{
aErrMsg = "未初始化COM对象";
return false;
}
if (aSerialPor.IsOpen == false)
{
aErrMsg = "未打开的COM对象";
return false;
}
aSerialPor.DiscardOutBuffer();
try
{
aSerialPor.Write(SetData, 0, SetData.Length);
FsByteSum += SetData.Length;
}
catch (Exception Err)
{
aErrMsg = Err.Message;
return false;
}
return true;
}
public static bool ClearDataBuffer()
{
if (aSerialPor == null)
return true;
if (aSerialPor.IsOpen == false)
return true;
try
{
aSerialPor.DiscardInBuffer();
}
catch (Exception Err)
{
aErrMsg = Err.Message;
return false;
}
return true;
}
private static void aSerialPor_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int iByteLen = aSerialPor.BytesToRead;
byte[] iBytes;
if (IsGroup)
{
int iGroup = iByteLen / OneGroupSum;
for (int i = 0; i < iGroup; i++)
{
iBytes = new byte[OneGroupSum];
aSerialPor.Read(iBytes, 0, OneGroupSum);
JsByteSum += OneGroupSum;
ByteList.Enqueue(iBytes);
}
}
else
{
iBytes = new byte[iByteLen];
aSerialPor.Read(iBytes, 0, iByteLen);
JsByteSum += iByteLen;
ByteList.Enqueue(iBytes);
}
}
}
public class ComPortInfo
{
public string PortName = "COM1";
public int PortBaudRate = 9600;
public int InoupSize = 5120;
public int OutPutSize = 1024;
public int DataBits = 8;
public StopBits ComStopBits = StopBits.One;
public Parity ComParity = Parity.None;
}
}