View Single Post
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