RP Store Tìm linh kiện theo mã, gửi BOM nhận báo giá Gửi BOM

Giao tiếp VC# 2010 với PIC16F887

Collapse
X
 
  • Giờ
  • Show
Clear All
new posts
  • boylojthoj
    Nhập môn đệ tử
    • Sep 2011
    • 3

    #1

    Giao tiếp VC# 2010 với PIC16F887

    Mình đang làm đề tài giao tiếp vi điều khiển với PC bằng VC# 2010. Tình hình là đã gửi và nhận dữ liệu được rồi, nhưng ở phần xử lý dữ liệu mình còn kém, mong các bạn trên 4rum giúp đỡ mình.
    Vấn đề như sau :
    1/ Mình gửi 1 dãy số từ 0-255 từ PIC16f887 lên VC# 2010, nhưng khi lên VC# 2010 thì lại hiển thị ra mã ACII, vậy làm sao để chuyển chúng thành dạng số để xử lý.
    2/ Mình cũng muốn gửi 1 vài số từ VC# 2010 xuống PIC16f887 để xử lý cơ cấu chấp hành, vậy làm sao để gửi dữ liệu dạng số. Nếu không gửi được dữ liệu số thì ở PIC16f887 mình phải xử lý thế nào để chuyển dữ liệu thành số.

    Hình ảnh giao diện



    Code của VC# 2010:
    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;
    using System.IO.Ports;
    using System.Xml;
     
    namespace testrs232
    {
        public partial class Form1 : Form
        {
            SerialPort P = new SerialPort(); //khai bao 1 object serial port moi
            string InputData = string.Empty; //khai bao string buff de hien thi du lieu
            delegate void SetTextCallback(string text); //khai bao delagate SetTextCallback voi tham so string
            public Form1()
            {
                InitializeComponent();
                //cai dat cac thong so cho COM
                //mang string ports de luu tat ca cac cong COM co tren may
                string[] ports = SerialPort.GetPortNames();
                //them tat ca cac COM tren may vao cbCom
                cbCom.Items.AddRange(ports);
                P.ReadTimeout = 1000;
                //khai bao ham delagate bang phuong thuc DataReceived cua Object SerialPort
                //khi co su kien nhan du lieu se nhay den phuong thuc DataReceived
                P.DataReceived += new SerialDataReceivedEventHandler(DataReceive);
                //cai dat baud rate
                string[] BaudRate = { "1200", "2400", "4800", "9600", "19200", "38400", "57600", "115200" };
                cbRate.Items.AddRange(BaudRate);
                //cai dat databits
                string[] DataBits={"6","7","8"};
                cbBits.Items.AddRange(DataBits);
                //cai dat parity
                string[] Parity = { "None", "Odd", "Even" };
                cbParity.Items.AddRange(Parity);
                //cai dat stop bit
                string[] Stopbit = { "1", "1.5", "2" };
                cbBit.Items.AddRange(Stopbit);
     
            }
     
          private void DataReceive(object obj, SerialDataReceivedEventArgs e)
                    {
                  string InputData=P.ReadExisting();
                  //try { InputData = P.ReadTo("\r"); }
                  //catch {return;}
                  P.DiscardInBuffer();
                        if (InputData != String.Empty)
                        {
                        // txtIn.Text = InputData; // Ko dùng đc như thế này vì khácthreads .
                        SetText(InputData); // Chính vì vậy phải sử dụng ủy quyền tạiđây. Gọi delegate đã khai báo trước đó.
                        }
                    }
            private void SetText(string text)
                    {
                            if (this.txtkq.InvokeRequired)
                            {
                            SetTextCallback d = new SetTextCallback(SetText); // khởi tạo 1 delegate mới gọi đến SetText
                            this.Invoke(d, new object[] { text}); 
                            }
                            else this.txtkq.Text += text;
                    }
     
     
          private void btGui_Click(object sender, EventArgs e)
              {
                          if (P.IsOpen)
                {
                    if (txtSend.Text == "") MessageBox.Show("Chưa có dữ liệu!", "Thông Báo");
                    else
                    {
                        P.Write(txtSend.Text);
                     
                    }
                }
                else MessageBox.Show("COM chưa mở.", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtSend.Clear();
                }
     
            private void Form1_Load(object sender, EventArgs e)
            {
                cbCom.SelectedIndex = 0; // chọn COM được tìm thấy đầu tiên
                cbRate.SelectedIndex = 3; // 9600
                cbBits.SelectedIndex = 2; // 8
                cbParity.SelectedIndex = 0; // None
                cbBit.SelectedIndex = 0; // None
                // Hiện thị Status cho Pro tí
                status.Text = "Hãy chọn 1 cổng COM để kết nối.";
            }
     
            private void btKetnoi_Click(object sender, EventArgs e)
            {
                      try
                    {
                    P.Open();
                    btNgat.Enabled = true;
                    btKetnoi.Enabled = false;
                    // Hiện thị Status
                    status.Text = "Đang kết nối với cổng " + cbCom.SelectedItem.ToString();
                    }
                    catch (Exception aa)
                    {
                    MessageBox.Show("Không kết nối được.","Thử lại",MessageBoxButtons.OK,MessageBoxIcon.Error);
                    }
            }
     
            private void btNgat_Click(object sender, EventArgs e)
            {
                P.Close();
                btKetnoi.Enabled = true;
                btNgat.Enabled = false;
                // Hiện thị Status
                status.Text = "Đã ngắt kết nối";
            }
     
            private void btThoat_Click(object sender, EventArgs e)
            {
                DialogResult kq = MessageBox.Show("Bạn thực sự muốn thoát", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (kq == DialogResult.Yes)
                {
                MessageBox.Show("Chúc bạn thành công","CĐCĐT10A");
                this.Close();
                }
            }
     
            private void btXoa_Click(object sender, EventArgs e)
            {
                txtkq.Text = "";
                txtSend.Text = "";
            }
     
            private void tácGiảToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Form2 frm = new Form2();
                frm.ShowDialog();
            }
     
            private void cbCom_SelectedIndexChanged_1(object sender, EventArgs e)
            {
                if (P.IsOpen)
                {
                    P.Close();
                }
                P.PortName = cbCom.SelectedItem.ToString(); //gan portname bang COM da chon
     
            }
     
            private void cbRate_SelectedIndexChanged_1(object sender, EventArgs e)
            {
                if (P.IsOpen)
                {
                    P.Close();
                }
                P.BaudRate = Convert.ToInt32(cbRate.Text);
            }
     
            private void cbBits_SelectedIndexChanged_1(object sender, EventArgs e)
            {
                if (P.IsOpen)
                {
                    P.Close();
                }
                P.DataBits = Convert.ToInt32(cbBits.Text);
            }
     
            private void cbParity_SelectedIndexChanged_1(object sender, EventArgs e)
            {
                if (P.IsOpen)
                {
                    P.Close();
                }
                switch (cbParity.SelectedItem.ToString())
                {
                    case "Odd":
                        P.Parity = Parity.Odd;
                        break;
                    case "None":
                        P.Parity = Parity.None;
                        break;
                    case "Even":
                        P.Parity = Parity.Even;
                        break;
                }
            }
     
            private void cbBit_SelectedIndexChanged_1(object sender, EventArgs e)
            {
                if (P.IsOpen)
                {
                    P.Close();
                }
                switch (cbBit.SelectedItem.ToString())
                {
                    case "1":
                        P.StopBits = StopBits.One;
                        break;
                    case "1.5":
                        P.StopBits = StopBits.OnePointFive;
                        break;
                    case "2":
                        P.StopBits = StopBits.Two;
                        break;
                }
            }
     
     
       
          }
        }
    Mong mọi người giúp đỡ !!!
  • boylojthoj
    Nhập môn đệ tử
    • Sep 2011
    • 3

    #2
    Không cao thủ nào giúp mình sao ???

    Comment

    • congpho
      Nhập môn đệ tử
      • Apr 2014
      • 1

      #3
      trong phần P.Write(txtSend.Text);
      phải thêm cái này xuống dưới nữa
      P.Write(new byte[] { 13 }, 0, 1);
      ampli california

      Comment

      Working...