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.Ports;namespace test_serialport
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }        private void button1_Click(object sender, EventArgs e)
        {
            SerialPort1.Portname = "COM2";
            SerialPort1.BaudRate = "9600";        }
    }
}

解决方案 »

  1.   

    serialPort1.BaudRate = 9600;
    BaudRate 是 int型
      

  2.   

    谢谢楼上的,可是int的这样用不行吗
      

  3.   

    int型怎么能附字符串的值那?“9600”这表示字符串string类型的!
      

  4.   

    谢谢,我刚用VS中的C#,查了一些文章,说可以有三种方法使用SERIALPORT, 一个就是把SERIALPORT的控件托入窗体,可是为什么只能放在窗体下面,而且改了名也不行。
      

  5.   

    spPTZ.BaudRate = 9600;
    spPTZ.DataBits = 8;
    spPTZ.PortName = "Com1";
    spPTZ.ReadTimeout = 2000;
    spPTZ.StopBits = System.IO.Ports.StopBits.One;没有问题啊。
      

  6.   

    谢谢8楼。
    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.Ports;
    namespace WindowsFormsApplication2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                spPTZ.BaudRate = 9600;
                spPTZ.DataBits = 8;
                spPTZ.PortName = "Com1";
                spPTZ.ReadTimeout = 2000;
                spPTZ.StopBits = System.IO.Ports.StopBits.One;
            }        private void textBox1_TextChanged(object sender, EventArgs e)
            {        }
        }
    }可是这样提示“当前上下文不存在名称spPTZ”