none
【RFID】有關辨認tag回傳的訊息的問題 RRS feed

  • 問題

  • 各位大大好

    目前我正在作有關RFID的專題,我已經知道如何讀取tag的資料

    現在的問題是要如何利用tag回傳的UID,來決定螢幕上繪出的一個點的座標

     

    當我讓reader送出訊號後,tag總共會回傳三次訊息

    因此我利用+=的方式讓三次的回傳值顯示在同一個label上

    等到reader下次送出訊號時再將label的字串清空

     

    我打算利用switch來辨認label上的字串

    藉此定位螢幕上繪出的一點的位置

    但是無論我怎麼試

    switch都無法辨認label上tag回傳的資訊

     

    請問各位大大是否知道這是怎麼回事呢?

     

    以下附上我的程式碼

     

     

    程式碼區塊

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


     

    namespace RFIDtestingVersion2
    {
        public partial class Form1 : Form
        {
            char SOH = Convert.ToChar(1);
            char STX = Convert.ToChar(2);
            char ETX = Convert.ToChar(3);
            char BCC = Convert.ToChar(51);
            private Image backgroundImage;
            private Image backgroundImage2;
            int x = 0, y = 0, z = -550, u = 0, a = 50, b = 50;

            delegate void ChangeTxtCallback(String sValue);

            public Form1()
            {
                InitializeComponent();
                serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);

                backgroundImage = new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("RFIDtestingVersion2.NouthL.jpg"));
                backgroundImage2 = new Bitmap(Assembly.GetExecutingAssembly().GetManifestResourceStream("RFIDtestingVersion2.NouthR.jpg"));

                menuItem1.Enabled = true;
                menuItem2.Enabled = false;
                menuItem3.Enabled = false;
            }

            private void ChangeTxt(String sValue)
            {
                if (this.label1.InvokeRequired)
                {
                    ChangeTxtCallback d = new ChangeTxtCallback(pChangeTxt);
                    this.Invoke(d, new object[] { sValue });
                }
                else
                {
                    pChangeTxt(sValue);
                }
            }

            private void pChangeTxt(String sValue)
            {
                label1.Text += sValue;
            }

            private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
            {
                ChangeTxt(serialPort1.ReadExisting());
            }

            protected override void OnPaint(PaintEventArgs e)
            {
                e.Graphics.DrawImage(backgroundImage, this.ClientRectangle, new Rectangle(x, y, this.backgroundImage.Width, this.backgroundImage.Height), GraphicsUnit.Pixel);
                e.Graphics.DrawImage(backgroundImage2, this.ClientRectangle, new Rectangle(z, u, this.backgroundImage2.Width, this.backgroundImage2.Height), GraphicsUnit.Pixel);
                Pen pen = new Pen(Color.Red, 3);
                e.Graphics.DrawRectangle(pen, a, b, 5, 5);

     

                    switch (label1.Text)
                    {
                        case "s01A1ME007000012D4E52Co":
                            a = 100;
                            b = 100;
                            z = -550;
                            x = 0;
                            this.Invalidate();
                            label1.Text += " ";
                            break; //s01A1ME007000012D4E52Co

                        case "s01A1ME007000012D4F3FB?":
                            a = 150;
                            b = 150;
                            z = -550;
                            x = 0;
                            this.Invalidate();
                            label1.Text += " ";
                            break;

                        case "s01A1ME007000012D4F378n":
                            a = 200;
                            b = 200;
                            z = -550;
                            x = 0;
                            this.Invalidate();
                            label1.Text += " ";
                            break;

                        case "s01A1ME007000023D5ABFCk":
                            a = 200;
                            b = 100;
                            z = 0;
                            x = -550;
                            this.Invalidate();
                            label1.Text += " ";
                            break;

                        case "s01A1ME007000023D5AB09g":
                            a = 50;
                            b = 150;
                            z = 0;
                            x = -550;
                            this.Invalidate();
                            label1.Text += " ";
                            break;

                        case "s01A1ME007000023DBD0D1;":
                            a = 200;
                            b = 50;
                            z = 0;
                            x = -550;
                            this.Invalidate();
                            label1.Text += " ";
                            break;
                        case "SerialPort Close.":
                            a = 0;
                            b = 0;
                            this.Invalidate();
                            label1.Text += " ";
                            break;
                    }

                base.OnPaint(e);
            }

            private void Form1_KeyDown(object sender, KeyEventArgs e)
            {
                switch (e.KeyCode)
                {
                    case Keys.Up:
                        b--;
                        this.Invalidate();
                        break;

                    case Keys.Down:
                        b++;
                        this.Invalidate();
                        break;
                    case Keys.Left:
                        a--;
                        this.Invalidate();
                        break;

                    case Keys.Right:
                        a++;
                        this.Invalidate();
                        break;

                    case Keys.Enter:
                        a = 50;
                        b = 50;
                        this.Invalidate();
                        break;
                }

                if (a > 235)
                {
                    a = 0;
                    z = 0;
                    x = -550;
                    this.Invalidate();
                }
                if (b > 245)
                {
                    b = 0;
                    this.Invalidate();
                }
                if (a < 0)
                {
                    a = 230;
                    z = -550;
                    x = 0;
                    this.Invalidate();
                }
                if (b < 0)
                {
                    b = 240;
                    this.Invalidate();
                }
            }

            private void Form1_MouseDown(object sender, MouseEventArgs e)
            {
                switch (e.Button)
                {
                    case MouseButtons.Left:
                        a = MousePosition.X;
                        b = MousePosition.Y;
                        this.Invalidate();
                        break;
                }
            }

            private void Form1_MouseMove(object sender, MouseEventArgs e)
            {
                switch (e.Button)
                {
                    case MouseButtons.Left:
                        a = MousePosition.X;
                        b = MousePosition.Y;
                        this.Invalidate();
                        break;
                }
            }

            private void menuItem1_Click(object sender, EventArgs e)
            {
                serialPort1.Open();
                label1.Text = "SerialPort Open.";

                menuItem1.Enabled = false;
                menuItem2.Enabled = true;
                menuItem3.Enabled = true;
            }

            private void menuItem2_Click(object sender, EventArgs e)
            {
                string o = SOH + "S" + "01" + "A1" + STX + "010" + ETX + BCC;
                label1.Text = "";
                serialPort1.WriteLine(o);
            }

            private void menuItem3_Click(object sender, EventArgs e)
            {
                serialPort1.Close();
                label1.Text = "SerialPort Close.";

                menuItem1.Enabled = true;
                menuItem2.Enabled = false;
                menuItem3.Enabled = false;
            }

            private void label1_ParentChanged(object sender, EventArgs e)
            {

            }

            private void menuItem4_Click(object sender, EventArgs e)
            {
                z = -550;
                x = 0;
                this.Invalidate();
            }

            private void menuItem5_Click(object sender, EventArgs e)
            {
                z = 0;
                x = -550;
                this.Invalidate();
            }
        }
    }

     

     

    2007年10月30日 下午 12:30

所有回覆

  • HI   A SIR:

     

    有幾點 疑惑   可否 詳述一下   ^^

     

    1.      switch (label1.Text) {...}  為何是寫在  OnPaint 事件處理常式 中

     

    2.   

        case "s01A1ME007000012D4E52Co":

                           a = 100;
                            b = 100;
                            z = -550;
                            x = 0;
                            this.Invalidate();
                            label1.Text += " ";   // <------ 加一個空格  這是有什膜用途嗎

     

    2007年10月30日 下午 03:30
  • 1. 你可在 switch (label1.Text) 最後面加上
       default:
           MsgBox(label1.Text)
           break;

       確認執行期間 label1.Text 是否就是你所列的那些字串。

       (如 cmf 所說, switch 應該是放錯地方了)

     

    2. 你要的事件順序應該是「點選右鍵」→「點選MenuItem2」→「呈現畫面」

       不過從你定義的事件動作看來應該是
           1.點選 Form1_MouseDown 的 MouseButtons.Left 重設 a, b 及OnPaint事件。
           2.OnPaint 事件重繪畫面, 依據 label1 再次重繪。
           3.觸發 menuItem2 事件會重設 label1 送出字串給 serialPort1。
           4.什麼事也沒有發生。

     

     

    2007年11月1日 上午 02:33