RS232串口通讯C#的win7的.NET Framework 3.5 SP1 [英] RS232 serial port communication c# win7 .net framework 3.5 sp1

查看:334
本文介绍了RS232串口通讯C#的win7的.NET Framework 3.5 SP1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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
 

这是一个问题太多,你现在需要自己控制的握手信号。直到他们看到机器加电并准备接收串口设备的绝大多数将不会发送任何东西。设置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.

这篇关于RS232串口通讯C#的win7的.NET Framework 3.5 SP1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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