159彩票


设为159彩票   加入收藏    网站地图
   
网站159彩票 西铁城条码打印机 西铁城资讯 解决方案 公司介绍 公司动态 联系我们
 
西铁城资讯

C#例子-串口读取机器状态与序列号(DM4)

发表日期:2011年11月08日

CLP - 521C,- 621C电,CL -,CL - S621C S521C
CL - S700,CL - S703,CL - S700R,CL - S703R

使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Drawing;
使用System.Text;
使用System.Windows.Forms;
使用System.IO.Ports;
使用System.Diagnostics;
使用的System.Threading;
命名空间DEMOtest
{
    公共部分类的调试器:表格
    {
        私人委托无效ShowHandle(字符串PutOut);
        私人委托无效RxOrTxHandle();
        私有静态SerialPort mySerialPort =新SerialPort();
        私人静态布尔Is16Show = FALSE;
        私人静态布尔Is16Send = TRUE;
        私有静态诠释SendPeriod = 250;
        私有静态诠释ReadBuffierSize = 4000;
        私有静态诠释WriteBuffierSize = 2;
        公共调试()
        {
            ();
        }
        私人无效Main_Load(对象发件人,EventArgs的发送)
        {
            MainInit();
        }
        私人无效btnExit_Click(对象发件人,EventArgs的发送)
        {
            this.Close();
            Application.Exit();
        }
        私人无效btnOpen_Click(对象发件人,EventArgs的发送)
        {
            (this.btnOpen.Text ==“打开串口”)
            {
                this.btnOpen.Text =“关闭串口”;
                this.lblComState.Text =“打开”;
                OpenSerialPort();
            }
            其他
            {
                this.btnOpen.Text =“打开串口”;
                this.lblComState.Text =“关闭”;
                CloseSerialPort();
            }
        }
        私人无效btnClearShow_Click(对象发件人,EventArgs的发送)
        {
            this.txtShow.Text =“”;
        }

        私人无效btnManualSend_Click(对象发件人,EventArgs的发送)
        {
            / /字符串普京=“\ R \ n吗?”;
            字符串普京=(char)的2 +(char)的27 +“的NS \ R \ N”;
            发送(普京);
        }
        私人无效mySerialPort_DataReceived(对象发件人,SerialDataReceivedEventArgs发送)
        {
            字节[]字节=新字节[mySerialPort.BytesToRead];
            INT PutOut = mySerialPort.Read(字节,0,mySerialPort.BytesToRead);
            显示(System.Text.Encoding.Default.GetString(字节数));
        }

        私人无效展(字符串PutOut)
        {
                (this.txtShow.InvokeRequired)
                {
                    ShowHandle SH =新ShowHandle(查看);
                    this.Invoke(SH,新的对象[] {PutOut});
                }
                其他
                {
                    (Is16Show)
                    {
                        this.txtShow.Text + = StrToHex(PutOut);
                    }
                    其他
                    {
                        this.txtShow.Text + = Put​​Out;
                    }
                }
        }
        发送私人无效(字符串普京)
        {
            如果(!mySerialPort.IsOpen)
            {
                MessageBox.Show(“请开启串口”);
            }
            其他
            {
                mySerialPort.Write(Encoding.Default.GetBytes(普京),0,Encoding.Default.GetBytes(普京)的长度。);
              
            }
        }
        私人无效mySerialPort_ErrorReceived(对象发件人,SerialErrorReceivedEventArgs发送)
        {
            MessageBox.Show(e.ToString());
        }
        私人无效MainInit()
        {
            BindSerialName();
            InitSerialPort();
            mySerialPort.DataReceived + =新SerialDataReceivedEventHandler(mySerialPort_DataReceived);
            mySerialPort.ErrorReceived + =新SerialErrorReceivedEventHandler(mySerialPort_ErrorReceived);
        }
        私人无效InitSerialPort()
        {
            mySerialPort.BaudRate = 9600;
            mySerialPort.Parity = Parity.None;
            mySerialPort.PortName = this.cbSerialName.SelectedItem.ToString();
            mySerialPort.DataBits = 8;
            mySerialPort.StopBits = StopBits.One;
        }

        私人无效BindSerialName()
        {
            字符串[] =埠SerialPort.GetPortNames();
            的foreach(字符串中的港口)
            {
                this.cbSerialName.Items.Add(端口);
            }
            (cbSerialName.Items.Count> 0)
            {
                (cbSerialName.Items.Contains(“COM1”))
                {
                    cbSerialName.SelectedItem =“COM1”;
                }
                其他
                {
                    cbSerialName.SelectedIndex = 0;
                }
            }
            其他
            {
                MessageBox.Show(“该机器不存在串口,请核实”);
            }
        }
        私人无效OpenSerialPort()
        {
            (mySerialPort.IsOpen)
            {
                尝试
                {
                    mySerialPort.Close();
                }
                (InvalidOperationException异常E1)
                {
                    MessageBox.Show(e1.Message);
                }
            }
            尝试
            {
                SetSerialPort();
                mySerialPort.Open();
            }
            (InvalidOperationException异常E2)
            {
                this.lblComState.Text =“错误”;
                MessageBox.Show(e2.Message);
            }
            (ArgumentOutOfRangeException E3)
            {
                this.lblComState.Text =“错误”;
                MessageBox.Show(e3.Message);
            }
            (ArgumentException的E4)
            {
                this.lblComState.Text =“错误”;
                MessageBox.Show(e4.Message);
            }
            (UnauthorizedAccessException E5)
            {
                this.lblComState.Text =“错误”;
                MessageBox.Show(e5.Message);
            }
            捕获
            {
                this.lblComState.Text =“错误”;
                抛出新的异常(异常,请通知管理员“);
            }
        }
        私人无效CloseSerialPort()
        {
            尝试
            {
                mySerialPort.Close();
            }
            (InvalidOperationException异常发送)
            {
                MessageBox.Show(e.Message);
            }
            捕获
            {
                抛出新的异常(异常,请通知管理员“);
            }
        }
        私人无效SetSerialPort()
        {
            InitSerialPort();
            mySerialPort.ReadBufferSize = ReadBuffierSize;
            mySerialPort.WriteBufferSize = WriteBuffierSize;
            (Is16Send)
            {
                mySerialPort.Encoding = Encoding.Unicode;
            }
            其他
            {
                mySerialPort.Encoding = Encoding.ASCII;
            }
        }
        私人字符串StrToHex(字符串普京)
        {
            StringBuilder的SB =新的StringBuilder();
            CHAR [] = Put​​In.ToCharArray();
            FOREACH(CHAR值C)
            {
                字符串TEMP = String.Format(“{0:X}”,Convert.ToInt32(C));
                sb.Append(临时);
            }
            返回sb.ToString();
        }
    }
}


copyright@东道包装 西铁城条码打印机 全国免费服务热线:18925423844
宁夏11选5走势图 江苏快3开奖结果 贵州11选5走势图 皇冠足彩 皇冠足彩 梭哈游戏平台 华东15选5走势图