none
找不到FormatException的問題點 RRS feed

  • 問題

  • 下面是我的程式碼,我在執行button3的read功能時,

    產生了FormatException,可是不知道是哪一個環節出問題,請幫我看看@@謝謝喔。

     

    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.Threading;

    namespace test
    {      

        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            public const int blockCount = 63;
            public int commandIndex;
            public enum CommandType : short                     //可操作之Command種類
            {

                ReadBlock = 2,

            };

            private void initial()
            {
                int i;
                cobBlock.Items.Clear();
                for (i = 0; i <= blockCount; i++)
                {
                    cobBlock.Items.Add(i.ToString());
                    cobBlock.SelectedIndex = 1;
                    cobBlock.Enabled = false;
                }
                TextBox_Clear();
                TextBox_Status(false);

                commandIndex = -1;

            }
            private void TextBox_Clear()
            {
                txtReadData.Text = "";
            }
            private void TextBox_Status(Boolean isEnabled)
            {

                txtReadData.Enabled = isEnabled;

            }
            private void Button_Status(Boolean isEnabled)
            {
                button1.Enabled = isEnabled;
                button2.Enabled = isEnabled;
                button3.Enabled = isEnabled;
            } 

           

            private void SerialPort_DataReceived()//處理從Com-Port接收之資料
            {


                try
                {


                    switch (commandIndex)
                    {

                        case (short)CommandType.ReadBlock:
                           

                            if (txtTemp.Text.IndexOf("1B050000000000") >= 0) //判斷回傳資料是否為"1B050000000000"
                            {
                                txtReadData.Text = "00000000";
                                return;
                            }
                            break;
                        default:
                            break;
                    }
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message);
                }

            }
            // 字元 轉  ASCII CODE
            int Asc(string s)
            {

                return (int)Encoding.ASCII.GetBytes(s.ToCharArray(),0,1)[0];
            }

            // 數值 轉  16進位字串 

            string Hex(int i)
            {
                return i.ToString("X");
            }

            private void sppp(object sender, SerialDataReceivedEventArgs e)
            {
                txtReadData.Text = sp.ReadExisting();
            }
            private void SendCommendtoReader(string sendCmd)
            {
                try
                {
                    string strCmdData = sendCmd;
                    byte[] bCmd = new byte[strCmdData.Length];

                    string strTemp = "";
                    int i;

                    for (i = 0; i <= strCmdData.Length - 1; i++)
                    {
                        bCmdIdea = (byte)Asc(strCmdData.Substring(i, 1));
                        strTemp = strTemp + Hex(bCmdIdea) + ",";
                    }
                    byte[] SendBuff = new byte[strTemp.Length];


                    for (i = 0; i <= strTemp.Length - 1; i++)
                    {
                        SendBuffIdea = (byte)Asc(strTemp.Substring(i, 1));
                    }

                   sp.Write(SendBuff,0,  SendBuff.Length);

                   sp.DataReceived += new SerialDataReceivedEventHandler(sppp);        

                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message);
                }
               
            }

            private void button3_Click(object sender, EventArgs e)
            {
                string strCmd;
                try
                {
                    commandIndex = short.Parse(txtReadData.Text);

                    strCmd = "R";
                    SendCommendtoReader(strCmd);
                    timer1.Enabled = true;
                }
                catch (FormatException ex)
                {
                    MessageBox.Show(ex.Message);
                }

            }
           

        }
    }

    2008年1月26日 下午 05:48

所有回覆

  • 先在 button3_Click 放中斷點,再用 F11 慢慢執行你的程式,再把出錯行 po 上來發問吧...
    (相關的資料能提供就提供吧...例如:
    txtReadData.Text)

    2008年1月27日 上午 03:03
  • txtReadData.Text  非數值字串

    秀出來看 便知道 


    Code Snippet



       private void button3_Click(object sender, EventArgs e)


            {


                string strCmd;



               MessageBox.Show(txtReadData.Text);


                try


                {


                    commandIndex = short.Parse(txtReadData.Text);


                }


                catch (FormatException ex)


                {


                    MessageBox.Show(ex.Message);


                }


                    strCmd = "R";


                    SendCommendtoReader(strCmd);


                    timer1.Enabled = true;


              


            }



    2008年1月27日 上午 03:28
  • 我想錯誤我已經找到了

    我要讓button3能夠讀取rfid

    所以從RFID CF READER原本的VB CODE下去改寫

    其中有個commandindex

    想要讓commandindex進入到

     

            private void SerialPort_DataReceived()//處理從Com-Port接收之資料
            {
                    switch (commandIndex)
                    {

     

                        case (short)CommandType.ReadBlock:
                           

                            if (txtTemp.Text.IndexOf("1B050000000000") >= 0) //判斷回傳資料是否為"1B050000000000"
                            {
                                txtReadData.Text = "00000000";
                                return;
                            }
                            break;
                        default:
                            break;
                    }

            }

     

    不過在commandIndex = short.Parse(txtReadData.Text); 之中發生型別的錯誤

    程式也沒有進入到SerialPort_DataReceived去執行switch

     

    2008年1月27日 上午 06:36


  • 這是甚麼 ?

     你的CODE
    Code Snippet

    private void sppp(object sender, SerialDataReceivedEventArgs e)

      {
                txtReadData.Text = sp.ReadExisting();
            }


         
    2008年1月27日 上午 06:56