0

我必须设计一个软件来与智能卡读卡器通信..

作为第一步,我想从读卡器接收数据并将它们显示在一个文本框中,当它发生变化时我可以立即看到它(以确保已发送数据)..

读卡器发送的数据(我以前用 PLC 编程)是一个 256 字节的数组

我确实执行了相同的方法,但我的richtextbox 中没有显示任何内容。

我想让你看看我的代码并告诉我出了什么问题:

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

namespace mycard
{
    public partial class Form1 : Form
    {
        byte[] memo = new byte[256];
        byte[] buffer = new byte[255];

        private SerialPort serialPort = new SerialPort();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            serialPort.DataReceived += 
                new SerialDataReceivedEventHandler(serialPort_DataReceived);
        }

        //open port
        private void button1_Click(object sender, EventArgs e)
        {
            if (s1.IsOpen)
            {
                s1.Close();
            }
            s1.Open();
        }


        //Eject order which is working fine
        private void button1_Click_1(object sender, EventArgs e)
        {
            byte[] data= new byte[4];

            memo[0]=0x60;
            memo[1]=0x00;
            memo[2]=0x02;
            memo[3]=0x43;
            memo[4]=0x31; //32
            memo[5]=0x10; //13

            s1.Write(memo,0,6);
        }

        //close port
        private void button2_Click(object sender, EventArgs e)
        {
            s1.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            txtDataReceived.Text = "\r\n";
        }

        public delegate void myDelegate();
        public void updateTextBox()
        {

            byte[] buffer = new byte[255];

            serialPort.Read(buffer, 0, 4);
            txtDataReceived.Text = (buffer[0].ToString() + Environment.NewLine 
                + buffer[1].ToString() + Environment.NewLine 
                + buffer[2].ToString() + Environment.NewLine 
                + buffer[3].ToString() + Environment.NewLine);

            // i have tried the following code as well but i got nothing as well
            // txtDataReceived.AppendText(serialPort.ReadExisting());
            // txtDataReceived.ScrollToCaret();

        }
    }
}

我希望在创建的 Richtextbox 中得到以下输出,如下所示.. 整个消息出现在哪里,并且缓冲区数组的每个字节的 xxx 值根据智能卡读卡器的状态(或者换句话说根据什么卡读者发送)...

例如(我在上面的代码中一直在尝试做的事情).. 当有连接并且卡插入读卡器时,缓冲区 [0]、[1]、[2] 和 [3] 值应该立即从 0 变为 48,48,48,48(由读卡器发送).. 等等..

buffer:
[0]=xxx
[1]=xxx
[2]=xxx
[3]=xxx
[4]=xxx
[5]=xxx
[6]=xxx
...
...
...
[255]=xxx

相反,盒子是空的......

希望我的概率。很清楚..我真的很想在这里找到解决方案..这对我来说是一件非常重要的事情..我花了很多时间尝试但没有成功


智能卡读卡器类型:KDM9650

4

3 回答 3

0

您需要实际读取serialPort_DataReceived. 你在这里什么都没做。另外:有一个button1_Click事件和一个button1_Click_1事件。哪个是被调用的button1?如果第一个没有被调用,则串行端口根本没有打开。

另外请注意:该DataReceived事件在其自己的线程中调用,因此在更新 UI 之前,您需要使用this.Invoke(WinForms) 或this.Dispatcher.Invoke(WPF) 更改为 UI 线程。

于 2012-05-02T11:29:59.693 回答
0

我在这里看到两个问题:

  1. 您没有从端口读取数据..

  2. 我认为您会遇到委托更新表单的问题,因为它将从另一个线程调用。

为了让您继续前进,您可以添加一个计时器,并在计时器滴答事件中读取数据(如果端口有任何数据):

    private void timer1_Tick(object sender, EventArgs e)
    {
        if (s1.BytesToRead > 0)
        {
            //write text to text box

            //e.g. maybe as a start try
            txtDataReceived.Text += s1.ReadExisting();
        }
    }

另一个关于从另一个线程更新表单的问题:

如何从 C# 中的另一个线程更新 GUI?

于 2012-05-02T11:39:01.510 回答
0

更新

手动运行updateTextBox()(将其添加到 ButtonClick EventHandler)

public void updateTextBox()
    {
        byte[] buffer = new byte[255];

        serialPort.Read(buffer, 0, 4); 
        var i = 0; //Press F9 here, it will put a breakpoint here
    }

然后运行您的应用程序,并在断点处停止后检查缓冲区是否为空?

Update2 好的,试试这段代码,然后阅读我的评论。

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

namespace mycard
{
    public partial class Form1 : Form
    {
        byte[] memo = new byte[256];
        byte[] buffer = new byte[255];

        //private SerialPort serialPort = new SerialPort();
        //We don't need this object. We never configure it, it's not the 
        //same object which you use to write 'eject bytes'!
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           //subscribe on data received event here. IMPORTANT: we 'listen' s1 object here!
            s1.DataReceived += 
                new SerialDataReceivedEventHandler(serialPort_DataReceived);
        }

        //open port
        private void button1_Click(object sender, EventArgs e)
        {
            if (s1.IsOpen)
            {
                s1.Close();
            }
            s1.Open();
        }

        //Eject order which is working fine
        private void button1_Click_1(object sender, EventArgs e)
        {
            byte[] data= new byte[4];

            memo[0]=0x60;
            memo[1]=0x00;
            memo[2]=0x02;
            memo[3]=0x43;
            memo[4]=0x31; //32
            memo[5]=0x10; //13

            s1.Write(memo,0,6);
        }

        //close port
        private void button2_Click(object sender, EventArgs e)
        {
            s1.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            txtDataReceived.Text = "\r\n";
        }

        public void updateTextBox()
        {
            byte[] buffer = new byte[255];

            //we must read from s1, not from serealPort!!!
            s1.Read(buffer, 0, 4);
            txtDataReceived.Text = (buffer[0].ToString() + Environment.NewLine 
                + buffer[1].ToString() + Environment.NewLine 
                + buffer[2].ToString() + Environment.NewLine 
                + buffer[3].ToString() + Environment.NewLine);
        }

        //data received, let's read it!
        private void serialPort_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
          updateTextBox(); 
        }
    }
}

你的问题 你有一个SerialPort类的实例。实例名称是s1。您已经对其进行了配置,但您尝试从新serialPort对象中读取数据。显然那里没有数据。

串行端口详细信息

于 2012-05-02T11:28:25.827 回答