python code for serialport

others protocols
Post Reply
Tron
Posts: 5
Joined: Sun Aug 18, 2019 3:03 pm

python code for serialport

Post by Tron » Sun Aug 18, 2019 3:14 pm

Serialport via PHP is broken by design.
(PHP dont support iocontrols)

Fix in python

Greetings

---------
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import serial, time, sys
#-------------------------------------------------------------------

SerialPort = "/dev/ttyUSB0"
SerialBaudrate = 2400

#-------------------------------------------------------------------

# serial docu: http://pyserial.sourceforge.net/pyserial.html

#-------------------------------------------------------------------

def SerialAuslesen(serObject):
while True:
try:
# Remove newline character '\n'

serObject.write('QPIGS\xb7\xa9\x0d')
#response = serObject.readline().strip()
response = serObject.readline()
if response:
#print("Serial-> "+str(response)),
# ...do something with it...

f = open("/tmp/rs232daten.txt","w")
f.write(str(response))
f.close()

except serial.SerialException as e:
print("Could not read serial port '{}': {}".format(SerialPort, e))
except (KeyboardInterrupt, SystemExit):
sys.exit()
serObject.close()

#-------------------------------------------------------------------

def main():
#initialization and open the port.
#possible timeout values:
# 1. None: wait forever, block call
# 2. 0: non-blocking mode, return immediately
# 3. x, x is bigger than 0, float allowed, timeout block call
ser = serial.Serial()
ser.port = SerialPort
ser.baudrate = SerialBaudrate
ser.bytesize = serial.EIGHTBITS #number of bits per bytes
ser.parity = serial.PARITY_NONE #set parity check: no parity
ser.stopbits = serial.STOPBITS_ONE #number of stop bits
#ser.timeout = None #block read
ser.timeout = 1 #non-block read
#ser.timeout = 2 #timeout block read
ser.xonxoff = False #disable software flow control
ser.rtscts = False #disable hardware (RTS/CTS) flow control
ser.dsrdtr = False #disable hardware (DSR/DTR) flow control
ser.writeTimeout = 2 #timeout for write
try:
ser.open()
except Exception as e:
print("Error open serial port: " + str(e))
sys.exit()
if ser.isOpen():
try:
ser.flushInput() #flush input buffer, discarding all its contents
ser.flushOutput() #flush output buffer, aborting current output and discard all that is in buffer

SerialAuslesen(ser)
except Exception as e:
print("Error...: " + str(e))
except (KeyboardInterrupt, SystemExit):
print("\nSchliesse Programm..\n")
if ser: ser.close()
sys.exit()

if __name__ == '__main__':
main()

jeanmarc
Posts: 1833
Joined: Thu Aug 29, 2013 7:16 am

Re: python code for serialport

Post by jeanmarc » Mon Aug 19, 2019 8:22 am

Hi,
I don't get the sense of this message ;)

Tron
Posts: 5
Joined: Sun Aug 18, 2019 3:03 pm

Re: python code for serialport

Post by Tron » Mon Sep 30, 2019 6:40 pm

> Serialport via PHP is broken by design.
> (PHP dont support iocontrols)

https://fr.wikipedia.org/wiki/Ioctl
https://en.wikipedia.org/wiki/Ioctl

?
// $fp = fopen("/dev/ttyUSB0", "w+"); //xxxx serialport com1 com2 ...

// $byte = fgetc($fp);


both will hung "sometimes", and if... than forever.

Greetings
Tron

jeanmarc
Posts: 1833
Joined: Thu Aug 29, 2013 7:16 am

Re: python code for serialport

Post by jeanmarc » Mon Sep 30, 2019 7:24 pm

Hi tron,
I still don't understand, what do you wanna do ?

Tron
Posts: 5
Joined: Sun Aug 18, 2019 3:03 pm

Re: python code for serialport

Post by Tron » Sun Oct 06, 2019 11:32 pm

Greetings

I have heavy modified the effecta code from project to work with
ISolarSMV II 3kW Inverter
connected to Raspberry via USB <--> RS - Serialport.

Image

// $fp = fopen("/dev/ttyUSB0", "w+"); //xxxx serialport com1 com2 ...
// $byte = fgetc($fp);

and this php don't work stabil.

I have sended the python workaround... (works stabil nonstop since 6 weeks)
i think (?) modpoll is an equivalent in compiled-code.


The ISolarSMV II 3kW Inverter supports isle - mode... means: it can load / unload an DC accumulator and transform 230V~ AC.
Currently i am adding accu-status / loadlevel to the Gfx... but i am very busy ....

Can you me explain what values Grid-Ph1 exact "means"?

Image

6 Values and and they dont match together. :o
But i check this php-code. :D

One leek is here in the Inverter-Data. if accus loading then this "energy" is not "explicit" in the Data, (i have to calculate this von Accu )
The monitoring of the accus is main-goal here.

Is there a place where i can upload my changes? in the next 1-2 months?


Greeting and thanks for you work
Tron


--

This python code fetches the Datarecord from Inverter and wrote it to /tmp than can php read it from there.

/tmp is mounted as tmpfs (RAM-Disk, no SD-Card)

#vi /etc/fstab
tmpfs /tmp tmpfs rw,nosuid,nodev 0 0

jeanmarc
Posts: 1833
Joined: Thu Aug 29, 2013 7:16 am

Re: python code for serialport

Post by jeanmarc » Mon Oct 07, 2019 7:51 am

Hi,
It's now a bit clearer for me ;) I can add new protocols files if needed.
Grid-Ph1 is the grid values (AC), Power, Voltage and Current on phase 1.
You can put your app in http://sourceforge.net, https://github.com, ..

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest