如题,串口通信方面是一点也不清楚。所以请大家多帮忙了

解决方案 »

  1.   

    忘了发邮件地址了,[email protected]希望各位帮忙了
      

  2.   

    在vb60里面用过:
    在窗体中添加名为MyComm的MSComm控件,并设置空间的属性,如下:.CommPort=2 '使用COM2
    .Setting="9600,e,7,1" '波特率9600,偶校验,7个数据位,1个停止位
    .InBufferSize=40 '设置MyComm接收缓冲区为40个字节
    .OutBufferSize=2 '设置MyComm发送缓冲区为2个字节
    .InputMode=comInputModeBinary
    '设置接收数据模式为二进制模式
    .InputLen=1 '设置一次从接收缓冲区读取字节数为1
    .Sthreshold=1 '设置一次从发送缓冲区读取字节数为1
    .PortOpen=True '打开通信口Private Sub MSComm1_OnComm()
    Dim strTemp As String
    Dim pos As Integer    Select Case MSComm1.CommEvent
            Case comEvCD   ' Change in the CD line.
            Case comEvCTS   ' Change in the CTS line.
            Case comEvDSR   ' Change in the DSR line.
            Case comEvRing   ' Change in the Ring Indicator.
            Case comEvReceive   ' Received RThreshold # of chars
                strTemp = MSComm1.Input
                
                strInputBuffer = strInputBuffer + strTemp
                Do
                    pos = InStr(1, strInputBuffer, msgTrail, vbBinaryCompare)
                    If pos > 0 Then
                        strTemp = Mid(strInputBuffer, 1, pos + 1)
                        strInputBuffer = Mid(strInputBuffer, pos + 2)
                        RaiseEvent DataArrived(strTemp)
                    End If
                Loop Until pos <= 0
            Case comEvSend   ' There are SThreshold number of characters in the transmit buffer.
            Case comEvEOF   ' An EOF charater was found in the input stream
        
            Case comEventBreak   ' A Break was received.
            Case comEventFrame   ' Framing Error
            Case comEventOverrun   ' Data Lost.
            Case comEventRxOver   ' Receive buffer overflow.
            Case comEventRxParity   ' Parity Error.
            Case comEventTxFull   ' Transmit buffer full.
            Case comEventDCB   ' Unexpected error retrieving DCB]
        End Select
    End SubPublic Function OpenComm(ByVal port As Integer, ByVal settings As String) As Integer
    On Error GoTo errorhandler
        If MSComm1.PortOpen = True Then
            MSComm1.PortOpen = False
        End If
        
        MSComm1.CommPort = port
        MSComm1.settings = settings
        MSComm1.RThreshold = 1
        MSComm1.PortOpen = True
        
        msgTrail = Chr(13) + Chr(10)
        OpenComm = 0
        Exit Function
    errorhandler:
        OpenComm = Err.Number
    End FunctionPublic Function CloseComm() As Integer
        If MSComm1.PortOpen = True Then
            MSComm1.PortOpen = False
        End If
    End FunctionPublic Function SendData(ByVal data As String) As Integer
        If MSComm1.PortOpen = True Then
            MSComm1.Output = data
            SendData = 0
        Else
            SendData = 1
        End If
    End FunctionPrivate Sub UserControl_Terminate()
        If MSComm1.PortOpen = True Then
            MSComm1.PortOpen = False
        End If
    End Sub 
      

  3.   

    Case comEvReceive   里就是接受到的数据处理,接收数据:MSComm1.Input
    发送数据就用:Function SendData(ByVal data As String)
      

  4.   

    try
    {
    com2.PortOpen =true;
    }
    catch(Exception ex)
    {
    MessageBox.Show("Port Open Error");
    return;
    }
    if(com2.PortOpen)
       com2.PortOpen =false;
    com2.Settings ="9600,n,8,1";
    com2.InputMode =MSCommLib.InputModeConstants.comInputModeText;
    com2.Output ="HELLO!";///发出的信息内容string inputStr = (string) com2.Input;///用来接收
      

  5.   

    我正在对一个读卡器进行操作,次读卡器内存入了一些卡号和时间,我想读出来。 
    下面是读卡器厂家给出的资料一部分: 
    ---------------------------------------------------------------------- 
    485通讯指令协议(开发说明和接口规格) 
    可以使用计算机(主机)对读卡器(分机)进行记录读取,时间设置和地址设置等。 
    ⑴通讯方法:232/485异步通讯,双绞线总线方式。 
    ⑵通讯波特率:9600  
    ⑶串口方式:1位开始位,8个数据位,一个停止位,无奇偶校验。 
    ⑷⑸通讯协议格式:。 
    ------------------------------------------------------ 
    |开始标志|分机地址|信息长度|命令和参数/丛机返回|校验| 
    ------------------------------------------------------ 
    [开始标志]:长度2个字节,主机给分机:AAH FFH; 
    分机给主机:BBH FFH 
    [分机地址]:长度1个字节,00H---FEH。  
    [广播地址]:FFH 
    [信息长度]:表示命令和参数的总字节数,不包括开始标志、地址和校验的长度。 
    [校    验]:长度1个字节,是开始标志、地址、信息长度、命令和参数中所有字节的异或值。 
    [命令和数据]: 
    ①读取刷卡记录命令 
    [功能]:从读卡器读取一条记录信息。 
    [命令]:A1H 
    读卡器返回:命令不能识别或校验出错(33H); 
    或无新刷卡纪录(77H); 
    或Flash中的一条读卡记录信息。 
    例如: 
    主机:AA FF 08(分机地址) 01(长度) A1 FD(校验) 
    AA FF 08 01 A1 FD 
    分机:BB FF 08(分机地址) 0B(长度) 00 22 34 55(卡号) 04 02 03 23 22 18 45(时间) 00  01  0B  00  00  00  00  0C  校验;红色字的返回是用来调试的,不用处理. 
    或者:BB FF 08 01 77(空记录) 校验; 
    或者:BB FF 08 01 33(出错)   校验 
    注意:卡号是4个字节的十六进制数,不能识别就是从机收到的上位机发来的命令不对或校验不对。 
    ------------------------------------------------------------------------- 
    就用C#使用MSComm控件,下面是我的程序: 
    private bool initChk(AxMSCommLib.AxMSComm MSComm, Byte i) 
          { 
             i = 1; 
             MSComm.CommPort = i;          
             MSComm.InputLen = 0;//接收缓冲区的大小 
             MSComm.Settings = "9600,n,8,1";//串口属性 
             MSComm.Handshaking = MSCommLib.HandshakeConstants.comNone; 
             MSComm.InputMode = MSCommLib.InputModeConstants.comInputModeBinary; 
             MSComm.RThreshold = 1;//每7个字节响应消息 
             MSComm.PortOpen = true;//打开通信串口 
             return true; 
          } 
          byte[] iNum = new byte[255]; 
          byte[] bJiaoYan = new byte[255]; 
          byte intPort = 1; 
          private string ComBuffer = "";       private void button1_Click(object sender, System.EventArgs e) 
          { 
             try 
             { 
                if(!MSComm.PortOpen) 
                initChk(MSComm , intPort); 
                MSComm.OutBufferCount = 0; 
                iNum[0] = 170; 
                MSComm.Output = iNum;//AA 
                bJiaoYan[0] = iNum[0]; 
                iNum[0] = 255;           
                bJiaoYan[0] =(byte)((Convert.ToInt32(bJiaoYan[0]))^(int)(Convert.ToInt32(iNum[0]))); 
                MSComm.Output = iNum;//FF 
                iNum[0] = 1; 
                bJiaoYan[0] =(byte)((Convert.ToInt32(bJiaoYan[0]))^(int)(Convert.ToInt32(iNum[0]))); 
                MSComm.Output = iNum;//1 
                iNum[0] = 1; 
                bJiaoYan[0] =(byte)((Convert.ToInt32(bJiaoYan[0]))^(int)(Convert.ToInt32(iNum[0]))); 
                MSComm.Output = iNum;//1 
                iNum[0] =161; 
                bJiaoYan[0] =(byte)((Convert.ToInt32(bJiaoYan[0]))^(int)(Convert.ToInt32(iNum[0]))); 
                MSComm.Output = iNum;//A1 
                MSComm.Output = bJiaoYan;//F4 
             } 
             catch(Exception ex) 
             {MessageBox.Show(ex.Message);} 
          }       private void MSComm_OnComm(object sender, System.EventArgs e) 
          { 
             string strIn="" ;   
             byte[] bytIn;   
             object objIn;   
             int i;  
             short aa = MSComm.CommEvent;      MSComm.InputMode=MSCommLib.InputModeConstants.comInputModeBinary;   
             objIn=MSComm.Input;    
             bytIn =(byte[])objIn;     
             for (i=0;i<=(bytIn.Length-1);i++ )   
             {   
    //            strIn ="\r\n"+ " "+bytIn[i].ToString("X");   
                strIn = bytIn[i].ToString("X"); 
             }   
             this.textBox1.Text = strIn; 
          }  
      

  6.   

    我做过一个用mscomm的,你看看把,用c#
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;
    using File;
    using System.Threading;namespace mscomm4
    {
    /// <summary>
    /// Form1 的摘要说明。
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
    private System.Windows.Forms.GroupBox groupBox1;
    private System.Windows.Forms.Label label1;
    private System.Windows.Forms.Label label2;
    private System.Windows.Forms.Label label3;
    private System.Windows.Forms.Label label4;
    private System.Windows.Forms.Label label5;
    private System.Windows.Forms.ComboBox Cbbox_Port;
    private System.Windows.Forms.ComboBox Cbbox_Bound;
    private System.Windows.Forms.ComboBox Cbbox_Databits;
    private System.Windows.Forms.ComboBox Cbbox_Parity;
    private System.Windows.Forms.ComboBox Cbbox_Stop;
    private System.Windows.Forms.GroupBox groupBox2;
    private System.Windows.Forms.GroupBox groupBox3;
    private System.Windows.Forms.TextBox Txtbox_Send;
    private System.Windows.Forms.Button Btn_Open;
    private System.Windows.Forms.Button Btn_Close;
    private System.Windows.Forms.Button Btn_Clear;
    private System.Windows.Forms.Button Btn_Send;
    private AxMSCommLib.AxMSComm axMSComm1;
    private System.Windows.Forms.TextBox Txtbox_Recev;
    private System.Windows.Forms.Button button1;
    private System.Windows.Forms.SaveFileDialog saveFileDialog1;
    private string fName;
    private bool stop=false;
    private bool open=false;
    private System.Windows.Forms.Button button2;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null; public Form1()
    {
    //
    // Windows 窗体设计器支持所必需的
    //
    InitializeComponent(); //
    // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
    //
    } /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
    if( disposing )
    {
    if (components != null) 
    {
    components.Dispose();
    }
    }
    base.Dispose( disposing );
    }
      

  7.   


    #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
    this.groupBox1 = new System.Windows.Forms.GroupBox();
    this.Cbbox_Stop = new System.Windows.Forms.ComboBox();
    this.Cbbox_Parity = new System.Windows.Forms.ComboBox();
    this.Cbbox_Databits = new System.Windows.Forms.ComboBox();
    this.Cbbox_Bound = new System.Windows.Forms.ComboBox();
    this.Cbbox_Port = new System.Windows.Forms.ComboBox();
    this.label5 = new System.Windows.Forms.Label();
    this.label4 = new System.Windows.Forms.Label();
    this.label3 = new System.Windows.Forms.Label();
    this.label2 = new System.Windows.Forms.Label();
    this.label1 = new System.Windows.Forms.Label();
    this.groupBox2 = new System.Windows.Forms.GroupBox();
    this.Txtbox_Recev = new System.Windows.Forms.TextBox();
    this.groupBox3 = new System.Windows.Forms.GroupBox();
    this.Txtbox_Send = new System.Windows.Forms.TextBox();
    this.Btn_Open = new System.Windows.Forms.Button();
    this.Btn_Close = new System.Windows.Forms.Button();
    this.Btn_Clear = new System.Windows.Forms.Button();
    this.Btn_Send = new System.Windows.Forms.Button();
    this.axMSComm1 = new AxMSCommLib.AxMSComm();
    this.button1 = new System.Windows.Forms.Button();
    this.saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
    this.button2 = new System.Windows.Forms.Button();
    this.groupBox1.SuspendLayout();
    this.groupBox2.SuspendLayout();
    this.groupBox3.SuspendLayout();
    ((System.ComponentModel.ISupportInitialize)(this.axMSComm1)).BeginInit();
    this.SuspendLayout();
    // 
    // groupBox1
    // 
    this.groupBox1.Controls.Add(this.Cbbox_Stop);
    this.groupBox1.Controls.Add(this.Cbbox_Parity);
    this.groupBox1.Controls.Add(this.Cbbox_Databits);
    this.groupBox1.Controls.Add(this.Cbbox_Bound);
    this.groupBox1.Controls.Add(this.Cbbox_Port);
    this.groupBox1.Controls.Add(this.label5);
    this.groupBox1.Controls.Add(this.label4);
    this.groupBox1.Controls.Add(this.label3);
    this.groupBox1.Controls.Add(this.label2);
    this.groupBox1.Controls.Add(this.label1);
    this.groupBox1.Location = new System.Drawing.Point(0, 0);
    this.groupBox1.Name = "groupBox1";
    this.groupBox1.Size = new System.Drawing.Size(176, 208);
    this.groupBox1.TabIndex = 0;
    this.groupBox1.TabStop = false;
    this.groupBox1.Text = "设置串口";
    // 
    // Cbbox_Stop
    // 
    this.Cbbox_Stop.Items.AddRange(new object[] {
    "1",
    "2"});
    this.Cbbox_Stop.Location = new System.Drawing.Point(104, 176);
    this.Cbbox_Stop.Name = "Cbbox_Stop";
    this.Cbbox_Stop.Size = new System.Drawing.Size(64, 20);
    this.Cbbox_Stop.TabIndex = 9;
    this.Cbbox_Stop.Text = "1";
    // 
    // Cbbox_Parity
    // 
    this.Cbbox_Parity.Items.AddRange(new object[] {
      "NONE",
      "ODD",
      "EVEN"});
    this.Cbbox_Parity.Location = new System.Drawing.Point(104, 136);
    this.Cbbox_Parity.Name = "Cbbox_Parity";
    this.Cbbox_Parity.Size = new System.Drawing.Size(64, 20);
    this.Cbbox_Parity.TabIndex = 8;
    this.Cbbox_Parity.Text = "NONE";
    // 
    // Cbbox_Databits
    // 
    this.Cbbox_Databits.Items.AddRange(new object[] {
    "8",
    "7",
    "6"});
    this.Cbbox_Databits.Location = new System.Drawing.Point(104, 96);
    this.Cbbox_Databits.Name = "Cbbox_Databits";
    this.Cbbox_Databits.Size = new System.Drawing.Size(64, 20);
    this.Cbbox_Databits.TabIndex = 7;
    this.Cbbox_Databits.Text = "8";
    // 
    // Cbbox_Bound
    // 
    this.Cbbox_Bound.Items.AddRange(new object[] {
     "300",
     "600",
     "1200",
     "2400",
     "4800",
     "9600",
     "19200",
     "38400",
     "43000"});
    this.Cbbox_Bound.Location = new System.Drawing.Point(104, 56);
    this.Cbbox_Bound.Name = "Cbbox_Bound";
    this.Cbbox_Bound.Size = new System.Drawing.Size(64, 20);
    this.Cbbox_Bound.TabIndex = 6;
    this.Cbbox_Bound.Text = "9600";
      

  8.   

    // 
    // Cbbox_Port
    // 
    this.Cbbox_Port.Location = new System.Drawing.Point(104, 16);
    this.Cbbox_Port.Name = "Cbbox_Port";
    this.Cbbox_Port.Size = new System.Drawing.Size(64, 20);
    this.Cbbox_Port.TabIndex = 5;
    // 
    // label5
    // 
    this.label5.Location = new System.Drawing.Point(8, 176);
    this.label5.Name = "label5";
    this.label5.TabIndex = 4;
    this.label5.Text = "停止位";
    // 
    // label4
    // 
    this.label4.Location = new System.Drawing.Point(8, 136);
    this.label4.Name = "label4";
    this.label4.TabIndex = 3;
    this.label4.Text = "校验";
    // 
    // label3
    // 
    this.label3.Location = new System.Drawing.Point(8, 96);
    this.label3.Name = "label3";
    this.label3.TabIndex = 2;
    this.label3.Text = "数据位";
    // 
    // label2
    // 
    this.label2.Location = new System.Drawing.Point(8, 56);
    this.label2.Name = "label2";
    this.label2.TabIndex = 1;
    this.label2.Text = "波特率";
    // 
    // label1
    // 
    this.label1.Location = new System.Drawing.Point(8, 17);
    this.label1.Name = "label1";
    this.label1.TabIndex = 0;
    this.label1.Text = "串口号";
    // 
    // groupBox2
    // 
    this.groupBox2.Controls.Add(this.Txtbox_Recev);
    this.groupBox2.Location = new System.Drawing.Point(184, 8);
    this.groupBox2.Name = "groupBox2";
    this.groupBox2.Size = new System.Drawing.Size(488, 256);
    this.groupBox2.TabIndex = 10;
    this.groupBox2.TabStop = false;
    this.groupBox2.Text = "接收区";
    // 
    // Txtbox_Recev
    // 
    this.Txtbox_Recev.Dock = System.Windows.Forms.DockStyle.Fill;
    this.Txtbox_Recev.Location = new System.Drawing.Point(3, 17);
    this.Txtbox_Recev.Multiline = true;
    this.Txtbox_Recev.Name = "Txtbox_Recev";
    this.Txtbox_Recev.ScrollBars = System.Windows.Forms.ScrollBars.Both;
    this.Txtbox_Recev.Size = new System.Drawing.Size(482, 236);
    this.Txtbox_Recev.TabIndex = 11;
    this.Txtbox_Recev.Text = "";
    // 
    // groupBox3
    // 
    this.groupBox3.Controls.Add(this.Txtbox_Send);
    this.groupBox3.Location = new System.Drawing.Point(184, 264);
    this.groupBox3.Name = "groupBox3";
    this.groupBox3.Size = new System.Drawing.Size(488, 144);
    this.groupBox3.TabIndex = 11;
    this.groupBox3.TabStop = false;
    this.groupBox3.Text = "发送区";
    // 
    // Txtbox_Send
    // 
    this.Txtbox_Send.Dock = System.Windows.Forms.DockStyle.Fill;
    this.Txtbox_Send.Location = new System.Drawing.Point(3, 17);
    this.Txtbox_Send.Multiline = true;
    this.Txtbox_Send.Name = "Txtbox_Send";
    this.Txtbox_Send.ScrollBars = System.Windows.Forms.ScrollBars.Both;
    this.Txtbox_Send.Size = new System.Drawing.Size(482, 124);
    this.Txtbox_Send.TabIndex = 12;
    this.Txtbox_Send.Text = "";
    // 
    // Btn_Open
    // 
    this.Btn_Open.Location = new System.Drawing.Point(56, 216);
    this.Btn_Open.Name = "Btn_Open";
    this.Btn_Open.TabIndex = 12;
    this.Btn_Open.Text = "打开串口";
    this.Btn_Open.Click += new System.EventHandler(this.Btn_Open_Click);
    // 
    // Btn_Close
    // 
    this.Btn_Close.Enabled = false;
    this.Btn_Close.Location = new System.Drawing.Point(56, 248);
    this.Btn_Close.Name = "Btn_Close";
    this.Btn_Close.TabIndex = 13;
    this.Btn_Close.Text = "关闭串口";
    this.Btn_Close.Click += new System.EventHandler(this.Btn_Close_Click);
    // 
    // Btn_Clear
    // 
    this.Btn_Clear.Location = new System.Drawing.Point(56, 312);
    this.Btn_Clear.Name = "Btn_Clear";
    this.Btn_Clear.TabIndex = 14;
    this.Btn_Clear.Text = "清空";
    this.Btn_Clear.Click += new System.EventHandler(this.Btn_Clear_Click);
    // 
    // Btn_Send
    // 
    this.Btn_Send.Location = new System.Drawing.Point(56, 376);
    this.Btn_Send.Name = "Btn_Send";
    this.Btn_Send.TabIndex = 15;
    this.Btn_Send.Text = "发送";
    this.Btn_Send.Click += new System.EventHandler(this.Btn_Send_Click);
    // 
    // axMSComm1
    // 
    this.axMSComm1.Enabled = true;
    this.axMSComm1.Location = new System.Drawing.Point(8, 240);
    this.axMSComm1.Name = "axMSComm1";
    this.axMSComm1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.GetObject("axMSComm1.OcxState")));
    this.axMSComm1.Size = new System.Drawing.Size(38, 38);
    this.axMSComm1.TabIndex = 16;
    this.axMSComm1.OnComm += new System.EventHandler(this.axMSComm1_OnComm);
    // 
    // button1
    // 
    this.button1.Location = new System.Drawing.Point(56, 344);
    this.button1.Name = "button1";
    this.button1.TabIndex = 17;
    this.button1.Text = "保存";
    this.button1.Click += new System.EventHandler(this.button1_Click);
    // 
    // button2
    // 
    this.button2.Location = new System.Drawing.Point(56, 280);
    this.button2.Name = "button2";
    this.button2.TabIndex = 10;
    this.button2.Text = "刷新";
    this.button2.Click += new System.EventHandler(this.button2_Click);
    // 
    // Form1
    // 
    this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
    this.ClientSize = new System.Drawing.Size(680, 413);
    this.Controls.Add(this.button1);
    this.Controls.Add(this.axMSComm1);
    this.Controls.Add(this.Btn_Send);
    this.Controls.Add(this.Btn_Clear);
    this.Controls.Add(this.Btn_Close);
    this.Controls.Add(this.Btn_Open);
    this.Controls.Add(this.groupBox3);
    this.Controls.Add(this.groupBox1);
    this.Controls.Add(this.groupBox2);
    this.Controls.Add(this.button2);
    this.Name = "Form1";
    this.Text = "Form1";
    this.Load += new System.EventHandler(this.Form1_Load);
    this.groupBox1.ResumeLayout(false);
    this.groupBox2.ResumeLayout(false);
    this.groupBox3.ResumeLayout(false);
    ((System.ComponentModel.ISupportInitialize)(this.axMSComm1)).EndInit();
    this.ResumeLayout(false); }
    #endregion
      

  9.   

    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main() 
    {
    Application.Run(new Form1());
    }
    private void iniComm()
    {
    axMSComm1.CommPort=pport(Cbbox_Port.SelectedItem.ToString());
    if (axMSComm1.PortOpen) axMSComm1.PortOpen = false;
    axMSComm1.Settings=Cbbox_Bound.SelectedItem.ToString()+","+(Cbbox_Parity.SelectedItem.ToString()).Substring(0,1)+","+Cbbox_Databits.SelectedItem.ToString()+","+Cbbox_Stop.SelectedItem.ToString();
    axMSComm1.RThreshold = 1; 
    axMSComm1.DTREnable = true;
    axMSComm1.Handshaking = MSCommLib.HandshakeConstants.comNone;
    axMSComm1.InputMode = MSCommLib.InputModeConstants.comInputModeText;
    axMSComm1.InputLen = 0;
    axMSComm1.NullDiscard = false; 
    axMSComm1.OnComm += new System.EventHandler(this.axMSComm1_OnComm);
    try
    {
    axMSComm1.PortOpen = true;
    open=true;
    }
    catch
    {
    MessageBox.Show("串口被占用或者不存在");
    open=false;
    }
    }
    private short pport(string a)
    {
    short b;
    switch(a)
    {
    case "COM1":
    b=1;break;
        case "COM2":
    b=2;break;
    case "COM3":
    b=3;break;
    case "COM4":
    b=4;break;
    default: b=1;break;
    }
    return b;
    } private void Btn_Open_Click(object sender, System.EventArgs e)
    {
    iniComm();
    if(open==true)
    {
    setopen();
    Btn_Open.Enabled=false;
    Btn_Close.Enabled=true;
    }
    else return;
    } private void axMSComm1_OnComm(object sender, System.EventArgs e)
    {
    if(!stop)
    {
    Thread b=new Thread(new ThreadStart(ProcessCom));
    b.IsBackground=true;
    b.Start();
    }


    }
    private void ProcessCom()
    {
    if (axMSComm1.InBufferCount > 0) ProcessComData((string) axMSComm1.Input);
    }
    private void ProcessComData(string input)
    {
    // Send incoming data to a Rich Text Box
    Txtbox_Recev.AppendText(input);
    }
    private void ProcessOutData(string output)
    {

    axMSComm1.Output=output;
    } private void Btn_Close_Click(object sender, System.EventArgs e)
    {
    axMSComm1.PortOpen=false;
    Btn_Open.Enabled=true;
    Btn_Close.Enabled=false;
    setclose();
    } private void button1_Click(object sender, System.EventArgs e)
    {
    SaveMyFile();
    Txtbox_Recev.Clear();
    }
      

  10.   

    public void SaveMyFile()
    {
    saveFileDialog1.Filter="文本文件(*.txt)|*.txt|所有文件|*.*";
    saveFileDialog1.RestoreDirectory=true;
    if(saveFileDialog1.ShowDialog()==DialogResult.OK)
    {
    fName=saveFileDialog1.FileName;
    File.File fSaveAs=new File.File(fName);
    fSaveAs.WriteFile(Txtbox_Recev.Text);
    }
    } private void Btn_Send_Click(object sender, System.EventArgs e)
    {
    stop=true;
    if(stop)
    {
    Thread a=new Thread(new ThreadStart(ProcessOut));
    a.IsBackground=true;
    a.Start();

    } //ProcessOutData(Txtbox_Send.Text);
    stop=false;
    }
    private void ProcessOut()
    {
    ProcessOutData(Txtbox_Send.Text);
    }
    private void setopen()
    {
    Cbbox_Port.Enabled=false;
    Cbbox_Bound.Enabled=false;
    Cbbox_Databits.Enabled=false;
    Cbbox_Parity.Enabled=false;
    Cbbox_Stop.Enabled=false; }
    private void setclose()
    {
    Cbbox_Port.Enabled=true;
    Cbbox_Bound.Enabled=true;
    Cbbox_Databits.Enabled=true;
    Cbbox_Parity.Enabled=true;
    Cbbox_Stop.Enabled=true; } private void Btn_Clear_Click(object sender, System.EventArgs e)
    {
    Txtbox_Recev.Clear();
    Txtbox_Send.Clear();
    } private void Form1_Load(object sender, System.EventArgs e)
    {
    testComm();

    }
    private void testComm()
    {
    for(short i=10;i>0;i--)
    {
    axMSComm1.CommPort=i;
    try
    {
    axMSComm1.PortOpen=true;
    }
    catch
    {
    ;
    }
    if(axMSComm1.PortOpen==true)
    {

    Cbbox_Port.Items.Add("COM"+i);
    axMSComm1.PortOpen=false;
    Cbbox_Port.Text="COM"+i;
    } } } private void button2_Click(object sender, System.EventArgs e)
    {
    testComm(Cbbox_Port.Enabled);
    }

    private void testComm(bool a)
    {

    if(a==true)
    {
    Cbbox_Port.Items.Clear();
    for(short i=10;i>0;i--)
    {
    axMSComm1.CommPort=i;
    try
    {
    axMSComm1.PortOpen=true;
    }
    catch
    {
    ;
    }
    if(axMSComm1.PortOpen==true)
    {

    Cbbox_Port.Items.Add("COM"+i);
    axMSComm1.PortOpen=false;
    Cbbox_Port.Text="COM"+i;
    } }
    } }

    }
    }
      

  11.   

    感谢hustxifangshibai(西方失败) 的大力支持