2014-08-29 92 views
1

这是我的代码。当我运行这个第一个字节保存在tempbuf的第0个位置。但是,当我第二个字节,第一个值在tempbuf :-(清除字节数组复制到c中的另一个字节数组#

private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) 
{ 
    int bytes = serialPort1.BytesToRead; 
    byte[] byte_buffer = new byte[200]; 
    byte[] tempbuf = new byte[200]; 
    byte[]tempbuf2=new byte[200]; 
    byte[] ar = new byte[20]; 
    byte[] ssd = new byte[4]; 
    byte[] ctrl = new byte[20]; 

    string pp = ""; 
    string ll = ""; 

    serialPort1.Read(byte_buffer, 0, bytes); 



    if (i == 0) 
    { 
     index = byte_buffer[0]; 
    } 
     tempbuf[q++] = byte_buffer[0]; 
    i++; 
    if (i == 200) 
    { 

     array[index] = TestSerializer.MarshalToStructureArray(tempbuf, tempbuf.Length); 
     if (textBox30.InvokeRequired) 
     { 

    this.Invoke(((MethodInvoker)delegate 
     { 
     textBox30.Text = index.ToString(); 
    })); 
    } 
    i = 0; 
    } 
    if (q == 200) 
    { 


    q = 0; 
} 
+1

了解格式化你的代码,请 – leppie 2014-08-29 04:30:30

+0

任何人都可以帮助我......? – user3952215 2014-08-29 05:13:32

+0

q的定义在哪里,你用它来做什么?你的变量名称相当隐晦,所以很难说出你想要做什么 – 2014-08-29 05:21:49

回答

1

我通过声明tempbuf为静态的字节数组解决了这个问题:

static byte[] tempbuf = new byte[200];