此代码Genrat“Com”端口错误并给我Com端口信息和Sol我连接的错误Dlink Dongle使用。谢谢。 [英] This Code Genrat "Com" Port Error And Give Me Com Port Information And Sol The Error I Am Connected Dlink Dongle Use. Thanks.

查看:130
本文介绍了此代码Genrat“Com”端口错误并给我Com端口信息和Sol我连接的错误Dlink Dongle使用。谢谢。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 使用系统; 
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Windows.Forms;

使用 System.IO.Ports;
命名空间 call_app
{
public 部分 Form1:表格
{
public Form1()
{
InitializeComponent();
}

private void button1_Click( object sender,EventArgs e)
{
try
{
SerialPort sp = new SerialPort();
sp.PortName = COM3;
sp.BaudRate = 9600 ;
sp.Parity = Parity.None;
sp.DataBits = 8 ;
sp.StopBits = StopBits.One;
sp.Handshake = Handshake.XOnXOff;
sp.DtrEnable = true ;
sp.RtsEnable = true ;
sp.Open();

if (!sp.IsOpen)
{
MessageBox.Show( 未打开串口);
return ;
}

sp.WriteLine( AT + Environment 。新队);
sp.WriteLine( ATD = \ + 7600684319; + \ + Environment.NewLine);

}
catch (异常errcatch)
{
MessageBox.Show(errcatch.Message);
}
}

私有 void Form1_Load( object sender,EventArgs e)
{

}
}
}

解决方案



审核完代码后,我认为您可能需要考虑以下几点:



  1. 在班级声明串口。

  2. 在表单加载处理程序中创建,初始化和打开串行端口。

  3. 根据调制解调器制造商的建议设置串行端口属性,而不是从其他来源复制的代码。

  4. 写入串口后,等待调制解调器制造商推荐的时间。

  5. 声明DataReceived事件的处理程序。除非您专门禁止调制解调器成功消息,否则您将能够看到应用程序与调制解调器的交互。

  6. 声明ErrorReceived事件的处理程序。在这里你可以确定出了什么问题。



保留这些物品请注意,这是对代码的修改:

 使用系统; 
使用 System.IO.Ports;
使用 System.Threading;
使用 System.Windows.Forms;

命名空间 CallApp
{

// ************************************* **************类Form1

public 部分 Form1:表格
{
// 在班级声明
SerialPort serial_port = null ;

// **************** ****************************** OnFormClosed

// 确保串口关闭并在末尾处理
// 应用程序

protected 覆盖 void OnFormClosed(FormClosedEventArgs e)
{

base .OnFormClosed(e);

if (serial_port!= null
{
if (serial_port.IsOpen)
{
serial_port.Close();
}
serial_port.Dispose();
serial_port = null ;
}
}

// ****** ****************************************** Form1_Load

// 创建,初始化并打开表格中的串口
// 加载处理程序

private void Form1_Load( object sender,
EventArgs e)
{

serial_port = new SerialPort();
serial_port.DataReceived + =
new SerialDataReceivedEventHandler(
serial_port_DataReceived);
serial_port.ErrorReceived + =
new SerialErrorReceivedEventHandler(
serial_port_ErrorReceived);

serial_port.PortName = COM3;
// 您必须设置以下
// 基于调制解调器的值
// 制造商推荐

// 请注意可能不是
//
serial_port.BaudRate = 9600 ;
serial_port.Parity = Parity.None;
serial_port.DataBits = 8 ;
serial_port.StopBits = StopBits.One;
serial_port.Handshake = Handshake.XOnXOff;
serial_port.DtrEnable = true ;
serial_port.RtsEnable = true ;
serial_port.NewLine = Environment.NewLine;
// 在这里打开串口
尝试
{
serial_port.Open();
}
catch (例外情况)
{
throw new ApplicationException(
失败打开串口 +
ex.Message + ex.StackTrace);
}
}

// ****** Form**** 1 ***************************************** span>

public Form1()
{

InitializeComponent();
}

// *********** ********************************** button1_Click

// 处理button1 Click事件

private void button1_Click( object sender,
EventArgs e)
{

尝试
{
如果(! serial_port.IsOpen)
{
MessageBox.Show( 串口未打开
串口错误);
return ;
}

serial_port.WriteLine( AT);
Thread.Sleep( 1000 );

serial_port.WriteLine( ATD = \ +
7600684319; +
\);
Thread.Sleep( 1000 );
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + ex.StackTrace);
}
}

// ****** **************************** serial_port_DataReceived

// 处理DataReceived事件

private void serial_port_DataReceived(
object sender,
SerialDataReceivedEventArgs e)
{
string received_string = String .Empty;
SerialPort serial_port =(SerialPort)sender;

received_string = serial_port.ReadExisting();
MessageBox.Show(received_string,
Recieved);
}

// *********** ********************** serial_port_ErrorReceived

// 在此处理串口错误

void serial_port_ErrorReceived(
< span class =code-keyword> object
sender,
SerialErrorReceivedEventArgs e)
{
SerialPort serial_port =(SerialPort)sender;

MessageBox.Show( Serial Port error
错误);
}

} // class Form1

} // 命名空间call_app



即使在COM3上没有调制解调器,也会显示以下日志:

 AT 
OK
ATD = 7600684319;
NO DIALTONE



希望有所帮助。


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 call_app
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                SerialPort sp = new SerialPort();
                sp.PortName = "COM3";
                sp.BaudRate = 9600;
                sp.Parity = Parity.None;
                sp.DataBits = 8;
                sp.StopBits = StopBits.One;
                sp.Handshake = Handshake.XOnXOff;
                sp.DtrEnable = true;
                sp.RtsEnable = true;
                sp.Open();

                if (!sp.IsOpen)
                {
                    MessageBox.Show("Serial port is not opened");
                    return;
                }

                sp.WriteLine("AT" + Environment.NewLine);
                sp.WriteLine("ATD=\"" + "7600684319;" + "\"" + Environment.NewLine);

            }
            catch(Exception errcatch)
            {
                MessageBox.Show(errcatch.Message);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}

解决方案


After reviewing your code, here are a few things that I think you might want to consider:


  1. Declare the serial port at the class level.
  2. Create, initialize, and open the serial port in the form load handler.
  3. Set the serial port properties based upon the modem manufacturer's recommendations, not on code copied from some other source.
  4. After writing to the serial port, wait the amount of time recommended by the modem manufacturer.
  5. Declare a handler for the DataReceived event. Unless you specifically inhibit modem success messages, you will be able to see your application's interactions with the modem.
  6. Declare a handler for the ErrorReceived event. Here you will be able to determine what went wrong.


Keeping those items in mind, here is a rework of your code:

using System;
using System.IO.Ports;
using System.Threading;
using System.Windows.Forms;

namespace CallApp
    {

    // *************************************************** class Form1

    public partial class Form1 : Form
        {
                                        // declare at class level
        SerialPort serial_port = null;

        // ********************************************** OnFormClosed

        // insure that serial port is closed and disposed at the end 
        // of the application

        protected override void OnFormClosed ( FormClosedEventArgs e )
            {

            base.OnFormClosed ( e );

            if ( serial_port != null )
                {
                if ( serial_port.IsOpen )
                    {
                    serial_port.Close ( );
                    }
                serial_port.Dispose ( );
                serial_port = null;
                }
            }

        // ************************************************ Form1_Load

        // create, initialize, and open the serial port in the forms
        // load handler

        private void Form1_Load ( object sender, 
                                  EventArgs e )
            {

            serial_port = new SerialPort ( );
            serial_port.DataReceived += 
                new SerialDataReceivedEventHandler ( 
                    serial_port_DataReceived );
            serial_port.ErrorReceived += 
                new SerialErrorReceivedEventHandler ( 
                    serial_port_ErrorReceived );

            serial_port.PortName = "COM3";
                                        // you must set the following 
                                        // values based upon modem 
                                        // manufacturer recommendation

                                        // note that some may not be 
                                        // needed
            serial_port.BaudRate = 9600;
            serial_port.Parity = Parity.None;
            serial_port.DataBits = 8;
            serial_port.StopBits = StopBits.One;
            serial_port.Handshake = Handshake.XOnXOff;
            serial_port.DtrEnable = true;
            serial_port.RtsEnable = true;
            serial_port.NewLine = Environment.NewLine;
                                        // open the serial port here
            try 
                {
                serial_port.Open ( );
                }
            catch ( Exception ex )
                {
                throw new ApplicationException ( 
                    "Failed to open serial port" +
                    ex.Message + ex.StackTrace );
                }
            }

        // ***************************************************** Form1

        public Form1 ( )
            {

            InitializeComponent ( );
            }

        // ********************************************* button1_Click

        // handle the button1 Click event

        private void button1_Click ( object    sender, 
                                     EventArgs e )
            {

            try
                {
                if ( !serial_port.IsOpen )
                    {
                    MessageBox.Show ( "Serial port is not opened",
                                      "Serial Port Error" );
                    return;
                    }

                serial_port.WriteLine ( "AT" );
                Thread.Sleep ( 1000 );

                serial_port.WriteLine ( "ATD=\"" + 
                                        "7600684319;" + 
                                        "\"" );
                Thread.Sleep ( 1000 );
                }
            catch ( Exception ex )
                {
                MessageBox.Show ( ex.Message + ex.StackTrace );
                }
            }

        // ********************************** serial_port_DataReceived

        // handle the DataReceived event

        private void serial_port_DataReceived (
                                object                      sender,
                                SerialDataReceivedEventArgs e )
            {
            string      received_string = String.Empty;
            SerialPort  serial_port = ( SerialPort ) sender;

            received_string = serial_port.ReadExisting ( );
            MessageBox.Show ( received_string, 
                              "Recieved" );
            }

        // ********************************* serial_port_ErrorReceived

        // handle serial port errors here

        void serial_port_ErrorReceived ( 
                                    object sender, 
                                    SerialErrorReceivedEventArgs e )
            {
            SerialPort  serial_port = ( SerialPort ) sender;

            MessageBox.Show ( "Serial Port error",
                              "Error" );
            }

        } // class Form1

    } // namespace call_app


Even without a modem on COM3, the following log is displayed:

AT
OK
ATD="7600684319;"
NO DIALTONE


Hope that helps.


这篇关于此代码Genrat“Com”端口错误并给我Com端口信息和Sol我连接的错误Dlink Dongle使用。谢谢。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆