lock access to serial port

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kkadrese@gmail.com

    lock access to serial port

    hello group,

    how to get ttyS0 serial port for exclusive access? I have a python
    script that uses this device with AT commands. I need that two
    instances can call simultaneosuly this python script but only one of
    them gets the device. I tried fcntl.flock, it was just ignored, put
    writtable file LCK..ttyS0 in /var/lock, tried ioctl (but I may not
    know what are appropriate arguments), googled half a day for various
    phrases, error messages etc....without success.

    please help,

    Andra
  • taco

    #2
    Re: lock access to serial port

    kkadrese@gmail. com wrote:
    hello group,
    >
    how to get ttyS0 serial port for exclusive access? I have a python
    script that uses this device with AT commands. I need that two
    instances can call simultaneosuly this python script but only one of
    them gets the device. I tried fcntl.flock, it was just ignored, put
    writtable file LCK..ttyS0 in /var/lock, tried ioctl (but I may not
    know what are appropriate arguments), googled half a day for various
    phrases, error messages etc....without success.
    >
    please help,
    >
    Andra
    not sure if I understand you well, but how about a server application which
    accepts 2 clients which can send messages which are output on the serial
    device? The serial device access protected with a mutex while receiving
    messages done in 2 threads or something.
    taco

    Comment

    • kkadrese@gmail.com

      #3
      Re: lock access to serial port

      not the goal setup but the way I tried it out - a webpage to send an
      sms to mobile phone. This is an php what calls a python script. A
      python script opens serial device, checks for gsm network, sends a
      message - on all the way I try to make sure that appropriate answers
      are received from the gsm device so that I know that all is going well
      (and if not - cancel it with error message).

      Testing it with sending an sms from two open webpages, I normally got
      that one message was sent and for the other I received "try once more,
      happened that and that". But I got also an inadmissible situation -
      the received sms contained also unwanted text. As far as I can realize
      how to deal with it is not allowing writing to the device for two
      processes simultaneously.


      Andra


      On 19 Marts, 11:00, taco <trala...@joepi e.nlwrote:
      kkadr...@gmail. com wrote:
      hello group,
      >
      how to get ttyS0 serial port for exclusive access? I have a python
      script that uses this device with AT commands. I need that two
      instances can call simultaneosuly this python script but only one of
      them gets the device. I tried fcntl.flock, it was just ignored, put
      writtable file LCK..ttyS0 in /var/lock, tried ioctl (but I may not
      know what are appropriate arguments), googled half a day for various
      phrases, error messages etc....without success.
      >
      please help,
      >
      Andra
      >
      not sure if I understand you well, but how about a server application which
      accepts 2 clients which can send messages which are output on the serial
      device? The serial device access protected with a mutex while receiving
      messages done in 2 threads or something.
      taco

      Comment

      • Grant Edwards

        #4
        Re: lock access to serial port

        On 2008-03-18, kkadrese@gmail. com <kkadrese@gmail .comwrote:
        how to get ttyS0 serial port for exclusive access? I have a python
        script that uses this device with AT commands. I need that two
        instances can call simultaneosuly this python script but only one of
        them gets the device. I tried fcntl.flock, it was just ignored, put
        writtable file LCK..ttyS0 in /var/lock,
        Using a lock file is the traditional method of providing
        mutually exclusive access to a serial port.
        tried ioctl (but I may not know what are appropriate
        arguments), googled half a day for various phrases, error
        messages etc....without success.
        It's unclear what "without success" means. Lockfiles have been
        used for decades, and they work fine as long as all of the
        applications follow the rules.

        --
        Grant

        Comment

        • kkadrese@gmail.com

          #5
          Re: lock access to serial port

          hmm, now it seems I get to the locking
          try:
          ser = serial.Serial(d ev, 19200, rtscts=1, timeout=1)
          if ser.isOpen():
          try:
          fcntl.lockf(ser ,fcntl.LOCK_EX)
          but get into some weirdness - trying to lock does not time out! The
          script stops at this line and waits ?for ever? (it waits for 15
          minutes at least, that I have tried).

          Andra

          not the goal setup but the way I tried it out - a webpage to send an
          sms to mobile phone. This is an php what calls a python script. A
          python script opens serial device, checks for gsm network, sends a
          message - on all the way I try to make sure that appropriate answers
          are received from the gsm device so that I know that all is going well
          (and if not - cancel it with error message).
          >
          Testing it with sending an sms from two open webpages, I normally got
          that one message was sent and for the other I received "try once more,
          happened that and that". But I got also an inadmissible situation -
          the received sms contained also unwanted text. As far as I can realize
          how to deal with it is not allowing writing to the device for two
          processes simultaneously.
          >
          Andra
          >
          On 19 Marts, 11:00, taco <trala...@joepi e.nlwrote:
          >
          kkadr...@gmail. com wrote:
          hello group,
          >
          how to get ttyS0 serial port for exclusive access? I have a python
          script that uses this device with AT commands. I need that two
          instances can call simultaneosuly this python script but only one of
          them gets the device. I tried fcntl.flock, it was just ignored, put
          writtable file LCK..ttyS0 in /var/lock, tried ioctl (but I may not
          know what are appropriate arguments), googled half a day for various
          phrases, error messages etc....without success.
          >
          please help,
          >
          Andra
          >
          not sure if I understand you well, but how about a server application which
          accepts 2 clients which can send messages which are output on the serial
          device? The serial device access protected with a mutex while receiving
          messages done in 2 threads or something.
          taco

          Comment

          • Andra

            #6
            Re: lock access to serial port

            I tried ready-made commands for file locking, and turned to that LCK
            file just in case some permissions are wrong and that's the reason the
            commands fail.

            On 19 Marts, 15:43, Grant Edwards <gra...@visi.co mwrote:
            On 2008-03-18, kkadr...@gmail. com <kkadr...@gmail .comwrote:
            >
            how to get ttyS0 serial port for exclusive access? I have a python
            script that uses this device with AT commands. I need that two
            instances can call simultaneosuly this python script but only one of
            them gets the device. I tried fcntl.flock, it was just ignored, put
            writtable file LCK..ttyS0 in /var/lock,
            >
            Using a lock file is the traditional method of providing
            mutually exclusive access to a serial port.
            >
            tried ioctl (but I may not know what are appropriate
            arguments), googled half a day for various phrases, error
            messages etc....without success.
            >
            It's unclear what "without success" means.  Lockfiles have been
            used for decades, and they work fine as long as all of the
            applications follow the rules.
            >
            --
            Grant

            Comment

            Working...