在从Com Port读取数据时需要帮助 [英] Need Help in reading data from Com Port

查看:50
本文介绍了在从Com Port读取数据时需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在从Com1端口读取数据时遇到问题


我可以从同一应用程序读取数据

但是现在我想从一个应用程序中写入数据,并从另一个应用程序中读取.

因此,所有专家"都请帮助我,并为我提供解决方案.

我当前的应用程序是...

I have a problem in reading data from Com1 Port


I can read data from same application

but now I want to write data from one application and read from another application.

So "All specialists " please help me and give me the solution.

My Current Application is...

using System;
using System.IO.Ports;
using System.Text;

namespace SerialPortExample
{
    class SerialPortProgram
    {
        // Create the serial port with basic settings
        SerialPort port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);

        [STAThread]
        static void Main(string[] args)
        {


            new SerialPortProgram();
        }

        private SerialPortProgram()
        {


           

            // Instatiate this class
            Console.WriteLine("Incoming Data:");


            port.DataReceived += new SerialDataReceivedEventHandler
(port_DataReceived);
            port.ErrorReceived += new SerialErrorReceivedEventHandler
(port_ErrorReceived);
            port.Open();
            port.Write("TEst1");
           Console.WriteLine(port.ReadExisting ());
            port.Close();
            System.Console.ReadKey();

            port.Close();
        }

        private void port_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
        {
            //Show all the incoming data in the port''s buffer
            Console.WriteLine("Character received");
            Console.WriteLine(port.ReadExisting());
        }


        private void port_ErrorReceived(object sender,
SerialErrorReceivedEventArgs e)
        {
            Console.WriteLine("Error");
        }
    }
}




等待着真的

JILANI SHAIKH




WAITING FOR REALLY

JILANI SHAIKH

推荐答案

我不相信您可以使用标准SerialPort来做到这一点,因为它每个端口一次只能接受一个连接-可以预料的.共享端口以使两个单独的应用程序可以访问该端口的唯一方法(一个用于读取,一个用于写入)将是编写一个单独的(可能是Windows服务)应用程序,该应用程序打开端口并将数据进出端口.两个应用程序.

以这种方式共享端口是一件不寻常的事情-确定要这样做吗?
I don''t believe that you can do that with the standard SerialPort, as it only accepts a single connection at a time per port - which is to be expected. The only way to share the port so that two separate applications can access it, one for read, and one for write, would be to write a separate (probably windows service) application which opens the port and feeds the data into and out of the two applications.

Sharing a port in this way is an unusual thing to do - are you sure you need to do this?


这篇关于在从Com Port读取数据时需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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