BBC BASIC for Windows
« Serial port pin driving an interrupt »

Welcome Guest. Please Login or Register.
Apr 5th, 2018, 11:55pm



ATTENTION MEMBERS: Conforums will be closing it doors and discontinuing its service on April 15, 2018.
Ad-Free has been deactivated. Outstanding Ad-Free credits will be reimbursed to respective payment methods.

If you require a dump of the post on your message board, please come to the support board and request it.


Thank you Conforums members.

BBC BASIC for Windows Resources
Online BBC BASIC for Windows documentation
BBC BASIC for Windows Beginners' Tutorial
BBC BASIC Home Page
BBC BASIC on Rosetta Code
BBC BASIC discussion group
BBC BASIC for Windows Programmers' Reference

« Previous Topic | Next Topic »
Pages: 1 2  Notify Send Topic Print
 hotthread  Author  Topic: Serial port pin driving an interrupt  (Read 6303 times)
admin
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 1145
xx Re: Serial port pin driving an interrupt
« Reply #19 on: Jan 7th, 2013, 11:56am »

on Jan 7th, 2013, 09:14am, manxman wrote:
Once again, please show me how to do this.

All the information you could possibly need, and more, can be found at MSDN under the various API functions:

http://msdn.microsoft.com/en-gb/library/windows/desktop/aa363479.aspx
http://msdn.microsoft.com/en-gb/library/windows/desktop/aa363435.aspx
http://msdn.microsoft.com/en-gb/library/windows/desktop/ms687032.aspx

The BB4W structure declarations can be got from API Viewer, and the Windows Constants utility will take care of defining the constants. You can either create the event you need using CreateEvent or, more easily, use the @hevent% System Variable.

It's a technique which is new to me too, but I'm confident it is the right way to go in your application. If you get stuck with the BBC BASIC translation, just ask here again.

Richard.
User IP Logged

admin
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 1145
xx Re: Serial port pin driving an interrupt
« Reply #20 on: Jan 9th, 2013, 1:41pm »

on Jan 7th, 2013, 11:56am, Richard Russell wrote:
It's a technique which is new to me too

Although I've not got any easy way of simulating a PPS input to a modem-status line, I realised that on one of my other PCs I'm receiving a demodulated MSF signal that way:

http://www.rtrussell.co.uk/msf/msf.html

The pulse widths are very variable, but it has allowed me to demonstrate that using WaitCommEvent in conjunction with WaitForSingleObject do indeed allow me to monitor the input transitions (in my case they're on RLSD rather than DSR).

I can't easily tell whether I'm detecting the edge timings any more accurately than I would by simple polling, but I'm pretty confident that waiting in WaitForSingleObject rather than Sleep should give benefits. After all, Sleep is effectively saying to Windows: "I've got nothing to do right now, go off and do something else and return to me at your leisure" whereas WaitForSingleObject is saying "I'm very interested in this event, return control to me as soon after it happens as you can"!

Later: Moving the window around doesn't seem to affect the timing significantly.

Richard.
« Last Edit: Jan 10th, 2013, 08:18am by admin » User IP Logged

admin
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 1145
xx Re: Serial port pin driving an interrupt
« Reply #21 on: Jan 11th, 2013, 08:40am »

The lack of feedback is somewhat disconcerting. If you are no longer interested in the results of my experiments, or you have abandoned the project, please let me know so I don't waste further time on it.

This is the latest version of my 'proof of principle' program, where WaitCommEvent and associated APIs have been used in the most straightforward fashion, exactly as documented on MSDN:

Code:
      REM Monitor incoming Serial Data and a Pulse Per Second signal.
      REM By Richard Russell, http://www.rtrussell.co.uk/, 10-Jan-2013
      
      REM!WC Windows Constants:
      EV_RLSD = 32
      EV_RXCHAR = 1
      MS_RLSD_ON = &80
      HIGH_PRIORITY_CLASS = &80
      NORMAL_PRIORITY_CLASS = 32
      WAIT_TIMEOUT = 258
      
      REM Settings:
      SerialPort$ = "COM1:19200,N,8,1"
      PPS_Status% = MS_RLSD_ON
      PPS_Event% = EV_RLSD
      PPS_Edge% = FALSE
      
      REM Initialise:
      MODE 7
      *FONT BBCWin
      SYS "timeBeginPeriod", 1
      SYS "GetCurrentProcess" TO hprocess%
      SYS "SetPriorityClass", hprocess%, HIGH_PRIORITY_CLASS
      DIM overlapped{Internal%, InternalHigh%, Offset%, OffsetHigh%, hEvent%}
      
      REM Boilerplate:
      ON ERROR PROCcleanup : SYS "MessageBox", @hwnd%, REPORT$, 0, 0 : QUIT
      ON CLOSE PROCcleanup : QUIT
      
      REM Open serial port:
      port% = OPENUP(SerialPort$)
      IF port% = 0 ERROR 100, "Cannot open port"
      overlapped.hEvent% = @hevent%
      
      REM Set event(s) to be monitored:
      SYS "SetCommMask", @hfile%(port%), PPS_Event% OR EV_RXCHAR TO res%
      IF res% = 0 ERROR 100, "SetCommMask failed"
      
      REM Main loop:
      SSM% = 0
      Was% = 0
      Reply$ = ""
      REPEAT
        REM Await serial event:
        SYS "WaitCommEvent", @hfile%(port%), ^evtmask%, overlapped{} TO res%
        IF res% = 0 THEN
          REPEAT
            SYS "WaitForSingleObject", @hevent%, 2000 TO res%
          UNTIL res% <> WAIT_TIMEOUT
          SYS "GetOverlappedResult", @hfile%(port%), overlapped{}, ^temp%, 0
        ENDIF
        
        REM Process Pulse Per Second events:
        IF evtmask% AND PPS_Event% THEN
          SYS "GetCommModemStatus", @hfile%(port%), ^status%
          IF (status% AND PPS_Status%)==0 EOR PPS_Edge% THEN
            SYS "timeGetTime" TO Was%
            SSM% = (SSM% + 1) MOD 86400
          ENDIF
        ENDIF
        
        REM Process received data events:
        IF evtmask% AND EV_RXCHAR THEN
          WHILE EXT#port%
            Reply$ += CHR$BGET#port%
          ENDWHILE
          IF RIGHT$(Reply$) = CHR$10 THEN
            SYS "timeGetTime" TO Now%
            PRINT SSM% + (Now%-Was%)/1000 " " Reply$ ;
            Reply$ = ""
          ENDIF
        ENDIF
      UNTIL FALSE
      PROCcleanup
      END
      
      REM Cleanup before exit:
      DEF PROCcleanup
      LOCAL hprocess%
      SYS "GetCurrentProcess" TO hprocess%
      SYS "SetPriorityClass", hprocess%, NORMAL_PRIORITY_CLASS
      ENDPROC 

Richard.
« Last Edit: Jan 11th, 2013, 08:42am by admin » User IP Logged

manxman
Developer

member is offline

Avatar




PM

Gender: Male
Posts: 21
xx Re: Serial port pin driving an interrupt
« Reply #22 on: Jan 15th, 2013, 11:03am »

Having been away from my desk for some time I have now had a chance to evaluate your alternative solution to this timing problem.

For those new to this thread, the circa 20Hz data stream from my seismometer circuit board is input via the COM1 serial port, with 1 pulse per second TTL pulses from a GPS module also being applied to pin 6 (DSR). Since this bare circuit board does not sense ground motion, the seismic signals remain fixed at 1027 and 0 in this instance. I have changed your program to write data to file and a sample of the results of a test are:

Seconds Velocity Beam
3743.913 1027 0
3743.964 1027 0
3744.015 1027 0
3744.064 1027 0
3744.115 1027 0
3744.166 1027 0
3744.217 1027 0
3744.267 1027 0
3744.317 1027 0
3744.368 1027 0
3744.419 1027 0
3744.469 1027 0
3744.52 1027 0
3744.57 1027 0
3744.621 1027 0
3744.671 1027 0
3744.722 1027 0
3744.772 1027 0
3744.823 1027 0
3744.873 1027 0
3744.924 1027 0
3744.975 1027 0
3745.025 1027 0
3745.075 1027 0
3745.126 1027 0
3745.177 1027 0
3745.228 1027 0
3745.277 1027 0
3745.328 1027 0
3745.379 1027 0
3745.43 1027 0
3745.479 1027 0
3745.53 1027 0
3745.581 1027 0
3745.632 1027 0
3745.682 1027 0
3745.732 1027 0
3745.783 1027 0
3745.834 1027 0
3745.884 1027 0
3745.935 1027 0
3745.985 1027 0
3746.036 1027 0
3746.086 1027 0
3746.137 1027 0
3746.187 1027 0
3746.238 1027 0

Excellent! The milliseconds accumulate linearly between PPS pulses in the manner expected. I don't fully understand the workings of your code, however, since many of the instructions are unfamiliar. Where, for example, is the DSR pin assignment made?

On another tack, I have struggled to configure @% to ensure 3 decimal places, including instances when the third significant figure is zero. With @%=&20305 the number 123.45 converts to the string using STR$ as 123.450 but when this string is written to a disk file it appears as 123.45

Many thanks for you expert help with this timing topic.

Regards,

Manxman
User IP Logged

admin
Administrator
ImageImageImageImageImage


member is offline

Avatar




PM


Posts: 1145
xx Re: Serial port pin driving an interrupt
« Reply #23 on: Jan 15th, 2013, 1:10pm »

on Jan 15th, 2013, 11:03am, manxman wrote:
many of the instructions are unfamiliar

I do hope you will make an effort to understand them. The Windows APIs are used exactly as they are documented on MSDN.

Quote:
Where, for example, is the DSR pin assignment made?

Here (both the input signal PPS_Status% and the event PPS_Event% must be changed):

Code:
      PPS_Status% = MS_RLSD_ON
      PPS_Event% = EV_RLSD 

I set it to RLSD because that's where my MSF data arrives and I certainly didn't want to re-wire the plug!

Quote:
With @%=&20305 the number 123.45 converts to the string using STR$ as 123.450

I don't think that can be right. If you don't set the STR$ bit in @% STR$ should use the default G9 format, which is 123.45, as it says here:

http://www.bbcbasic.co.uk/bbcwin/manual/bbcwin6.html#strformat

"Byte 3 affects the format of the string generated by the STR$ function. If Byte 3 is 1 the string will be generated according to the format set by @%, otherwise the G9 format will be used".

Richard.
User IP Logged

manxman
Developer

member is offline

Avatar




PM

Gender: Male
Posts: 21
xx Re: Serial port pin driving an interrupt
« Reply #24 on: Jan 15th, 2013, 5:29pm »

Thanks for the clarification.

I will continue experimenting with your improved timing code which, I expect, will now form the basis of timestamping for my seismometer array data.

I'll have another look at the use of @% in order to try and force all string output to format to 3 decimal places.

Regards,

Manxman
User IP Logged

Pages: 1 2  Notify Send Topic Print
« Previous Topic | Next Topic »

| |

This forum powered for FREE by Conforums ©
Terms of Service | Privacy Policy | Conforums Support | Parental Controls