Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. read more bytes

read more bytes

Scheduled Pinned Locked Moved C#
helpdata-structuresquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    levis
    wrote on last edited by
    #1

    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 --*/

    H 1 Reply Last reply
    0
    • L levis

      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 --*/

      H Offline
      H Offline
      HumanOsc
      wrote on last edited by
      #2

      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)... ;)

      L 1 Reply Last reply
      0
      • H HumanOsc

        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)... ;)

        L Offline
        L Offline
        levis
        wrote on last edited by
        #3

        Hi!Thanks!This code no problem but how am i going to call it to my Main,please?Thanks again!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups