Skip to main content

[archive] MsComm

  • March 20, 2008
  • 1 reply
  • 0 views

[Migrated content. Thread originally posted on 19 March 2008]

Hi all,
I've some problems reading data from a com port via MsComm. I use the polling method and not the event driven one. using a code like:


       working-storage section.
       78  k-id-x-com                    value 4001.
       78  k-delay                       value "0,1".
      *
       78  k-fase-received-data          value "RD".
       78  k-fase-sent-data              value "SD".

       01  x-com.
           02 x-com-log.
              03 x-com-log-active        pic x(01).
              03 x-com-log-fun           pic x(50).
              03 x-com-log-ope           pic x(50).
              03 x-com-log-param         pic x(50).
           02 x-com-object.
              03 x-com-ev.
                 04 x-com-control-id     usage signed-int.
                 04 x-com-event          usage signed-int.
              03 x-com-handle usage is handle of MSComm.
              03 x-com-v                  pic 9(03)v99.
              03 x-com-h                  pic 9(03)v99.
              03 x-com-id                 pic 9(05).
              03 x-com-config.
                 04 x-com-port            pic 9(02).
                 04 x-com-settings        pic x(250).
                 04 x-com-port-open       pic 9(01).
                 04 x-com-inp-len         usage signed-int.
              03 x-com-buf-num            pic 9(08).
              03 x-com-data.
                 04 x-com-inp             pic x(250).
                 04 x-com-out             pic x(250).
                 04 x-com-status          pic 9(02).
              03 x-com-role               pic x(01).
              03 x-com-fase               pic x(02).
      *
       01  x-com-fun.
           02 x-com-fun-status            pic x(01).
           88 x-com-fun-ok         value "S".
           02 x-com-fun-msg               pic x(250).
           02 x-com-fun-timeout           pic 9(03).

       ...
       screen section.
       01  s-mm-1
           event procedure z-99-event-form-1
           .
         02 e-tm-fl-auto
            check-box
            title l-tm-fl-auto
            line        2,20
            col         2,00
            size       30,00
            height-in-cells
            font label-font
            color ext-color-label
            id k-id-tm-fl-auto
            .
      **----------------------------------------------------------------**
      ** Risultato
      **----------------------------------------------------------------**
         02 label
            l-tm-result
            line   7,80
            col    2,00
            low
            font label-font
            color ext-color-label
            left
            .
         02 e-tm-result
            entry-field
            3-d
            line        9,00
            col         2,00
            size       40,00
            lines       1,10
            max-text   40
            auto
            left
            height-in-cells
            width-in-cells
            color ext-color-controls
            value tm-result
            id k-id-tm-result
            read-only
            .


       ...
      * the main loop code

       a-action.
           initialize x-com-object
           compute x-com-v = 1
           compute x-com-h = 1
           compute x-com-id = k-id-x-com
           move 1                        to x-com-port
           move "9600,N,8,1"             to x-com-settings
           move 0                        to x-com-inp-len
           perform x-com-fun-create
           perform until (my-exit-exception)
               accept e-tm-result before time 100
                      on exception continue
               end-accept
               if (my-exit-exception)
                  exit perform
               end-if
               perform x-com-fun-polling-receive-data
               if (x-com-fase = k-fase-received-data)
                  perform my-validation-code
                  if (fl-validate-ok = k-yes)
                     perform my-elab
                  end-if
               end-if
           end-perform
           perform x-com-fun-destroy
           .
       z-99-event-form-1.
           .
       ...

       ...
      * com port governance functions
       x-com-fun-open-port.
           move "com-open-port"              to x-com-log-fun
           initialize x-com-fun-status
           initialize x-com-fun-msg
           if (x-com-handle = 0)
              or (x-com-port = 0)
               string "Port "
                      "not ready!"  delimited size
                                into x-com-fun-msg
               exit paragraph
           end-if
           inquire x-com-handle,
                             @PortOpen in x-com-port-open
           if (x-com-port-open <> k-false)
              move k-yes      to x-com-fun-status
              exit paragraph
           end-if
           modify x-com-handle ,
                  @CommPort = x-com-port
                  @Settings = x-com-settings
                  @PortOpen = k-true
           inquire x-com-handle,
                             @PortOpen in x-com-port-open
           if (x-com-port-open <> k-false)
              move k-yes      to x-com-fun-status
              modify x-com-handle ,
                     @InputLen = x-com-inp-len
           end-if
           .
       x-com-fun-close-port.
           move "com-close-port"        to x-com-log-fun
           move k-yes                   to x-com-fun-status
           initialize x-com-fun-status
           if (x-com-handle = 0)
              or (x-com-port = 0)
              string "Port "
                      " not ready!"  delimited size
                                into x-com-fun-msg
              exit paragraph
           end-if
           inquire x-com-handle,
                               @PortOpen in x-com-port-open
           if (x-com-port-open = k-false)
              exit paragraph
           end-if
           modify x-com-handle,
                  @PortOpen = k-false
           inquire x-com-handle ,
                               @PortOpen in x-com-port-open
           if (x-com-port-open <> k-false)
              move k-no       to x-com-fun-status
           end-if
           .
       x-com-fun-send-data.
           move "com-send"             to x-com-log-fun
           initialize x-com-fun-msg
           modify x-com-handle,
                  @Output = x-com-inp
           .
       x-com-fun-polling-receive-data.
           move "com-polling-receive"  to x-com-log-fun
           initialize x-com-fun-msg
           initialize x-com-inp
           move 1                  to x-com-fun-timeout
           perform test after until (x-com-buf-num > 0)
              call "C$SLEEP" using k-delay
              if (x-com-fun-timeout > 10) |timeout
                 exit perform
              end-if
              move x-com-id to x-com-control-id
              inquire x-com-handle ,
                            @InBufferCount in x-com-buf-num
              add 1          to x-com-fun-timeout
           end-perform
           if (x-com-buf-num > 0)
              perform x-com-fun-receive-data
              move k-fase-received-data to x-com-fase
           end-if
           .
       x-com-fun-receive-data.
           move "com-receive"      to x-com-log-fun
           initialize x-com-fun-msg
           initialize x-com-fun-status
           |leggo la risposta del dispositivo collegato
           initialize x-com-inp
           inquire x-com-handle,
                         @Input in x-com-inp
           .
       x-com-fun-create.
           move "com-create"       to x-com-log-fun
           initialize x-com-fun-msg
           initialize x-com-fun-status
           move k-yes              to x-com-fun-status
           if (x-com-handle = zero)
              display  MsComm,
                line x-com-v
                col x-com-h
                enabled 0,
                visible 0,
                id is x-com-id
                handle in x-com-handle
              end-display
           end-if
           initialize x-com-data
      *    perform x-com-fun-init
           .
       x-com-fun-destroy.
           move "com-destroy"      to x-com-log-fun
           initialize x-com-fun-status
           initialize x-com-fun-msg
           perform x-com-fun-close-port
           if (x-com-handle  <> zero)
              destroy x-com-handle
              initialize x-com-object
           end-if
           .


Everything wents ok till x-com-buf-num is > 0
then launching x-com-fun-receive-data the inquire of the @Input retrieves a x-com-inp = spaces even if x-com-buf-num was = 20.
Any help will be appreciated.
TiA Giovanni.

1 reply

[Migrated content. Thread originally posted on 19 March 2008]

Hi all,
I've some problems reading data from a com port via MsComm. I use the polling method and not the event driven one. using a code like:


       working-storage section.
       78  k-id-x-com                    value 4001.
       78  k-delay                       value "0,1".
      *
       78  k-fase-received-data          value "RD".
       78  k-fase-sent-data              value "SD".

       01  x-com.
           02 x-com-log.
              03 x-com-log-active        pic x(01).
              03 x-com-log-fun           pic x(50).
              03 x-com-log-ope           pic x(50).
              03 x-com-log-param         pic x(50).
           02 x-com-object.
              03 x-com-ev.
                 04 x-com-control-id     usage signed-int.
                 04 x-com-event          usage signed-int.
              03 x-com-handle usage is handle of MSComm.
              03 x-com-v                  pic 9(03)v99.
              03 x-com-h                  pic 9(03)v99.
              03 x-com-id                 pic 9(05).
              03 x-com-config.
                 04 x-com-port            pic 9(02).
                 04 x-com-settings        pic x(250).
                 04 x-com-port-open       pic 9(01).
                 04 x-com-inp-len         usage signed-int.
              03 x-com-buf-num            pic 9(08).
              03 x-com-data.
                 04 x-com-inp             pic x(250).
                 04 x-com-out             pic x(250).
                 04 x-com-status          pic 9(02).
              03 x-com-role               pic x(01).
              03 x-com-fase               pic x(02).
      *
       01  x-com-fun.
           02 x-com-fun-status            pic x(01).
           88 x-com-fun-ok         value "S".
           02 x-com-fun-msg               pic x(250).
           02 x-com-fun-timeout           pic 9(03).

       ...
       screen section.
       01  s-mm-1
           event procedure z-99-event-form-1
           .
         02 e-tm-fl-auto
            check-box
            title l-tm-fl-auto
            line        2,20
            col         2,00
            size       30,00
            height-in-cells
            font label-font
            color ext-color-label
            id k-id-tm-fl-auto
            .
      **----------------------------------------------------------------**
      ** Risultato
      **----------------------------------------------------------------**
         02 label
            l-tm-result
            line   7,80
            col    2,00
            low
            font label-font
            color ext-color-label
            left
            .
         02 e-tm-result
            entry-field
            3-d
            line        9,00
            col         2,00
            size       40,00
            lines       1,10
            max-text   40
            auto
            left
            height-in-cells
            width-in-cells
            color ext-color-controls
            value tm-result
            id k-id-tm-result
            read-only
            .


       ...
      * the main loop code

       a-action.
           initialize x-com-object
           compute x-com-v = 1
           compute x-com-h = 1
           compute x-com-id = k-id-x-com
           move 1                        to x-com-port
           move "9600,N,8,1"             to x-com-settings
           move 0                        to x-com-inp-len
           perform x-com-fun-create
           perform until (my-exit-exception)
               accept e-tm-result before time 100
                      on exception continue
               end-accept
               if (my-exit-exception)
                  exit perform
               end-if
               perform x-com-fun-polling-receive-data
               if (x-com-fase = k-fase-received-data)
                  perform my-validation-code
                  if (fl-validate-ok = k-yes)
                     perform my-elab
                  end-if
               end-if
           end-perform
           perform x-com-fun-destroy
           .
       z-99-event-form-1.
           .
       ...

       ...
      * com port governance functions
       x-com-fun-open-port.
           move "com-open-port"              to x-com-log-fun
           initialize x-com-fun-status
           initialize x-com-fun-msg
           if (x-com-handle = 0)
              or (x-com-port = 0)
               string "Port "
                      "not ready!"  delimited size
                                into x-com-fun-msg
               exit paragraph
           end-if
           inquire x-com-handle,
                             @PortOpen in x-com-port-open
           if (x-com-port-open <> k-false)
              move k-yes      to x-com-fun-status
              exit paragraph
           end-if
           modify x-com-handle ,
                  @CommPort = x-com-port
                  @Settings = x-com-settings
                  @PortOpen = k-true
           inquire x-com-handle,
                             @PortOpen in x-com-port-open
           if (x-com-port-open <> k-false)
              move k-yes      to x-com-fun-status
              modify x-com-handle ,
                     @InputLen = x-com-inp-len
           end-if
           .
       x-com-fun-close-port.
           move "com-close-port"        to x-com-log-fun
           move k-yes                   to x-com-fun-status
           initialize x-com-fun-status
           if (x-com-handle = 0)
              or (x-com-port = 0)
              string "Port "
                      " not ready!"  delimited size
                                into x-com-fun-msg
              exit paragraph
           end-if
           inquire x-com-handle,
                               @PortOpen in x-com-port-open
           if (x-com-port-open = k-false)
              exit paragraph
           end-if
           modify x-com-handle,
                  @PortOpen = k-false
           inquire x-com-handle ,
                               @PortOpen in x-com-port-open
           if (x-com-port-open <> k-false)
              move k-no       to x-com-fun-status
           end-if
           .
       x-com-fun-send-data.
           move "com-send"             to x-com-log-fun
           initialize x-com-fun-msg
           modify x-com-handle,
                  @Output = x-com-inp
           .
       x-com-fun-polling-receive-data.
           move "com-polling-receive"  to x-com-log-fun
           initialize x-com-fun-msg
           initialize x-com-inp
           move 1                  to x-com-fun-timeout
           perform test after until (x-com-buf-num > 0)
              call "C$SLEEP" using k-delay
              if (x-com-fun-timeout > 10) |timeout
                 exit perform
              end-if
              move x-com-id to x-com-control-id
              inquire x-com-handle ,
                            @InBufferCount in x-com-buf-num
              add 1          to x-com-fun-timeout
           end-perform
           if (x-com-buf-num > 0)
              perform x-com-fun-receive-data
              move k-fase-received-data to x-com-fase
           end-if
           .
       x-com-fun-receive-data.
           move "com-receive"      to x-com-log-fun
           initialize x-com-fun-msg
           initialize x-com-fun-status
           |leggo la risposta del dispositivo collegato
           initialize x-com-inp
           inquire x-com-handle,
                         @Input in x-com-inp
           .
       x-com-fun-create.
           move "com-create"       to x-com-log-fun
           initialize x-com-fun-msg
           initialize x-com-fun-status
           move k-yes              to x-com-fun-status
           if (x-com-handle = zero)
              display  MsComm,
                line x-com-v
                col x-com-h
                enabled 0,
                visible 0,
                id is x-com-id
                handle in x-com-handle
              end-display
           end-if
           initialize x-com-data
      *    perform x-com-fun-init
           .
       x-com-fun-destroy.
           move "com-destroy"      to x-com-log-fun
           initialize x-com-fun-status
           initialize x-com-fun-msg
           perform x-com-fun-close-port
           if (x-com-handle  <> zero)
              destroy x-com-handle
              initialize x-com-object
           end-if
           .


Everything wents ok till x-com-buf-num is > 0
then launching x-com-fun-receive-data the inquire of the @Input retrieves a x-com-inp = spaces even if x-com-buf-num was = 20.
Any help will be appreciated.
TiA Giovanni.
Hello all, problem solved: it was due to a mismatch in speed transfer between how the device connected to the port was setted and how the MsComm32 @PortSetting was filled. Setting the port to the right speed @Input started to flow correctly.
bye Gio