C# 无法从串行端口读取数据(在c中)

C# 无法从串行端口读取数据(在c中),c#,serial-port,C#,Serial Port,我想从串行端口读取传感器的数据,我可以读取端口的名称,但我无法从它们接收数据。。。我是c和串口编码新手,谢谢~ 以下是我的尝试: using System; using System.ComponentModel; using System.Collections; using System.Diagnostics; using System.IO; using System.IO.Ports; using System.Text; using System.Security; using Sy

我想从串行端口读取传感器的数据,我可以读取端口的名称,但我无法从它们接收数据。。。我是c和串口编码新手,谢谢~

以下是我的尝试:

using System;
using System.ComponentModel;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.IO.Ports;
using System.Text;
using System.Security;
using System.Security.Permissions;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    class Program
    {
        //----------------------------------------------------- GLOBAL PARAMETER ------------------------------------------------------
        //---serial port objects
        static private List <SerialPort> _serialPort = new List<SerialPort>();
        //---serial port read buffer
        static private List <byte[]> _buffer = new List<byte[]>();
        //---length of the port list
        static private int _length;

        //--------------------------------------------------- INIT FUNCTIONS ----------------------------------------------------------
        //---init main function
        static private void initFunction(){
            //create the serial port objs
            createPortObj();
            //create the buffers
            createBuffer();
            //init a clock
            //init the ports' name
            return;
        }

        //---Set the port objs
        static private void setPortOption(int i) {
            SerialPort tPort = _serialPort[i];

            //set options
            tPort.BaudRate = 9600;
            tPort.Parity = Parity.None;
            tPort.StopBits = StopBits.One;
            tPort.DataBits = 8;
            tPort.Handshake = Handshake.None;

            //set the datareceived function 
            //tPort.DataReceived += new SerialDataReceivedEventHandler(DataReceivedHandler);
            tPort.DataReceived += DataReceivedHandler;

            tPort.Open();

            return;
        }

        //---Create the port objs
        static private void createPortObj(){
            // Get a list of serial port names. 
            string[] _names = SerialPort.GetPortNames();
            _length = _names.Length;

            //create the objs
            for(int i=0; i < _length; i++)
            {
                Console.WriteLine(_names[i]);
                //create the port objects
                _serialPort.Add(new SerialPort(_names[i]));
                //init the port object
                setPortOption(i);
            }

            return;
        }

        //---Create the buffer
        static private void createBuffer() { 
            //create buffer for each port obj
            for (int i = 0; i < _length; i++){
                byte[] bufferOne = new Byte[_serialPort[i].BytesToRead];
                _buffer.Add(bufferOne);
            }
            return;
        }

        //-------------------------------------------------- FEATURED FUNCTION ----------------------------------------------------
        //---Transmit the code 
        //---Data received handler
        static public void DataReceivedHandler(Object sender, SerialDataReceivedEventArgs e){
            SerialPort sp = (SerialPort)sender;
            string indata = sp.ReadExisting();
            Console.WriteLine("Data Received:");
            Console.Write(indata);


            //Receive the data from serial ports
            for (int i=0; i<_length; i++){
                int temp;
                temp = _serialPort[i].Read(_buffer[i], 100, _buffer[i].Length);
                Console.WriteLine(temp);

            }

            return;
        }


        //-------------------------------------------------- RETRIVE FUNCTION ----------------------------------------------------
        //---Retrive the source 
        static private void retriveFunction(){
            //close the serial ports
            foreach (SerialPort port in _serialPort){
                port.Close();
            }

            return;
        }

        //-------------------------------------------------- MAIN -----------------------------------------------------------------
        //---main function
        static void Main(string[] args){
            //init function, and open the 
            initFunction();

            int[] num = new int[_length];
            for (int i = 0; i < _length; i++){
                num[i] = _serialPort[i].Read(_buffer[i], 0, _buffer[i].Length);
            }

            //check the result of the read function
            Console.Write(num[0]);

            //on serial port receiving

            //retrive the source
            retriveFunction();

            Console.ReadLine();
        }
    }
}

再次感谢~

我认为您的代码不是等待数据接收。在传感器发回数据之前,您的代码已完成,程序已结束,您应该在新线程或DataReceived事件中处理数据。

调试。调试。调试。您是否能够从串行端口而不是通过C应用程序获取数据?从单个串行端口开始,确保您能够从其中一个端口接收数据,读取结果为0,并且您没有输入i var..@ilansch。我确信数据可以由其他应用程序接收,例如arduino客户端。此外,我不认为是数组问题导致了这个问题哦,你是说SerialDataReceiveDevenHandler函数吗?我已经在上面的代码中添加了它,奇怪的是,没有调用事件侦听器函数,我设置了中断,只是看到程序忽略了代码