Serial.readBytes() and c#

hello friends,i want to transfer bytes from my bin file in pc to spi eeprom which is connected in arduino.i that code,i am interesting is it coect?

//c#
OpenFileDialog op=new OpenFileDialog();
            if(op.ShowDialog()==DialogResult.OK){
            byte[] b=File.ReadAllBytes(op.FileName);
            serialPort1.Open();
              
            for (int i = 0; i < b.Length;i+=256 )
                serialPort1.Write(b,i,256);




            }
            serialPort1.Close()
}

and arduino code

void loop(void) {
 
 
   digitalWrite(SS, HIGH);
  digitalWrite(SS, LOW);  
  SPI.transfer(WB_WRITE_ENABLE);
  digitalWrite(SS, HIGH);
  digitalWrite(SS, LOW);  
  SPI.transfer(WB_PAGE_PROGRAM);
  SPI.transfer(0);
  SPI.transfer(0);
  SPI.transfer(0);
  
  while(Serial.available()){
  
  
  byte buf[256];
Serial.readBytes(buf,256);
 
  
  
  
  for(int i=0;i<256;i++)
  SPI.transfer(buf[i]);
  }
  digitalWrite(SS, HIGH);

is it coect?

No. Your C# code opens the serial port, resetting the Arduino. While it’s busy rebooting, you start shoving data at it as fast as you can.

Serial.readBytes(buf,256);

Read a maximum of 256 bytes. Throw away the number actually read. Why?

  for(int i=0;i<256;i++)

Assume you actually read 256 bytes. Bad idea!

As for the other problems, well, you should have posted all of your code if you wanted all of the answers.

Have a look at serial input basics for simple reliable ways to receive data.

However it assumes you can use at least one byte value as an end-marker (or 2 as start- and end-markers). If that is not possible please say so.

...R

PaulS:
No. Your C# code opens the serial port, resetting the Arduino. While it’s busy rebooting, you start shoving data at it as fast as you can.

Serial.readBytes(buf,256);

Read a maximum of 256 bytes. Throw away the number actually read. Why?

  for(int i=0;i<256;i++)

Assume you actually read 256 bytes. Bad idea!

As for the other problems, well, you should have posted all of your code if you wanted all of the answers.

i want to write bios bin file in eeprom and in datasheet i saw it can write one page (256 byte)in one write command.i want to read all bytes from bios dump file
using c#

byte[] b=File.ReadAllBytes(op.FileName);//read all bytes
            serialPort1.Open();
              
            for (int i = 0; i < b.Length;i+=256 )
                serialPort1.Write(b,i,256);//sent one page
            serialPort1.Close();

and when i want to read that page in arduino ,and sent it to eeprom

 byte buf[256];
Serial.readBytes(buf,256);

 for(int i=0;i<256;i++)
  SPI.transfer(buf[i]);
  }

afther this ,sent second page from c# to arduino and …etc

            for (int i = 0; i < b.Length;i+=256 )
                serialPort1.Write(b,i,256);//sent one page

After reading some bytes, sending data this way is just plain wrong. You are sending 256 bytes starting at 0, then sending another 256 bytes starting at 1, then another 256 bytes starting at 2, etc.

Either send ONE byte starting at 0, then one byte starting at 1, etc., OR, send 256 bytes WITHOUT a for loop.

Whichever way you go, the proper number of bytes to send is NOT 256. The proper number of bytes to send is b.Length.

Don’t send 256 bytes if you only read 12 bytes from the file.

The readBytes() method returns the number of bytes read. Do NOT ignore that value. Do NOT assume that you read 256 bytes.

PaulS:

            for (int i = 0; i < b.Length;i+=256 )

serialPort1.Write(b,i,256);//sent one page



After reading some bytes, sending data this way is just plain wrong. You are sending 256 bytes starting at 0, then sending another 256 bytes starting at 1, then another 256 bytes starting at 2, etc.

Either send ONE byte starting at 0, then one byte starting at 1, etc., OR, send 256 bytes WITHOUT a for loop.

Whichever way you go, the proper number of bytes to send is NOT 256. The proper number of bytes to send is b.Length.

Don't send 256 bytes if you only read 12 bytes from the file.

The readBytes() method returns the number of bytes read. Do NOT ignore that value. Do NOT assume that you read 256 bytes.

no, friend i sent 256 byte in each time,and bytes position increas to 256 in each cycle
have written i+=256 in for loop
first starting adress =0,second 256,third 512 …etc
for (int i = 0; i < b.Length;i+=256 )
serialPort1.Write(b,i,256);//sent one page[/code]

i

The problem which you have, is that the serial input buffer might ( or might not ) be able to hold 256 bytes at once.

michinyon: The problem which you have, is that the serial input buffer might ( or might not ) be able to hold 256 bytes at once.

The standard serial input buffer in an Arduino is 64 bytes long. However as long as the Arduino program takes bytes from the buffer faster than they arrive that does not pose a limit.

...R

and bytes position increas to 256 in each cycle

I guess I need to read all the way to the end of the line.

However, you are still assuming that the file contains some exact multiple of 256 bytes. That is not likely to be the case very often.

Why not use a buffer size that the Arduino can handle - 64 bytes?

Serial.readnbytes( buf, n ) ;

will allegedly try to read n bytes and put them in the buffer.

According to the description of the function, it will then return a byte value containing the number of bytes read. Assuming your actual read works, good luck trying to return the number 256 in a byte variable.

But, since you don't actually collect the value returned by the readBytes( ) function call, to check whether you actually got 256 bytes, this may or may not matter.