在wince 6.0中查看comport代码

时间:2016-12-07 15:47:46

标签: windows-ce

hellou all,我们有ibutton设备用于阅读ibutton。这个设备是usb并且有FTDI虚拟comport。

我们有简单的应用程序从阅读器读取数据,但读者有问题(何时更多按钮在阅读器上滑动 - 它停止发送数据) 在这种情况下,当我们发现问题(发送G和g - 像绿色二极管停止和启动),重新启动comport - 但重新启动后comport aplication占用100%的CPU。

这里有任何人知道我们需要做什么或如何修改源代码。 非常感谢

using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using OpenNETCF;

namespace SerialDownload
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            AddLog("Vytvaram novy seriovy port");

            serialPort1 = new System.IO.Ports.SerialPort();
            serialPort1.PortName = "COM4";
            serialPort1.Handshake = System.IO.Ports.Handshake.None;
            serialPort1.DataReceived += serialPort1_DataReceived;
            //serialPort1.ErrorReceived += new System.IO.Ports.SerialErrorReceivedEventHandler(serialPort1_ErrorReceived);
            //serialPort1.PinChanged += new System.IO.Ports.SerialPinChangedEventHandler(serialPort1_PinChanged);
            serialPort1.WriteBufferSize = 10000;
            serialPort1.RtsEnable = true;
            serialPort1.DtrEnable = true;
            serialPort1.ReadTimeout = 2000;
            serialPort1.WriteTimeout = 2000;
            AddLog("Vytvoreny seriovy port");
        }
        bool zelena = false;
        private void timerResetCOM_Tick(object sender, EventArgs e)
        {

            timerResetCOM.Enabled = false;

            if (!nacitavam_kartu_serial)
            {
                try
                {

                    string znak = "G";
                    if (!zelena)
                    {
                        znak = "g";
                        zelena = true;
                    }
                    else
                    {
                        zelena = false;
                    }

                    AddLog("Posielam " + znak);
                    serialPort1.WriteTimeout = 2000;
                    serialPort1.Write(znak);
                    System.Threading.Thread.Sleep(200);
                    AddLog("Odoslane " + znak);

                }
                catch
                {
                    System.Threading.Thread.Sleep(200);
                    AddLog("Chyba serioveho portu pri odoslani G");
                    try
                    {
                        serialPort1.DataReceived -= serialPort1_DataReceived;
                        AddLog("robim discard");
                        serialPort1.DiscardOutBuffer();
                        System.Threading.Thread.Sleep(200);
                        AddLog("Discard dokonceny");
                        AddLog("Zatvaram port");
                        serialPort1.Close();
                        AddLog("port zatvoreny");

                        KillProc("SerialDownload");
                    }
                    catch
                    {
                        System.Threading.Thread.Sleep(200);
                        AddLog("Chyba zatorenia portu");

                    }

                    /*
                    try
                    {

                        AddLog("Dispose serioveho");
                        serialPort1.DataReceived -= serialPort1_DataReceived;


                        serialPort1.Dispose();
                        System.Threading.Thread.Sleep(200);

                        AddLog("Dispose serioveho OK");

                        AddLog("Vytvaram novy seriovy port");

                        serialPort1 = new System.IO.Ports.SerialPort();
                        serialPort1.PortName = "COM4";
                        serialPort1.Handshake = System.IO.Ports.Handshake.None;
                        serialPort1.DataReceived += serialPort1_DataReceived;
                        //serialPort1.ErrorReceived += new System.IO.Ports.SerialErrorReceivedEventHandler(serialPort1_ErrorReceived);
                        //serialPort1.PinChanged += new System.IO.Ports.SerialPinChangedEventHandler(serialPort1_PinChanged);
                        serialPort1.WriteBufferSize = 10000;
                        serialPort1.RtsEnable = true;
                        serialPort1.DtrEnable = true;
                        serialPort1.ReadTimeout = 2000;
                        serialPort1.WriteTimeout = 2000;
                        AddLog("Vytvoreny seriovy port");

                    }
                    catch (Exception ex)
                    {
                        AddLog("Chyba vytvaranie serioveho portu - alebo rusenia");
                    }
                    System.Threading.Thread.Sleep(1000);
                    try
                    {
                        AddLog("otvaram port");
                        serialPort1.Open();

                        serialPort1.RtsEnable = true;
                        serialPort1.DtrEnable = true;
                        serialPort1.ReadTimeout = 2000;
                        serialPort1.WriteTimeout = 2000;
                        AddLog("port otvoreny");
                    }
                    catch
                    {
                        System.Threading.Thread.Sleep(200);
                        AddLog("Chyba otvorenia portu");
                    }*/
                }
            }

        }
        bool nacitavam_kartu_serial = false;

        private void KillProc(string name)
        {
            var processes = OpenNETCF.ToolHelp.ProcessEntry.GetProcesses();

            foreach (OpenNETCF.ToolHelp.ProcessEntry process in processes)
            {
                if (name == process.ExeFile)
                {
                    process.Kill();
                }
            }
        }

        private void AddLog(string riadok)
        {
            Debug.WriteLine(DateTime.Now.ToString("yyyy-mm-dd HH:mm:ss") + ": " + riadok);
        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {

            nacitavam_kartu_serial = true;
            AddLog("Nacitacam data");
            try
            {
                string pStrTmp = string.Empty;
                while (serialPort1.BytesToRead > 0)
                {
                    pStrTmp += (char)serialPort1.ReadByte();
                    System.Threading.Thread.Sleep(15);
                }


                string card_id_t = pStrTmp;

                AddLog("Nacitane data: " + card_id_t);

                StreamWriter subor = File.CreateText("\\NAND\\karta_serial.txt");
                subor.Write(card_id_t);
                subor.Close();
            }
            catch (Exception ex)
            {
                AddLog(ex.Message);
            }
            nacitavam_kartu_serial = false;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

已经很晚了,但我会在这里留下答案。 两年前,我看到过.NET Compact Framework和com端口的类似问题。我的设备是在Windows Mobile 6上,另一台设备是BT打印机。

其他手持设备运行得很好,只有这个有完全相同的问题,当你打开打印机时 - 应用程序运行缓慢,到了你不能再用它工作的地步了。

我的时间解决方案是关闭SerialPort对象。

我希望它有所帮助