RS232串口通讯C#的win7的.NET Framework 3.5 SP1串口、通讯、NET、Framework

由网友(早恋提高情商)分享简介:HI即时通讯新在C#中的串行端口。我写C#程序的运行是winxp和Win7保持接收到的数据从串口当机发送数据。使用System.IO;使用System.IO.Ports;使用的System.Threading;命名空间RS232RVR{公共部分类Form1中:形态{公共Form1中(){的Initiali...

HI 即时通讯新在C#中的串行端口。我写C#程序的运行是winxp和Win7保持接收到的数据从串口当机发送数据。

 使用System.IO;
使用System.IO.Ports;
使用的System.Threading;


命名空间RS232RVR
{
公共部分类Form1中:形态
{
    公共Form1中()
    {
        的InitializeComponent();
        SettingRS232();
    }

    公共无效SettingRS232()
    {
        尝试
        {
            的SerialPort mySerialPort =新的SerialPort(COM6);

            mySerialPort.BaudRate = 9600;
            mySerialPort.Parity = Parity.None;
            mySerialPort.StopBits = StopBits.One;
            mySerialPort.DataBits = 8;
            mySerialPort.Handshake = Handshake.None; //发送到硬件流控制。

            mySerialPort.DataReceived + =新SerialDataReceivedEventHandler(DataReceviedHandler);

            mySerialPort.Open();


            richTextBox1.Text上=;

            mySerialPort.Close();
        }
        赶上(例外前)
        {
            richTextBox1.Text = ex.Message;
        }

    }

    私人无效DataReceviedHandler(
                    对象发件人,
                    SerialDataReceivedEventArgs五)
    {
        的SerialPort SP =(的SerialPort)发送;
        字符串INDATA = sp.ReadExisting();
        richTextBox1.Text = INDATA;

    }

}
 

}

COM6是活跃在我的电脑。但我的问题是,似乎当它有数据从串口传来的DataReceived事件不火。 (我不得不使用一些免费的应用程序的检查运动)

任何人都可以帮忙吗?

感谢

解决方案

  mySerialPort.Open();
        richTextBox1.Text上=;
        mySerialPort.Close();
 

这是行不通的,你打开它之后关闭串口几微秒。是的,DataReceived事件检索处理程序是不可能解雇。只有关闭程序时关闭端口。

  mySerialPort.Handshake = Handshake.None
 
.NET Framework For Win7 Win8 V4.5.1 .NET Framework v4.0

这是一个问题太多,你现在需要自己控制的握手信号。直到他们看到机器加电并准备接收串口设备的绝大多数将不会发送任何东西。设置DtrEnabled和RtsEnabled属性为true。

HI im new in c# serial port. im writing a c# program running is winXP and win7 to keep received data from the serial port when the machine was sent data.

using System.IO;
using System.IO.Ports;
using System.Threading;


namespace RS232RVR
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        SettingRS232();
    }

    public void SettingRS232 ()
    {
        try
        {
            SerialPort mySerialPort = new SerialPort("COM6");

            mySerialPort.BaudRate = 9600;
            mySerialPort.Parity = Parity.None;
            mySerialPort.StopBits = StopBits.One;
            mySerialPort.DataBits = 8;
            mySerialPort.Handshake = Handshake.None; //send to hardware flow control.

            mySerialPort.DataReceived += new SerialDataReceivedEventHandler(DataReceviedHandler);

            mySerialPort.Open();


            richTextBox1.Text = "on";

            mySerialPort.Close();
        }
        catch (Exception ex)
        {
            richTextBox1.Text = ex.Message;    
        }

    }

    private void DataReceviedHandler(
                    object sender,
                    SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        string indata = sp.ReadExisting();
        richTextBox1.Text = indata;

    }

}

}

COM6 is active in my pc. but my problem was seem the datareceived event is not fire when it has data coming from the serial port. ( i had checked the sport by using some of the freeware application)

anyone can help?

thanks

解决方案

        mySerialPort.Open();
        richTextBox1.Text = "on";
        mySerialPort.Close();

That's not going to work, you'll close the serial port a couple of microseconds after opening it. Yes, the DataReceived event handler is not likely to fire. Only close the port when shutting down your program.

        mySerialPort.Handshake = Handshake.None

That's a problem too, you'll need to control the handshake signals yourself now. The vast majority of serial port devices won't send anything until they see the machine powered up and ready to receive. Set the DtrEnabled and RtsEnabled properties to true.

阅读全文

相关推荐

最新文章