• WinCE SerialPort


    直接上代码

    窗体加载
    1 privatevoid Form1_Load(object sender, EventArgs e)
    2 {
    3 serialPort1.PortName ="COM3";
    4 serialPort1.BaudRate =9600;
    5 serialPort1.DataBits =8;
    6 serialPort1.Open();
    7 if (serialPort1.IsOpen)
    8 {
    9 serialPort1.PortName+"端口已打开"
    10 }
    11 else
    12 {
    13 serialPort1.PortName+"端口打开 失败"
    14 }
    15 }
    SerialPort DataReceived
    privatevoid serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
    System.Threading.Thread.Sleep(
    120);
    int bytes = serialPort1.BytesToRead;
    byte[] buffer =newbyte[bytes];
    if (bytes ==0)
    {
    return; }
    serialPort1.Read(buffer,
    0, bytes);
    RfidLoad(ByteToStr(buffer));
    }
    委托
    publicvoid RfidLoad(string msg)
    {
    this.Invoke(new EventHandler(delegate
    {
    try
    {
    if (!string.IsNullOrEmpty(msg))
    {
    if (!listBox1.Items.Contains(msg))
    {
    listBox1.Items.Add(msg);
    }
    }
    }
    catch (Exception e)
    {
    MessageBox.Show(e.ToString());
    }
    }));
    }
    转换
    publicstring ByteToStr(byte[] value)
    {
    string item =string.Empty;
    if (((value.Length >8) && (value.Length >= ((((value[4] >>3) +1) *2) +4))) && ((((value[2] &0x7f) ==0x10) || ((value[2] &0x7f) ==0x11)) && (value[3] !=1)))
    {
    int num = ((value[4] >>3) +1) *2;
    bool flag =false;
    for (int i =0; i < num; i++)
    {
    if (!flag)
    {
    if (value[4+ i] ==0xff)
    {
    flag
    =true;
    }
    else
    {
    item
    += Convert.ToString((int)(value[4+ i] >>4), 0x10) + Convert.ToString((int)(value[4+ i] &15), 0x10);
    }
    }
    else
    {
    flag
    =false;
    item
    += Convert.ToString((int)(value[4+ i] >>4), 0x10) + Convert.ToString((int)(value[4+ i] &15), 0x10);
    }
    }
    }
    if (!string.IsNullOrEmpty(item))
    {
    return item.ToUpper().Substring(4, 24);
    }
    return item;
    }


  • 相关阅读:
    微软下载
    stsadm.exe
    将表数据生成Insert脚本
    silverlight客户端保存文件乱码问题
    MySql全文索引
    SQL 自增主键从1开始
    Redis 数据结构之简单动态字符串SDS
    vs2012升级到vs2013后,sql server 无法通过IP登录解决方案
    常见XSD问题
    EntitySpace 常用语句
  • 原文地址:https://www.cnblogs.com/macheal/p/2078545.html
Copyright © 2020-2023  润新知