read more bytes
-
Hi all!i have a read function here.I face a problem,it just can read limited byte only..Can somebody help me?Where to edit to make it read more bytes?Thank so much! public byte[] Read(int NumBytes) { byte[] BufBytes; //------Declaring of dynamic Array byte[] OutBytes; //------Declaring of dynamic Array BufBytes = new byte[NumBytes]; //------Setting the Array Size to NumBytes( From parameter ) /*-- Check Port is Open --*/ if( hComm != INVALID_HANDLE_VALUE ) { OVERLAPPED ovlCommPort = new OVERLAPPED(); //------Declaring of ovlCommport int BytesRead = 0; //------Declaring of BytesRead and set to 0 ReadFile(hComm, BufBytes, NumBytes, ref BytesRead, ref ovlCommPort); //------Get data from RS232 OutBytes = new byte[BytesRead];//------Setting the size of Array to BytesRead Array.Copy(BufBytes, OutBytes, BytesRead); //------Copy previous array data to new array data for preventing data loss return OutBytes; //------Return the GET data }/*-- End of Check Port is Open --*/ return null; //------Return null( nothing ) }/*-- End of Function Read --*/
-
Hi all!i have a read function here.I face a problem,it just can read limited byte only..Can somebody help me?Where to edit to make it read more bytes?Thank so much! public byte[] Read(int NumBytes) { byte[] BufBytes; //------Declaring of dynamic Array byte[] OutBytes; //------Declaring of dynamic Array BufBytes = new byte[NumBytes]; //------Setting the Array Size to NumBytes( From parameter ) /*-- Check Port is Open --*/ if( hComm != INVALID_HANDLE_VALUE ) { OVERLAPPED ovlCommPort = new OVERLAPPED(); //------Declaring of ovlCommport int BytesRead = 0; //------Declaring of BytesRead and set to 0 ReadFile(hComm, BufBytes, NumBytes, ref BytesRead, ref ovlCommPort); //------Get data from RS232 OutBytes = new byte[BytesRead];//------Setting the size of Array to BytesRead Array.Copy(BufBytes, OutBytes, BytesRead); //------Copy previous array data to new array data for preventing data loss return OutBytes; //------Return the GET data }/*-- End of Check Port is Open --*/ return null; //------Return null( nothing ) }/*-- End of Function Read --*/
Hello... Try something like this: public MemoryStream Read(int bufSize) { byte[] BufBytes = new byte[bufSize]; MemoryStream stream = new MemoryStream(); /*-- Check Port is Open --*/ if( hComm != INVALID_HANDLE_VALUE ) { OVERLAPPED ovlCommPort = new OVERLAPPED(); int BytesRead = 0; while(ReadFile(hComm, BufBytes, bufSize, ref BytesRead, ref ovlCommPort) != 0 /* or true (based on your interop declaration) */) stream.write(BufBytes,0,BytesRead); } return stream; } Remark: This is code is not tested (it can contains some small bugs but the base concept will work)... ;)
-
Hello... Try something like this: public MemoryStream Read(int bufSize) { byte[] BufBytes = new byte[bufSize]; MemoryStream stream = new MemoryStream(); /*-- Check Port is Open --*/ if( hComm != INVALID_HANDLE_VALUE ) { OVERLAPPED ovlCommPort = new OVERLAPPED(); int BytesRead = 0; while(ReadFile(hComm, BufBytes, bufSize, ref BytesRead, ref ovlCommPort) != 0 /* or true (based on your interop declaration) */) stream.write(BufBytes,0,BytesRead); } return stream; } Remark: This is code is not tested (it can contains some small bugs but the base concept will work)... ;)