Re: [GLLUG] Reading USB serial port broken

2019-09-25 Thread Henrik Morsing via GLLUG

On Tue, Sep 24, 2019 at 09:16:15PM +0100, James Courtier-Dutton wrote:



[...]

Would you be able to post the python script so we can see how you are
setting up the tty and how you read/write bytes to it?

[...]

Sure, little script at the bottom. Looking through this, I have actually 
managed to solve the problem. Thanks.

I was planning to change the communication from timed to the Arduino saying "READY" and 
the Python script waiting for "READY" to proceed, but somewhere looking at this, a 
sleep() after opening the port had disappeared which meant the Python script sent its command too 
early and they both ended up waiting for each other.

I think the tail command meant the port was already open and the Arduino ready 
for a command, hance the Python/munin script was flying.

Thanks 



import sys

def output():
   received_data = "begin"
   port = serial.Serial('/dev/ttyACM0', 115200, rtscts=0, dsrdtr=True, 
timeout=5)

   port.write('munin;')
   time.sleep(1)
   received_data = port.readline()

   while received_data != "end":
   if re.match("arduino_[a-z]+_temp", received_data):
   print(received_data)
   received_data = port.readline().rstrip('\r\n')

   port.close()


def config():
   print("graph_title MVHR temperature")
   print("arduino_extract_temperature.label Extract temp")
   #print("arduino_extract_humidity.label Extract humidity")
   print("arduino_feed_temperature.label Feed temp")
   #print("arduino_feed_humidity.label Feed humidity")
   print("arduino_supply_temperature.label Supply temp")
   #print("arduino_supply_humidity.label Supply humidity")
   print("arduino_loft_temperature.label Loft temp")
   #print("arduino_loft_humidity.label Loft humidity")

try:
   rc = sys.argv[1]
except:
   rc = ""

if rc == "config":
   config()
else:
   output()



--
GLLUG mailing list
GLLUG@mailman.lug.org.uk
https://mailman.lug.org.uk/mailman/listinfo/gllug

Re: [GLLUG] Reading USB serial port broken

2019-09-24 Thread James Courtier-Dutton via GLLUG
On Fri, 20 Sep 2019 at 08:02, Henrik Morsing via GLLUG
 wrote:
>
>
> Hi,
>
> I have an Arduino connected via USB to a PC, to read sensors around my house. 
> It calls a Python script as a Munin plugin.
>
> For quite some time now, and I think this possible broke after an upgrade, 
> the Python script gets no output from ttyACM0.
>
> Unless I run a 'tail -f' on the device from the command line! Then it works. 
> Otherwise the script just times out getting nothing.
>
> I'm guessing it's down to a kernel change in how it opens or signals through 
> USB serial devices but what? Googling for a year has found nothing similar to 
> this problem. I'm not really an expert on serial devices and even less on USB 
> serial devices.
>
> It's getting really frustrating now as the monitoring just never works 
> anymore, so any help appreciated.
>

Serial programming is notoriously complicated.
I have done it in python before, doing a similar thing that you are doing.
Would you be able to post the python script so we can see how you are
setting up the tty and how you read/write bytes to it?
It is very messing writing python code that catches all possible errors.
For example, I was seeing RF noise coming from all the sensors,
occasionally knocking the USB link off/on.
My python code then had to detect this, close the tty, wait a mo for
it to settle, then open it again.
After that, in the end I dumped Python, and used a mixture of Java, C and C++.
Essentially moving from domogik (Python) to openhab (mainly Java but
with C, C++ for low level device/tty access.)

Kind Regards

James

-- 
GLLUG mailing list
GLLUG@mailman.lug.org.uk
https://mailman.lug.org.uk/mailman/listinfo/gllug

Re: [GLLUG] Reading USB serial port broken

2019-09-24 Thread Henrik Morsing via GLLUG

On Fri, Sep 20, 2019 at 03:25:30PM +0100, Dimitrios Siganos wrote:

In that case, your problem is likely to be with the DSR pin handling.
Experiment with dsrdtr flow control and with toggling the dsr pin manually.



Hi,

I'm not really any wiser. Do you mean toggle it in Python or with something 
like stty? I've tried both, neither helps.

I feel a bit stuck. 


Thanks
Henrik


--
GLLUG mailing list
GLLUG@mailman.lug.org.uk
https://mailman.lug.org.uk/mailman/listinfo/gllug

Re: [GLLUG] Reading USB serial port broken

2019-09-20 Thread Dimitrios Siganos via GLLUG
In that case, your problem is likely to be with the DSR pin handling.
Experiment with dsrdtr flow control and with toggling the dsr pin manually.


On Fri, 20 Sep 2019 at 14:40, Henrik Morsing via GLLUG <
gllug@mailman.lug.org.uk> wrote:

> On Fri, Sep 20, 2019 at 01:23:44PM +0100, Adrian McMenamin via GLLUG wrote:
> >Did permissions change on your script or on the python exec on an update?
> >Sorry, just a guess but if an ordinary user can see what's crossing the
> >port but your code can't that would seem like something to check.
>
> Hi,
>
> Munin runs as root:
>
> root  1386  0.0  0.0  24212  3560 ?Ss   Aug31   1:26
> /usr/bin/perl -wT /usr/sbin/munin-node
>
> Device is set as per recommendation:
>
> root@albert:/dev# ls -l ttyACM0
> crw-rw 1 root dialout 166, 0 Sep 20 14:32 ttyACM0
>
> Python script as follows:
>
> root@albert:/dev# ls -l /etc/munin/plugins/mvhr_temp
> lrwxrwxrwx 1 root root 41 Dec 23  2018 /etc/munin/plugins/mvhr_temp ->
> /usr/local/munin/lib/plugins/mvhr_temp.py
> root@albert:/dev# ls -l /usr/local/munin/lib/plugins/mvhr_temp.py
> -rwxr-xr-x 1 root staff 1085 Sep 20 11:51
> /usr/local/munin/lib/plugins/mvhr_temp.py
>
> And python:
>
> root@albert:/dev# ls -l $(which python2.7)
> -rwxr-xr-x 1 root root 3685224 Apr  6 02:42 /usr/bin/python2.7
>
> I run tail as root, haven't tried anything else. And the python/munin
> module can open the device, it just gets no output unless tail reads it as
> well.
>
> TBH, Arduinos reboot when something opens the port, and that is how my
> Arduino code works.
>
> I think the problem is not so much that python/munin can't read the output
> but that the Arduino doesn't output anything because it no longer gets the
> signal to reset when python opens it.
>
> Thanks
>
>
> --
> GLLUG mailing list
> GLLUG@mailman.lug.org.uk
> https://mailman.lug.org.uk/mailman/listinfo/gllug
-- 
GLLUG mailing list
GLLUG@mailman.lug.org.uk
https://mailman.lug.org.uk/mailman/listinfo/gllug

Re: [GLLUG] Reading USB serial port broken

2019-09-20 Thread Henrik Morsing via GLLUG

On Fri, Sep 20, 2019 at 01:23:44PM +0100, Adrian McMenamin via GLLUG wrote:

Did permissions change on your script or on the python exec on an update?
Sorry, just a guess but if an ordinary user can see what's crossing the
port but your code can't that would seem like something to check.


Hi,

Munin runs as root:

root  1386  0.0  0.0  24212  3560 ?Ss   Aug31   1:26 /usr/bin/perl 
-wT /usr/sbin/munin-node

Device is set as per recommendation:

root@albert:/dev# ls -l ttyACM0
crw-rw 1 root dialout 166, 0 Sep 20 14:32 ttyACM0

Python script as follows:

root@albert:/dev# ls -l /etc/munin/plugins/mvhr_temp
lrwxrwxrwx 1 root root 41 Dec 23  2018 /etc/munin/plugins/mvhr_temp -> 
/usr/local/munin/lib/plugins/mvhr_temp.py
root@albert:/dev# ls -l /usr/local/munin/lib/plugins/mvhr_temp.py
-rwxr-xr-x 1 root staff 1085 Sep 20 11:51 
/usr/local/munin/lib/plugins/mvhr_temp.py

And python:

root@albert:/dev# ls -l $(which python2.7)
-rwxr-xr-x 1 root root 3685224 Apr  6 02:42 /usr/bin/python2.7

I run tail as root, haven't tried anything else. And the python/munin module 
can open the device, it just gets no output unless tail reads it as well.

TBH, Arduinos reboot when something opens the port, and that is how my Arduino code works. 


I think the problem is not so much that python/munin can't read the output but 
that the Arduino doesn't output anything because it no longer gets the signal 
to reset when python opens it.

Thanks


--
GLLUG mailing list
GLLUG@mailman.lug.org.uk
https://mailman.lug.org.uk/mailman/listinfo/gllug

Re: [GLLUG] Reading USB serial port broken

2019-09-20 Thread Henrik Morsing via GLLUG

On Fri, Sep 20, 2019 at 01:40:17PM +0100, Dimitrios Siganos wrote:

That can be answered by:
strace tail -f ...



Doesn't seem to give anything helpful, just open and read.

Thanks

--
GLLUG mailing list
GLLUG@mailman.lug.org.uk
https://mailman.lug.org.uk/mailman/listinfo/gllug

Re: [GLLUG] Reading USB serial port broken

2019-09-20 Thread Dimitrios Siganos via GLLUG
That can be answered by:
strace tail -f ...

On Fri, 20 Sep 2019 at 13:15, Henrik Morsing via GLLUG <
gllug@mailman.lug.org.uk> wrote:

> On Fri, Sep 20, 2019 at 11:00:53AM +0100, Dimitrios Siganos wrote:
> >How do you open the serial port from python?
> >If you use pyserial, try both with hardware flow control on and off like
> >this.
> >
> >ser = serial.Serial(device, baudrate, rtscts=1)
> >and...
> >ser = serial.Serial(device, baudrate, rtscts=0)
> >
>
> Thanks, I just tried both those rtscts settings and neither helped.
>
> I'm just wondering what tail does that makes it work.
>
> Henrik
>
> --
> GLLUG mailing list
> GLLUG@mailman.lug.org.uk
> https://mailman.lug.org.uk/mailman/listinfo/gllug
-- 
GLLUG mailing list
GLLUG@mailman.lug.org.uk
https://mailman.lug.org.uk/mailman/listinfo/gllug

Re: [GLLUG] Reading USB serial port broken

2019-09-20 Thread Adrian McMenamin via GLLUG
Did permissions change on your script or on the python exec on an update?
Sorry, just a guess but if an ordinary user can see what's crossing the
port but your code can't that would seem like something to check.
-- 
GLLUG mailing list
GLLUG@mailman.lug.org.uk
https://mailman.lug.org.uk/mailman/listinfo/gllug

Re: [GLLUG] Reading USB serial port broken

2019-09-20 Thread Henrik Morsing via GLLUG

On Fri, Sep 20, 2019 at 11:00:53AM +0100, Dimitrios Siganos wrote:

How do you open the serial port from python?
If you use pyserial, try both with hardware flow control on and off like
this.

ser = serial.Serial(device, baudrate, rtscts=1)
and...
ser = serial.Serial(device, baudrate, rtscts=0)



Thanks, I just tried both those rtscts settings and neither helped.

I'm just wondering what tail does that makes it work.

Henrik  

--
GLLUG mailing list
GLLUG@mailman.lug.org.uk
https://mailman.lug.org.uk/mailman/listinfo/gllug

Re: [GLLUG] Reading USB serial port broken

2019-09-20 Thread Dimitrios Siganos via GLLUG
How do you open the serial port from python?
If you use pyserial, try both with hardware flow control on and off like
this.

ser = serial.Serial(device, baudrate, rtscts=1)
and...
ser = serial.Serial(device, baudrate, rtscts=0)

On Fri, 20 Sep 2019 at 10:31, Henrik Morsing via GLLUG <
gllug@mailman.lug.org.uk> wrote:

> On Fri, Sep 20, 2019 at 10:04:23AM +0100, Dimitrios Siganos wrote:
> >Have you checked that it is not a baud rate, flow control or other serial
> >setting issue? You can strace tail to see what it does.
> >
>
> Hi,
>
> I have checked what my limited skills allow me to, but running a tail on
> the device wouldn't really change those settings.
>
> I have tried setting this:
>
> stty -F /dev/ttyUSB0 cs8 115200 ignbrk -brkint -icrnl -imaxbel -opost
> -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke noflsh
> -ixon -crtscts
>
> that I found here:
>
> https://playground.arduino.cc/Interfacing/LinuxTTY/
>
> It's like the Python module connecting to the device doesn't set it high
> or whatever it is it normally does when connecting.
>
> Thanks
>
> --
> GLLUG mailing list
> GLLUG@mailman.lug.org.uk
> https://mailman.lug.org.uk/mailman/listinfo/gllug
-- 
GLLUG mailing list
GLLUG@mailman.lug.org.uk
https://mailman.lug.org.uk/mailman/listinfo/gllug

Re: [GLLUG] Reading USB serial port broken

2019-09-20 Thread Dimitrios Siganos via GLLUG
Have you checked that it is not a baud rate, flow control or other serial
setting issue? You can strace tail to see what it does.

On Fri, 20 Sep 2019, 08:02 Henrik Morsing via GLLUG, <
gllug@mailman.lug.org.uk> wrote:

>
> Hi,
>
> I have an Arduino connected via USB to a PC, to read sensors around my
> house. It calls a Python script as a Munin plugin.
>
> For quite some time now, and I think this possible broke after an upgrade,
> the Python script gets no output from ttyACM0.
>
> Unless I run a 'tail -f' on the device from the command line! Then it
> works. Otherwise the script just times out getting nothing.
>
> I'm guessing it's down to a kernel change in how it opens or signals
> through USB serial devices but what? Googling for a year has found nothing
> similar to this problem. I'm not really an expert on serial devices and
> even less on USB serial devices.
>
> It's getting really frustrating now as the monitoring just never works
> anymore, so any help appreciated.
>
> Thanks
> Henrik
>
> --
> GLLUG mailing list
> GLLUG@mailman.lug.org.uk
> https://mailman.lug.org.uk/mailman/listinfo/gllug
-- 
GLLUG mailing list
GLLUG@mailman.lug.org.uk
https://mailman.lug.org.uk/mailman/listinfo/gllug