search

Home  >  Q&A  >  body text

C# serial port class reads dynamic data. How to transfer this data?

using System.IO.Ports;

namespace SerialPorts

{

    public class SerialPorts

    {     

        public void ReadSerialPorts()

        {

            SerialPort mySerialPort = new SerialPort("COM2");

            mySerialPort.BaudRate = 9600;

            mySerialPort.Parity = Parity.None;

            mySerialPort.StopBits = StopBits.One;

            mySerialPort.DataBits = 8;

            mySerialPort.Handshake = Handshake.None;

            mySerialPort.DataReceived = new SerialDataReceivedEventHandler(DataReceive_Method);

            mySerialPort.Open();

        }

        static void DataReceive_Method(object sender,SerialDataReceivedEventArgs e)

        {

            SerialPort sp = (SerialPort)sender;

            string indata = sp.ReadExisting();          

        }

    }

目前问题是,可以使用线程传出串口的缓存数据,但是线程不能断开,否则再打开就接不到数据。怎么能把indata值传出?我是打算把这个做成类库,然后提供JQUERY或Javascript调用。

phpcn_u5220phpcn_u52202739 days ago1672

reply all(1)I'll reply

  • 小小卫

    小小卫2017-09-19 16:14:04

    There is no p-bean. I really can’t help you. I won’t accept your question. Thank you for watching the video. There is no p-bean. Sorry for the trouble. Thank you

    reply
    0
  • Cancelreply