Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.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# 从Arduino向PC发送大型阵列_C#_Arduino_Serial Port - Fatal编程技术网

C# 从Arduino向PC发送大型阵列

C# 从Arduino向PC发送大型阵列,c#,arduino,serial-port,C#,Arduino,Serial Port,我正试图从arduino发送一个字节数组,因为有一个C#windows窗体应用程序。问题是没有到达完整的阵列。前100-200字节是正确的,但其他字节都是零。多少是正确的完全是随机的。这是我的arduino abd C#代码 if(commandString.equals(“impo”)){ 数字写入(led2Pin,高); 数字写入(led3Pin,低电平); 字节buf[2048]; 对于(int i=0;i2){ 字节lb=SRAM.readByte(2*i); 字节hb=SRAM.rea

我正试图从arduino发送一个字节数组,因为有一个C#windows窗体应用程序。问题是没有到达完整的阵列。前100-200字节是正确的,但其他字节都是零。多少是正确的完全是随机的。这是我的arduino abd C#代码

if(commandString.equals(“impo”)){
数字写入(led2Pin,高);
数字写入(led3Pin,低电平);
字节buf[2048];
对于(int i=0;i<1024;i++){
如果(Serial.availableForWrite()>2){
字节lb=SRAM.readByte(2*i);
字节hb=SRAM.readByte(2*i+1);
int16_t raw2=单词(hb,lb);
buf[2*i]=raw2&255;
buf[2*i+1]=(raw2>>8)&255;
}
}
串行写入(buf,sizeof(buf));
数字写入(led2Pin,低电平);
数字写入(led3Pin,高);
}
和c#:

int[]bdata=newint[1024];
私有void SerialPort1_DataReceived(对象发送方,SerialDataReceivedEventArgs e)
{
int bytes=2048;//ComPort.BytesToRead;
byte[]buffer=new byte[bytes];//Párosak a low bytok.Páratlanok a hugh byteok
ComPort.Read(缓冲区,0,字节);
int[]intbuffer=Array.ConvertAll(buffer,x=>(int)x);
//int[]bdata=新的int[字节/2];
对于(int i=0;i<1024;i++)
{
bdata[i]=intbuffer[2*i+1]*256+intbuffer[2*i];
}
}
DataReceivedTRESHLOD设置为2048。 以下是调试过程中的图片:


有没有人知道可能是什么问题?SRAM是23LCV512。

我对C#不是很了解,但是
int[]intbuffer=Array.ConvertAll(buffer,x=>(int)x)看起来很可疑。在我看来,似乎您正在将所有2048字节转换为2048
int
值,而不是指定字节顺序的1024
int
值。
Read
方法通常返回实际读取的字节数-这可能小于请求的数量,例如,由于操作系统使用的缓冲区小于字节数组。您不能假设所有数据将在一次
Read
调用中返回,您必须使用循环或将串行端口包装在缓冲流中通过通信链路发送原始数据是不明智的。首先,您需要人工干预来启动和同步两端的程序。然后假设链接是完美的,没有噪音/干扰。传输结束后,无法确认所有数据是否完整正确地收到。您填写的Arduino代码
buf[]
对我来说毫无意义。首先,为什么复制每个单词的条件是
Serial.availableForWrite()>2
?其次,低字节和高字节到半字操作的转换似乎都是多余的。您以小端顺序获取,然后以小端顺序存储。字节顺序没有变化@sawdust Serial.AvailableForWrite已在以前的版本中使用。这是一个保留在代码中的错误。在上一个程序中,我尝试在一个2字节的包中发送10个数据。因为它不起作用,我试着寄了一个包裹。它有很多噪音和虚假数据。我从Arduino发送2048字节,到达2200多字节。
if (commandString.equals("IMPOR")) {
  digitalWrite(led2Pin, HIGH);
  digitalWrite(led3Pin, LOW);
  byte buf[2048];
  for (int i = 0; i < 1024; i++) {
    if (Serial.availableForWrite() > 2) {
      byte lb = SRAM.readByte(2 * i);
      byte hb = SRAM.readByte(2 * i + 1);
      int16_t raw2 = word(hb, lb);
      buf[2 * i] = raw2 & 255;
      buf[2 * i + 1] = (raw2 >> 8)  & 255;
    }
  }
  Serial.write(buf,sizeof(buf));
  digitalWrite(led2Pin, LOW);
  digitalWrite(led3Pin, HIGH);
}
int[] bdata = new int[1024];
private void SerialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        int bytes = 2048; // ComPort.BytesToRead;
        byte[] buffer = new byte[bytes]; //Párosak a low bytok. Páratlanok a hugh byteok
        ComPort.Read(buffer, 0, bytes);
        int[] intbuffer = Array.ConvertAll(buffer, x => (int)x);
        //int[] bdata = new int[bytes / 2];
        for(int i = 0; i < 1024; i++)
        {
            bdata[i] = intbuffer[2 * i + 1] * 256 + intbuffer[2 * i];
        }
    }