Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在C语言中从串口正确获取阵列数据#_C#_Serial Port - Fatal编程技术网

C# 如何在C语言中从串口正确获取阵列数据#

C# 如何在C语言中从串口正确获取阵列数据#,c#,serial-port,C#,Serial Port,我在下面的程序中的目的是从微控制器获取16字节的数据,并为适当的指令处理数据。这里有很多相关的问题和答案,但我在下一期中找不到任何相关的问题和答案。我可以从MCU得到16个字节。字节的值是正确的,我可以在dataGridView中看到它们,但字节的顺序正在改变。例如,首先MCUData[0]=0x01,MCUData[1]=0xFE,MCUData[2]=0xCC,然后它会更改为MCUData[0]=0xFE,MCUData[1]=0xCC,MCUData[2]=0x01。İt就像在字节数组中移

我在下面的程序中的目的是从微控制器获取16字节的数据,并为适当的指令处理数据。这里有很多相关的问题和答案,但我在下一期中找不到任何相关的问题和答案。我可以从MCU得到16个字节。字节的值是正确的,我可以在dataGridView中看到它们,但字节的顺序正在改变。例如,首先MCUData[0]=0x01,MCUData[1]=0xFE,MCUData[2]=0xCC,然后它会更改为MCUData[0]=0xFE,MCUData[1]=0xCC,MCUData[2]=0x01。İt就像在字节数组中移动数据时遇到了一些问题。我确信我的MCU发送数据正确,因为我签入了一个串行终端程序。我的代码在下面

使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.IO.Ports;
使用系统线程;
名称空间串行通信
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
dataGridView1.Columns.Add(“MCUData”,“字节名”);
dataGridView1.Columns.Add(“MCUData”,“Byte Value”);
}
public System.IO.Ports.SerialPort SerialPc;
#区域变量
公共字符串可用端口;
公共字符串[]端口=SerialPort.GetPortNames();
字节[]MCUData=新字节[16];
#端区
私有void Connect\u单击(对象发送方,事件参数e)
{
DataGreedByteNameShow();
SerialConnectandRead();
ConnectButton.Enabled=false;
DisconnectButton.Enabled=true;
}
私有无效断开连接\u单击(对象发送方,事件参数e)
{
SerialPc.Close();
ConnectButton.Enabled=true;
DisconnectButton.Enabled=false;
}
public void SerialConnectandRead()
{
SerialPc=新的串行端口(可用端口,115200,奇偶校验。无,8,停止位。1);
尝试
{
SerialPc.Open();
SerialPc.DataReceived+=新的SerialDataReceivedEventHandler(SerialPc\u DataReceived);
}
捕获(例外情况除外)
{
Show(例如ToString(),“串行端口错误”);
}
}
private void SerialPc_DataReceived(对象发送方,SerialDataReceivedEventArgs e)
{
SerialPc.DiscardNull=false;
SerialPc.Read(MCUData,0,16);
SerialPc.ReceivedBytestThreshold=16;
DataGreedByteValueShow();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
foreach(端口中的字符串端口)
{
comboBox1.Items.Add(端口);
}
DisconnectButton.Enabled=false;
}
public void datagreedbyteneshow()
{
dataGridView1.Rows.Add(“MCUData[0]”);
dataGridView1.Rows.Add(“MCUData[1]”);
dataGridView1.Rows.Add(“MCUData[2]”);
dataGridView1.Rows.Add(“MCUData[3]”);
dataGridView1.Rows.Add(“MCUData[4]”);
dataGridView1.Rows.Add(“MCUData[5]”);
dataGridView1.Rows.Add(“MCUData[6]”);
dataGridView1.Rows.Add(“MCUData[7]”);
dataGridView1.Rows.Add(“MCUData[8]”);
dataGridView1.Rows.Add(“MCUData[9]”);
dataGridView1.Rows.Add(“MCUData[10]”);
dataGridView1.Rows.Add(“MCUData[11]”);
dataGridView1.Rows.Add(“MCUData[12]”);
dataGridView1.Rows.Add(“MCUData[13]”);
dataGridView1.Rows.Add(“MCUData[14]”);
dataGridView1.Rows.Add(“MCUData[15]”);
}
私有void DataGreedByteValueShow()
{
dataGridView1.Rows[0]。单元格[1]。值=MCUData[0];
dataGridView1.Rows[1]。单元格[1]。值=MCUData[1];
dataGridView1.Rows[2]。单元格[1]。值=MCUData[2];
dataGridView1.Rows[3]。单元格[1]。值=MCUData[3];
dataGridView1.Rows[4]。Cells[1]。Value=MCUData[4];
dataGridView1.Rows[5]。Cells[1]。Value=MCUData[5];
dataGridView1.Rows[6]。Cells[1]。Value=MCUData[6];
dataGridView1.Rows[7]。Cells[1]。Value=MCUData[7];
dataGridView1.Rows[8]。Cells[1]。Value=MCUData[8];
dataGridView1.Rows[9]。Cells[1]。Value=MCUData[9];
dataGridView1.Rows[10]。Cells[1]。Value=MCUData[10];
dataGridView1.Rows[11]。单元格[1]。值=MCUData[11];
dataGridView1.Rows[12]。Cells[1]。Value=MCUData[12];
dataGridView1.Rows[13]。Cells[1]。Value=MCUData[13];
dataGridView1.Rows[14]。Cells[1]。Value=MCUData[14];
dataGridView1.Rows[15]。Cells[1]。Value=MCUData[15];
}
私有无效组合框1\u SelectedIndexChanged(对象发送方,事件参数e)
{
AvailablePort=comboBox1.SelectedItem.ToString();
}
}
}

根据MSDN文档:

从SerialPort输入缓冲区读取若干字节并写入 这些字节以指定的偏移量放入字节数组

以下是当前代码如何使用该函数:

SerialPc.Read(MCUData, 0, 16);
缓冲区是一个全局变量,定义为:

byte[] MCUData = new byte[16];
这是解决问题的一种方法:

    List<byte> MCUDataOverTime = new List<byte>();        

    private void SerialPc_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        SerialPc.DiscardNull = false;
        SerialPc.Read(MCUData, 0, 16);
        MCUDataOverTime.AddRange(MCUData);
        SerialPc.ReceivedBytesThreshold = 16;
        DataGreedByteValueShow();
    }

    private void DataGreedByteValueShow()
    {
        if (MCUDataOverTime.Count >= 16)
        {
            dataGridView1.Rows[0].Cells[1].Value = MCUDataOverTime[0];
            dataGridView1.Rows[1].Cells[1].Value = MCUDataOverTime[1];
            dataGridView1.Rows[2].Cells[1].Value = MCUDataOverTime[2];
            dataGridView1.Rows[3].Cells[1].Value = MCUDataOverTime[3];
            dataGridView1.Rows[4].Cells[1].Value = MCUDataOverTime[4];
            dataGridView1.Rows[5].Cells[1].Value = MCUDataOverTime[5];
            dataGridView1.Rows[6].Cells[1].Value = MCUDataOverTime[6];
            dataGridView1.Rows[7].Cells[1].Value = MCUDataOverTime[7];
            dataGridView1.Rows[8].Cells[1].Value = MCUDataOverTime[8];
            dataGridView1.Rows[9].Cells[1].Value = MCUDataOverTime[9];
            dataGridView1.Rows[10].Cells[1].Value = MCUDataOverTime[10];
            dataGridView1.Rows[11].Cells[1].Value = MCUDataOverTime[11];
            dataGridView1.Rows[12].Cells[1].Value = MCUDataOverTime[12];
            dataGridView1.Rows[13].Cells[1].Value = MCUDataOverTime[13];
            dataGridView1.Rows[14].Cells[1].Value = MCUDataOverTime[14];
            dataGridView1.Rows[15].Cells[1].Value = MCUDataOverTime[15];
        }
    }
List mcudata加班=新建列表();
private void SerialPc_DataReceived(对象发送方,SerialDataReceivedEventArgs e)
{
SerialPc.DiscardNull=false;
SerialPc.Read(MCUData,0,16);
MCUData.AddRange(MCUData);
SerialPc.ReceivedBytestThreshold=16;
DataGreedByteValueShow();
}
私有void DataGreedByteValueShow()
{
如果(mcudata超时.Count>=16)
{
dataGridView1.Rows[0]。单元格[1]。值=MCUDataOverTime[0];
dat
    List<byte> MCUDataOverTime = new List<byte>();        

    private void SerialPc_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        SerialPc.DiscardNull = false;
        SerialPc.Read(MCUData, 0, 16);
        MCUDataOverTime.AddRange(MCUData);
        SerialPc.ReceivedBytesThreshold = 16;
        DataGreedByteValueShow();
    }

    private void DataGreedByteValueShow()
    {
        if (MCUDataOverTime.Count >= 16)
        {
            dataGridView1.Rows[0].Cells[1].Value = MCUDataOverTime[0];
            dataGridView1.Rows[1].Cells[1].Value = MCUDataOverTime[1];
            dataGridView1.Rows[2].Cells[1].Value = MCUDataOverTime[2];
            dataGridView1.Rows[3].Cells[1].Value = MCUDataOverTime[3];
            dataGridView1.Rows[4].Cells[1].Value = MCUDataOverTime[4];
            dataGridView1.Rows[5].Cells[1].Value = MCUDataOverTime[5];
            dataGridView1.Rows[6].Cells[1].Value = MCUDataOverTime[6];
            dataGridView1.Rows[7].Cells[1].Value = MCUDataOverTime[7];
            dataGridView1.Rows[8].Cells[1].Value = MCUDataOverTime[8];
            dataGridView1.Rows[9].Cells[1].Value = MCUDataOverTime[9];
            dataGridView1.Rows[10].Cells[1].Value = MCUDataOverTime[10];
            dataGridView1.Rows[11].Cells[1].Value = MCUDataOverTime[11];
            dataGridView1.Rows[12].Cells[1].Value = MCUDataOverTime[12];
            dataGridView1.Rows[13].Cells[1].Value = MCUDataOverTime[13];
            dataGridView1.Rows[14].Cells[1].Value = MCUDataOverTime[14];
            dataGridView1.Rows[15].Cells[1].Value = MCUDataOverTime[15];
        }
    }
    List<byte> MCUDataOverTime = new List<byte>();        

    private void SerialPc_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        SerialPc.DiscardNull = false;

        int readcount = 0;
        byte [] temp;

        do
        {
            readcount = SerialPc.Read(MCUData, 0, 16);
            if (readcount > 0)
            {
                temp = new byte[readcount];
                Array.Copy(MCUData, 0, temp, 0, readcount);
                MCUDataOverTime.AddRange(temp);
                SerialPc.ReceivedBytesThreshold = 16;
                DataGreedByteValueShow();
            }
        } while (readcount > 0);
    }

    private void DataGreedByteValueShow()
    {
        if (MCUDataOverTime.Count >= 16)
        {
            dataGridView1.Rows[0].Cells[1].Value = MCUDataOverTime[0];
            dataGridView1.Rows[1].Cells[1].Value = MCUDataOverTime[1];
            dataGridView1.Rows[2].Cells[1].Value = MCUDataOverTime[2];
            dataGridView1.Rows[3].Cells[1].Value = MCUDataOverTime[3];
            dataGridView1.Rows[4].Cells[1].Value = MCUDataOverTime[4];
            dataGridView1.Rows[5].Cells[1].Value = MCUDataOverTime[5];
            dataGridView1.Rows[6].Cells[1].Value = MCUDataOverTime[6];
            dataGridView1.Rows[7].Cells[1].Value = MCUDataOverTime[7];
            dataGridView1.Rows[8].Cells[1].Value = MCUDataOverTime[8];
            dataGridView1.Rows[9].Cells[1].Value = MCUDataOverTime[9];
            dataGridView1.Rows[10].Cells[1].Value = MCUDataOverTime[10];
            dataGridView1.Rows[11].Cells[1].Value = MCUDataOverTime[11];
            dataGridView1.Rows[12].Cells[1].Value = MCUDataOverTime[12];
            dataGridView1.Rows[13].Cells[1].Value = MCUDataOverTime[13];
            dataGridView1.Rows[14].Cells[1].Value = MCUDataOverTime[14];
            dataGridView1.Rows[15].Cells[1].Value = MCUDataOverTime[15];
        }
    }