PIC Vietnam

Go Back   PIC Vietnam > Truyền thông > Giao tiếp cổng COM và LPT

Tài trợ cho PIC Vietnam
Trang chủ Đăng Kí Hỏi/Ðáp Thành Viên Lịch Bài Trong Ngày Vi điều khiển

Giao tiếp cổng COM và LPT RS232, RS485 và LPT là những giao tiếp cơ bản và kinh điển khi mới học về vi điều khiển...

 
 
Ðiều Chỉnh Xếp Bài
Prev Previous Post   Next Post Next
Old 23-04-2008, 10:21 AM   #9
baybalinh
Đệ tử 1 túi
 
Tham gia ngày: Dec 2007
Bài gửi: 18
:
Các bác xem code của em bị lỗi gì với

Em có làm 1 ví dụ từ nguồn http://www.codeproject.com/KB/system...hreadSafe.aspx giao tiếp RS232 dùng C#.
Nó báo lỗi thế này:
Error 1 The name 'port' does not exist in the current context C:\Documents and Settings\KkK\My Documents\Visual Studio 2005\Projects\vd codeproject Csharp\vd codeproject Csharp\Form1.cs

Em gửi kèm project em làm. Các bác xem hộ em sai ở đâu với ạ.
Em cảm ơn các bác nhiều.

Đây là code của em
Code:
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;


namespace RS232
{
    public partial class fclsRS232Tester : Form
    {
        string InputData = String.Empty;
        // This delegate enables asynchronous calls for setting

        // the text property on a TextBox control:

        delegate void SetTextCallback(string text);
        public fclsRS232Tester()
        {
            InitializeComponent();
            // Nice methods to browse all available ports:

            string[] ports = SerialPort.GetPortNames();

            // Add all port names to the combo box:

            foreach (string port in ports)
            {
                cmbComSelect.Items.Add(port);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (port.IsOpen) port.WriteLine(txtOut.Text);
            else MessageBox.Show("Serial port is closed!",
                                 "RS232 tester",
                                 MessageBoxButtons.OK,
                                 MessageBoxIcon.Error);
            txtOut.Clear();
        }

        private void cmbComSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (port.IsOpen) port.Close();
            port.PortName = cmbComSelect.SelectedItem.ToString();
            stsStatus.Text = port.PortName + ": 9600,8N1";

            // try to open the selected port:

            try
            {
                port.Open();
            }
            // give a message, if the port is not available:

            catch
            {
                MessageBox.Show("Serial port " + port.PortName +
                   " cannot be opened!", "RS232 tester",
                   MessageBoxButtons.OK, MessageBoxIcon.Warning);
                cmbComSelect.SelectedText = "";
                stsStatus.Text = "Select serial port!";
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            txtIn.Clear();
        }

        private void txtIn_TextChanged(object sender, EventArgs e)
        {

        }

        private void stsStatus_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }
        private void port_DataReceived_1(object sender, SerialDataReceivedEventArgs e)
        {
            InputData = port.ReadExisting();
            if (InputData != String.Empty)
            {
                //             txtIn.Text = InputData;

                // because of different threads this

                // does not work properly !!


                SetText(InputData);
            }
        }
        private void SetText(string text)
        {
            // InvokeRequired required compares the thread ID of the

            // calling thread to the thread ID of the creating thread.

            // If these threads are different, it returns true.

            if (this.txtIn.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { text });
            }
            else this.txtIn.Text += text;
        }
    }
}
File Kèm Theo
File Type: rar vd codeproject Csharp.rar (19.0 KB, 222 lần tải)

thay đổi nội dung bởi: namqn, 24-04-2008 lúc 10:26 PM.
baybalinh vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn
 


Quyền Sử Dụng Ở Diễn Ðàn
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is Mở
Smilies đang Mở
[IMG] đang Mở
HTML đang Tắt

Chuyển đến


Múi giờ GMT. Hiện tại là 06:56 AM.


Được sáng lập bởi Đoàn Hiệp
Powered by vBulletin®
Page copy protected against web site content infringement by Copyscape
Copyright © PIC Vietnam