none
Windows Form to PIC RRS feed

  • 問題

  • public partial class Form1 : Form
        {
            delegate void UpdateTextEventHandler(string text);//委托,此为重点
            UpdateTextEventHandler updateText;
    
            public Form1()
            {
                InitializeComponent();
    
                string[] comport = SerialPort.GetPortNames();   //宣告comport字串陣列=電腦上的序列埠
    
                comboBox1.DataSource = comport; //combobox裡的來源=comport
    
                updateText = new UpdateTextEventHandler(UpdateTextBox);//实例化委托对象
    
                serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
            }
            
            private void openLoadFilehexToolStripMenuItem_Click(object sender, EventArgs e)
            {
                if(openFileDialog1.ShowDialog()==DialogResult.OK)
                {
                    label3.Text = openFileDialog1.SafeFileName;
    
                    string name = openFileDialog1.FileName;
                    
                    textBox3.Text = File.ReadAllText(name).Replace(":","");
                }
            }
            public void openTxt(string filePath)
            {
                int amount=0;
                string line;
                
                //透過路徑讀取檔案
                
                StreamReader sr = new StreamReader(filePath);
                string linein;
                
                while ((line = sr.ReadLine()) != null)//&& ((line = sr.ReadLine()) != "\r\n")
                {
                    linein = line.Replace(":", "");
                    amount = (linein.Length) / 2;
                    //開始一行一行讀取
                    //textBox4.Text = line;
                    byte[] hex = new byte[amount];
                    for (int i = 0; i < amount; i++)
                    {
                        hex[i] = Convert.ToByte(linein.Substring(i * 2, 2), 16);//分割+string to byte
                    }
                    serialPort1.Write(hex, 0, hex.Length);
                    flag = 1;
                }
                 sr.Close();
                
            }
            private void button4_Click(object sender, EventArgs e)
            {
                if(label3.Text!= "<No Hex File Currently Selected>")
                {
                    openTxt(openFileDialog1.FileName);
                }
            }
    
            int a = 0;
            byte[] data = new byte[16];
    
            private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                data[a] = Convert.ToByte(serialPort1.ReadByte());
                a++;
                if (a == 16)
                {
                    this.Invoke(updateText, new string[] { data.ToString() });
    
                    a = 0;
                }
            }
    
            private void UpdateTextBox(string text)
            {
              
                textBox4.Text= BitConverter.ToString(data).Replace("-", "").ToLower();
            }
        }
    }

    各位前輩好:
    小弟目前面臨接收的問題
    這段程式是跟MCU對傳
    當我的電腦傳hex檔
    MCU依照hex裡的相對位置寫入
    寫完後再讀取回傳給電腦
    但電腦接收指有接收前面一點點
    MCU回傳16bytes
    但電腦只有接收到4bytes
    我要發送四次才能夠有正常的動作
    請問這是什麼樣的問題呢?
    請各位前輩指導謝謝

    2019年1月29日 上午 03:14

所有回覆