小弟现在急需做一个串口通讯的项目 请大家帮帮忙我现在要向一个显示屏输出字符 并且还要接收串口回来的数据 请问在vs2005中应怎么使用 Serialport 另求QQ群

解决方案 »

  1.   

    这位兄弟,下面的是VB.Net编写的SerialPort通信程序,请参考一下。
    Public Class Form1    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Try
                If Button1.Text.StartsWith("Open") Then
                    Button1.Text = "Close"
                    SP1.NewLine = vbCrLf
                    SP1.Open()
                    Button1.ForeColor = Color.Red
                Else
                    Button1.Text = "Open"
                    SP1.Close()
                    Button1.ForeColor = Color.Green
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message, Text)
            End Try
        End Sub    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            txtRead.Clear()
            Try
                SP1.WriteLine(txtSend.Text.Trim)
            Catch ex As Exception
                MessageBox.Show(ex.Message, Text)
            End Try
        End Sub
        Private Delegate Sub SetText(ByVal strText As String)
        Private Sub SP1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SP1.DataReceived
            Try
                'txtRead.Text = String.Concat(txtRead.Text, SP1.ReadExisting)
                Call ReadText(SP1.ReadExisting)
            Catch ex As Exception
                MessageBox.Show(ex.Message, Text)
            End Try
        End Sub
        Private Sub ReadText(ByVal txt As String)
            If txtRead.InvokeRequired Then
                Me.Invoke(New SetText(AddressOf ReadText), txt)
            Else
                txtRead.Text = String.Concat(txtRead.Text, txt)
            End If
        End Sub
    End Class
      

  2.   

    DataReceived事件过程用来接收来自串口的数据
      

  3.   

    如果你装了MSDN2005就拖一个 Serialport 控件到然后按F1就很清楚了
    主要是如下几个属性和事件搞清楚就可以了
    1,DataReceived事件接受数据,
    2,ReceivedBytesThreshold是设置接收到几个字符来触发DataReceived事件
    3,read()来读缓冲区的数据,READ有很多种读的方式(详细看MSDN)
    4,Write()用来往发送缓冲区写数据,Write也有很多中方式(详细看MSDN)
      

  4.   

    写已经搞定了 但是读不出来  我是这么写的byte[] a=new byte[50];
    Serialport1.read(a,0,50);
    string b=a.tostring();
    textbox1.text=b; 这里出错 提示线程间操作无效;从不是创建控件"textbox1"的线程访问它
      

  5.   

    byte[] by = new byte[128];
                        
                        if (!sp.IsOpen)
                        {
                            sp.Open();
                        }                    Thread.Sleep(200);                    sp.Read(by, 0, by.Length);                    string instring = Encoding.ASCII.GetString(by);                    this.textBox3.Text = instring;
      

  6.   

    form constrol function add :
     Control.CheckForIllegalCrossThreadCalls = false;
    com read (.net 2.0 SerialPort)
     public string WriteCommand(string sCommand)
            {
                string sb = string.Empty;
                try
                {
                    ss_port.DiscardInBuffer();
                    ss_port.Write(sCommand);
                    Thread.Sleep(1500);
                    _ReadBuffer = new byte[ss_port.BytesToRead];
                    if (_ReadBuffer.Length > 0)
                        ss_port.Read(_ReadBuffer, 0, _ReadBuffer.Length);
                    else
                    {
                        nReadCount++;
                    }                if (nReadCount == 3)
                    {
                        nReadCount = 0;
                        throw new Exception("设置不正确或没有联接设备!");
                    }
                    sb = Encoding.ASCII.GetString(_ReadBuffer);
                }
                catch (Exception ex)
                {
                    throw new Exception("从设备获取数据失败!\r\n错误信息:" + ex.Message);
                }
                return sb;
            }
      

  7.   

    #region Construct
            public frmMain()构造函数
            {
                this.Hide();
                this.Enabled = false;
                InitializeComponent();
                nfOpw.Visible = false;
                Control.CheckForIllegalCrossThreadCalls = false;
            }
            #endregion