Windows ce 查看wince 6.0中的组件代码

Windows ce 查看wince 6.0中的组件代码,windows-ce,Windows Ce,大家好,我们有阅读ibutton的ibutton设备。此设备为usb,具有FTDI虚拟组件 我们有从读卡器读取数据的简单应用程序,但读卡器有问题(当更多的按钮在读卡器上滑动时-它停止发送数据) 在这种情况下,当我们发现问题(发送G和G-类似绿色二极管停止和启动),重新启动comport-但在重新启动后,comport应用程序占用100%的cpu 这里有谁知道我们需要做什么或者如何修改源代码。 非常感谢 using System; using System.IO; using System.Lin

大家好,我们有阅读ibutton的ibutton设备。此设备为usb,具有FTDI虚拟组件

我们有从读卡器读取数据的简单应用程序,但读卡器有问题(当更多的按钮在读卡器上滑动时-它停止发送数据) 在这种情况下,当我们发现问题(发送G和G-类似绿色二极管停止和启动),重新启动comport-但在重新启动后,comport应用程序占用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;
        }
    }
}

虽然很晚了,但我会把答案留在这里。 两年前,我在.NETCompactFramework和com端口上看到过类似的问题。我使用的设备是WindowsMobile6,另一个设备是comport上的BT打印机

其他手持设备运行正常,只有这台设备也有同样的问题,当你打开打印机时,应用程序运行缓慢,以至于你无法再使用它工作

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


我希望这会有帮助

已经很晚了,但我会把我的答案留在这里。 两年前,我在.NETCompactFramework和com端口上看到过类似的问题。我使用的设备是WindowsMobile6,另一个设备是comport上的BT打印机

其他手持设备运行正常,只有这台设备也有同样的问题,当你打开打印机时,应用程序运行缓慢,以至于你无法再使用它工作

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


我希望它能有所帮助

我很好奇为什么你有
serialPort1.DataReceived+=serialPort1\u DataReceived。这不应该是
serialPort1.DataReceived+=newsystem.IO.Ports.SerialDataReceivedEventHandler(serialPort1\u DataReceived)?另一个注释是关于
ReadByte
然后
Sleep
命令。可能是通信通道中塞满了数据。我会删除睡眠和调试,或者使用
ReadLine
(这就是我们在Win CE 6.0应用程序中使用RS232端口的方式)。这不应该是
serialPort1.DataReceived+=newsystem.IO.Ports.SerialDataReceivedEventHandler(serialPort1\u DataReceived)?另一个注释是关于
ReadByte
然后
Sleep
命令。可能是通信通道中塞满了数据。我会删除休眠和调试,或者使用
ReadLine
(这就是我们在Win CE 6.0应用程序中使用RS232端口的方式)。