Announcement

Collapse
No announcement yet.

Is there a way to have ReadFile signal when there is data to be read?

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Is there a way to have ReadFile signal when there is data to be read?

    Is there a way to have ReadFile signal when there is data to be read?

  • #2
    Dave,

    ReadFile is an API for reading data where what you need is some way to monitor the source to read from. If you tell us more about what the source is, there is probably someone here who can help you with a suggestion to get the task done.
    hutch at movsd dot com
    The MASM Forum

    www.masm32.com

    Comment


    • #3
      Hi Steve
      I am reading from a USB HID device.

      Basically I do:
      CreateFile(PChar(DevicePath), 0, FILE_SHARE_READ or FILE_SHARE_WRITE, null, OPEN_EXISTING, 0, 0 );


      Then I:
      ReadFile( OverLappedReadHandle, pReportBuffer, InputReportByteLength, BytesRead, OverlappedStructure );

      need to "register" the create file handle for callbacks..... I think!

      Comment


      • #4
        Not sure if this will help but here's the code I use

        FUNCTION CommWithUSB() AS LONG

        LOCAL lBytesWritten AS LONG
        LOCAL result AS DWORD
        LOCAL SecAttrib AS SECURITY_ATTRIBUTES
        LOCAL lBytesReturned AS LONG
        LOCAL zText AS ASCIIZ * 50

        FUNCTION = %FALSE

        SecAttrib.nLength = SIZEOF(SecAttrib)
        SecAttrib.lpSecurityDescriptor = %NULL
        SecAttrib.bInheritHandle = %TRUE
        result = WriteFile(glHidHandle, WriteBuffer(0), BYTESTOSEND, lBytesWritten, BYVAL 0)
        IF result = 0 THEN
        ConnectedStatusFlag = %FALSE
        CALL CloseUSB
        zText = "WRITE Fail - reconnecting"
        SendMessage ghWnd_Form1_Statusbar1, %SB_SETTEXT, 0, BYVAL VARPTR(zText)
        END IF

        result = ReadFile(glrHidHandle, ReadBuffer(0), BYTESTORECEIVE, lBytesReturned, HIDOverlapped)
        result = WaitForSingleObject(eventobject, 1000)
        IF result = %WAIT_OBJECT_0 THEN
        zText = (STR$(ReadBuffer(1),3)) + "," + (STR$(ReadBuffer(2),3)) + "," + (STR$(ReadBuffer(3),3)) + "," + (STR$(ReadBuffer(4),3)) + "," _
        + (STR$(ReadBuffer(5),3)) + "," + (STR$(ReadBuffer(6),3)) + "," + (STR$(ReadBuffer(7),3)) + "," + (HEX$(ReadBuffer(8),2))
        SetWindowText(ghWnd_Form1_Edit1, zText)
        FUNCTION = %TRUE
        ELSE
        ConnectedStatusFlag = %FALSE
        CALL CloseUSB
        zText = "READ Fail - reconnecting"
        SendMessage ghWnd_Form1_Statusbar1, %SB_SETTEXT, 0, BYVAL VARPTR(zText)
        END IF
        END FUNCTION

        Comment


        • #5
          Thanks James

          What is WaitForSingleObject(eventobject, 1000) waiting for?? Where does the eventobject get set?

          I am trying avoid poling the ReadFile - I am hoping to find a way that data in the buffer will cause a windows message - Heck, there may already be such a message!

          Comment


          • #6
            What is WaitForSingleObject(eventobject, 1000) waiting for?? Where does the eventobject get set?
            It's been a while since I played with this but I believe its the way that overlapped reads signal that data. I may be on thin ice here but you have to write to a device (a device in USBspeak is the other end of the string from the Host) to elist a response.

            The event object gets created in an initial routine that seeks for a particular VID/PID and if found establishes a connection.

            If you're curious I can send you the whole app (PC and device code), I didn't post it because without the hardware it's all rather pointless.

            Comment


            • #7
              Thanks James!
              You have sent me in the right direction:

              "The OVERLAPPED structure must contain a handle to a manual-reset (not an auto-reset) event object. The system sets the state of the event object to not-signaled when a call to the I/O function returns before the operation has been completed. The system sets the state of the event object to signaled when the operation has been completed. The thread uses a wait function to check the current state of the event object or to wait for its state to be signaled."

              Comment


              • #8
                Wot David said plus "....reads signal that data." in my previous post should have been "....read's signal that data is available."

                Comment


                • #9
                  Dave,

                  A change in a file, usually a check of its length is not a real big deal to do. Depending on the interval you need to check for changes, a simple timer would do the job as long as you don't need to poll for very small durations. If you checked every second, the processor load would be so small you could not calculate it, even every 100 ms would barely register. Any technique that does regular checks on some event means polling, you can get the OS to do it for you but a simple timer is easy to set up, easy to use and very efficient.
                  hutch at movsd dot com
                  The MASM Forum

                  www.masm32.com

                  Comment


                  • #10
                    Hi Steve!
                    I need this for a HID USB thing - I have overlapped working now! Although the rest of the program is not working yet!

                    Comment


                    • #11
                      Hi David.
                      Is that a regular file your working with.
                      Those watchdirectory kind of routines are very low on cpu usage
                      I have played with those kind of routines before it is a regular file or directory you are wanting to monitor for file/directory
                      activity.
                      I am not sure if the Info being received from such a program can keep up with massive activity if you don’t narrow down the scope of what you are monitoring.
                      From memory, In my testing, I was able to filter down to the write notification only as opposed to file open and close notification. For my reasons,I did not want to open the file, just monitor it. I really liked the low cpu useage of the program doing the monitoring.
                      p purvis

                      Comment


                      • #12
                        FYI
                        Doing some research on another topic.
                        I stumbled across some newer code posted by a few people on file notification kind of work
                        https://forum.powerbasic.com/forum/u...he-file-system
                        p purvis

                        Comment


                        • #13
                          Monitor directory for changes?
                          I use Larry Charlton code to monitor a server log.
                          Code:
                          Search   site:powerbasic.com filewatcher
                          Never tried on a USB drive.
                          Larry's source code
                          https://forum.powerbasic.com/forum/u...system-watcher
                          https://duckduckgo.com instead of google

                          Comment


                          • #14
                            Thanks everybody, some fine ideas here. Overlapped is what I was looking for. Lets ReadFile signal when there is data in the buffer.

                            Code:
                            Do
                            
                              ResetEvent(GLOBAL_EVENT_WAIT_FOR_OVERLAPPED_READ)             '<-- set to not-signaled
                            
                              RESULT = WaitForSingleObject(MY_OVERLAPPED.hEvent, 1000)  
                            
                              RESULT = ReadFile(BYVAL FILE_HANDLE,  READ_BUFFER, MY_InputReportByteLength , BYTES_READ, MY_OVERLAPPED)  
                            
                            Loop

                            Comment

                            Working...
                            X