Cộng đồng kỹ thuật điện tử Việt Nam Nơi ý tưởng được cấp nguồn.

Thử tìm PIC16F877A dsPIC nguồn xung pin mặt trời

Linh kiện · RP Store

R&P phân phối chính thức Microchip, ROHM, Epson, Nichicon, Taejin…

Gửi BOM nhận báo giá

Lập trình C# giao tiếp vi điều khiển xuất dữ liệu ra Excel

Thu gọn
X
 
  • Giờ
  • Hiển thị
Xóa tất cả
bài mới
  • chiefpro
    Thành viên mới
    • Aug 2011
    • 1

    #1

    Lập trình C# giao tiếp vi điều khiển xuất dữ liệu ra Excel

    Em có viết chương trình C# giao tiep với vi điều khiển với mục đích là những dữ liệu từ vi điều khiển gửi lên sẽ được lưu vào mảng để khi ngắt kết nối với vi điều khiển và sẽ nhấn nút để xuất dữ liệu trong mảng ra file excel. Nhưng code em bị lỗi chỗ khai báo mảng chứa dữ liệu thu được từ ngắt serialport. Lỗi là "Field 'hienthi.Form1.a' is never assigned to, and will always have its default value null". Em mới chỉ biết sơ về C# nên không biết sữa như thế nào, mong mấy bác giúp em.
    Đây là đoạn code:
    namespace hienthi
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    }
    int intlen = 0; // luu gia tri so COM ket noi vao may tinh

    private void timer1_Tick(object sender, EventArgs e)
    {
    string[] ports = SerialPort.GetPortNames(); // lay tat ca COM noi vao PC dua vao mang port
    if(intlen != ports.Length)
    {
    intlen = ports.Length;
    cbComList.Items.Clear();
    for(int j = 0; j < intlen; j++)
    {
    cbComList.Items.Add(ports[j]);
    }
    }
    }

    private void btConnect_Click(object sender, EventArgs e)
    {
    if (cbComList.Text == " ")
    {
    MessageBox.Show(" Vui lòng chon cong COM ", "Thong bao", MessageBoxButtons.OK);
    return;
    }

    if (serialPort.IsOpen)
    {
    serialPort.Close();
    btConnect.Text = "Connect";
    }
    else
    {
    try
    {
    serialPort.PortName = cbComList.Text;
    serialPort.Open();
    btConnect.Text = "Dissconnect";
    }
    catch
    {
    MessageBox.Show(" khong the mo cong " + serialPort.PortName, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    }
    }

    int i = 0;
    string[] a;

    private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
    string s;
    s = serialPort.ReadExisting();
    s = a[i];
    i++;


    Display(s);

    }
    private delegate void DlDisplay(string s);
    private void Display(string s)
    {
    if (tbhienthi.InvokeRequired)
    {
    DlDisplay sd = new DlDisplay(Display);
    tbhienthi.Invoke(sd, new object[] { s });
    }
    else
    {
    if (s.IndexOf('E') != -1 && s.LastIndexOf('F') != -1)
    {
    tbhienthi.Text = s.Substring(2);
    }
    else if (s.IndexOf('G') != -1 && s.LastIndexOf('H') != -1)
    {
    tbhienthi2.Text = s.Substring(2);
    }
    }

    }

    private void btOut_Click(object sender, EventArgs e)
    {
    Microsoft.Office.Interop.Excel.Application xla = new Microsoft.Office.Interop.Excel.Application();
    Workbook wb = xla.Workbooks.Add(XlSheetType.xlWorksheet);
    Worksheet ws = (Worksheet)xla.ActiveSheet;

    for (int j = 2; j <= 10; j++)
    {
    xla.Visible = false;
    ws.Cells[1, 1] = "Thong so";
    ws.Cells[j, 1] = a[j-2];
    }
    }
    }
    }
    Sửa lần cuối bởi chiefpro; 30-09-2014, 23:01.
  • datdien
    Thành viên mới
    • Jul 2008
    • 7

    #2
    int i = 0;
    string[] a;
    private void serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
    string s;
    s = serialPort.ReadExisting();
    s = a[i];
    i++;
    Có vẻ như bạn đang nhầm giữa việc gán giá trị cho mảng a và biến s.
    Theo mình phải là sửa code đoạn này thành: "a[i] = s;"

    Bình luận

    Về tác giả

    Thu gọn

    chiefpro Tìm hiểu thêm về chiefpro

    Bài viết mới nhất

    Thu gọn

    Đang tải...