[help] Làm sao tách dữ liệu được gữi lên PC

Carue

Trứng gà
Bà con cô bác chỉ cho em cách tách dữ lieu được gửi lên WinFrom với, em it não quá nên làm nhiều cách rồi mà không có kết quả. Cụ thể là chỉ cho em cách tách 2 chữ "Hello" và chữ "Word" ra 2 textbox... như hình bên dưới (pót ảnh không được!)


Đây là code từ MCU:
Code:
#include "msp430g2553.h"
#include "init_io_port.c"
#include "stdio.h"
#include "struct_bit.c"
// ================= Gia tri tra ve khong ======================================
void Send_Char(unsigned char chr)
{
    while (!(IFG2&UCA0TXIFG));        // USCI_A0 TX buffer ready?
      UCA0TXBUF = chr;                    // load gia tri vao UCA0TXBUF
 
}
 
// ================ Ham Print chuoi ky tu tu msp430 gui len Terminal ===========
// ================== Gia tri tra ve: khong ====================================
void Print_UART(unsigned char *ch){
    unsigned char i = 0;
    while(ch[i] != '\0'){
            Send_Char(ch[i]);
            i++;
      }
}
unsigned int m;
 
void main( void )
{
  // Stop watchdog timer to prevent time out reset
  WDTCTL = WDTPW + WDTHOLD;
 
  BCSCTL1 = CALBC1_1MHZ;
  DCOCTL = CALDCO_1MHZ;
 
  init_UART();
  __delay_cycles(10000);
__bis_SR_register(GIE);
 
  while(1){
  Print_UART("Hello Word");
  __delay_cycles(100000);
  }
 
}
 
 
//================ Day la ngat nhan ============================================
#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR(void)
{
  while (!(IFG2&UCA0TXIFG));
__bic_SR_register_on_exit(CPUOFF+GIE);
}
Code này em làm theo đúng định dạng của diễn dàn mình luôn cho mọi người dễ can thiệp!!
Code:
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 TX2010
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
 
        // Quet lấy tất cả cac cong COM tren may tinh
        int intlen = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            string[] ports = SerialPort.GetPortNames();
            if (intlen != ports.Length)
            {
                intlen = ports.Length;
                cbb_Select_com.Items.Clear();
                for (int j = 0; j < intlen; j++)
                {
                    cbb_Select_com.Items.Add(ports[j]);
                }
            }
 
        }
 
 
        // Xu ly doi tuong cho Button Connect
        private void PbConnect_Click(object sender, EventArgs e)
        {
            if (LbStatus.Text == "Disconnect")
            {
                Com.PortName = cbb_Select_com.Text;  // chon cong com muon ket noi
                //Com.PortName = "COM11";  // lay truc tiep cong com muon ket noi
                Com.Open();
                LbStatus.Text = "Connect";
                bt_Connect.Text = "Disconnect";
                LbStatus.ForeColor = Color.Red;
            }
            else
            {
                Com.Close();
                LbStatus.Text = "Disconnect";
                bt_Connect.Text = "Connect";
                LbStatus.ForeColor = Color.Blue;
            }
        }
 
        private void  LbStatus_Click(object sender, EventArgs e)
        {
 
        }
 
 
 
 
 
 
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
 
        }
 
        private void bt_Send_Data_Click(object sender, EventArgs e)
        {
            string s;
            if (LbStatus.Text == "Connect")
            {
                s = txt_Receive_Data0.Text;
                txt_Receive_Data0.Text = s;
                Com.WriteLine(s);
            }
        }
 
        private void OnCom(object sender, SerialDataReceivedEventArgs e)
        {
            string s;
            s = Com.ReadExisting();
            Display(s);
   
        }
 
// chuong trinh doc du lieu  trong buff
      private delegate void DlDisplay(string s);
      private void Display (string s)
    {
 
      if (txt_Receive_Data0.InvokeRequired)
            {
                DlDisplay sd = new DlDisplay(Display);
                txt_Receive_Data0.Invoke(sd, new object[]{s});
 
            }
            else
            {
                txt_Receive_Data0.Text = s;
            }
 
    }
 
 
      // Click vào để thoát khỏi chương trình
      private void bt_thoat_Click(object sender, EventArgs e)
      {
          DialogResult result1 = MessageBox.Show("Bạn có thật sự muốn thoát?", "", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
          if (result1 == DialogResult.Yes)
          {
              Close();
          }
      }
 
      private void txt_Receive_Data1_TextChanged(object sender, EventArgs e)
      {
 
      }
 
 
      private void Form1_Load(object sender, EventArgs e)
      {
 
      }
 
      private void cbb_Select_com_SelectedIndexChanged(object sender, EventArgs e)
      {
 
      }
 
      private void txt_Receive_Data0_TextChanged(object sender, EventArgs e)
      {
 
      }
 
 
    }
}
Còn đây là toàn bộ projet của em nó: https://www.dropbox.com/s/gv11837y5dp14ay/Trruen nhan UART.rar
:-s
 

nhokviet

Trứng gà
Link hình của bạn đây: http://upanh.vn88.com/images/2013/05/22/UntitledL9rb.png


Có đoạn code ở site này bạn xem tạm nhen. http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.datareceived.aspx

Fix 1 tí xíu:
Code ở MCU khi gửi chuỗi nên kèm \r\n (Kết thúc 1 dòng) để bên winform đọc cho nó chuẩn hơn

Code Winfrom theo ở link trên sửa dòng
Code:
        string indata = sp.ReadExisting();
Thành
Code:
        string indata = sp.ReadLine();
Sau khi nhận được data là "Hello World" muốn cắt ra từng chhữ thì
Code:
        string[] splitData = indata.Split(' ');
 

Carue

Trứng gà
Link hình của bạn đây: http://upanh.vn88.com/images/2013/05/22/UntitledL9rb.png


Có đoạn code ở site này bạn xem tạm nhen. http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.datareceived.aspx

Fix 1 tí xíu:
Code ở MCU khi gửi chuỗi nên kèm \r\n (Kết thúc 1 dòng) để bên winform đọc cho nó chuẩn hơn

Code Winfrom theo ở link trên sửa dòng
Code:
        string indata = sp.ReadExisting();
Thành
Code:
        string indata = sp.ReadLine();
Sau khi nhận được data là "Hello World" muốn cắt ra từng chhữ thì
Code:
        string[] splitData = indata.Split(' ');
Hi, sao khi nhấn nút 'like' xong xuống sửa lại chạy luôn định lên làm phát nữa mà không được:D, mình cảm ơn nhiều vì thực tình là chẳng biết gì về C# cũng như Winfrom... Thank!
 

Carue

Trứng gà
Ah, thí dụ như trong khung truyền mình nhồi thêm các ký tự đặc biệt như 'Start of Text' và 'End of Text' để định khung cho dữ liệu ->> sau đó gửi lên PC hiển thị ra GUI thì chúng ta làm cách nào?? Cụ thể là làm sao có thể phát hiện được ký tự bắt đầu là 'Star of Text' và loại bỏ nó??? .... ai biết chỉ mình với vì mình cũng thấy mọi người gợi ý cách định khung như vậy mà không hiểu, cảm ơn rất nhiều!!!":*"":*"":*"":*"
 
Top