0

スマート カード リーダーと通信するためのソフトウェアを設計する必要があります。

最初のステップとして、カードリーダーからデータを受信し、変更時にすぐに表示できるテキストボックスに表示したい(データが送信されたことを確認するため)..

カードリーダーから送信されたデータ(以前はPLCでプログラムしていた)は256バイトの配列です

私は同じアプローチを実行しましたが、リッチテキストボックスには何も表示されません..

私のコードを見て、何が悪いのか教えてください。

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();

        }
    }
}

私は以下のように作成されたリッチテキストボックスで次の出力を取得することを期待しています..メッセージ全体が表示され、バッファ配列の各バイトの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

アップデート

手動で実行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 OK、このコードを試して、私のコメントを読んでください。

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 に答える
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

ここには 2 つの問題があります。

  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();
        }
    }

別のスレッドからのフォームの更新に関する別の SO の質問:

C#で別のスレッドからGUIを更新するには?

于 2012-05-02T11:39:01.510 に答える