diy solar

diy solar

Anyone know Python?

No.15

That's my number
Joined
Jun 29, 2020
Messages
123
And is willing to help me with a script? I am trying to test batteries with a DC load bank that understands some scpi.

I know nothing about programming and put together a script that I mostly copied from magnaload. Obviously it does not work, but it connects to the load and opened an empty graph at the end. What I am looking to do is capacity test batteries. Once I get it right I will offer capacity testing for free for anyone in my area.


remember don't laugh this is from someone who knows nothing about python

Python:
# Import plotting library Matplotlib
# Import .csv parser
# Import socket
# Import time: Time access and conversions to allow for pausing
# Import numpy for mathemetical manipulation of arrays
import matplotlib.pyplot as plt
import csv, socket, time
import numpy as np

# Create connection object
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('192.168.10.103', 3000))
# Create an empty numpy data array 999 rows, 4 columns
outputSamples = np.empty([999, 3])
iSample = 0

# Send SCPI commands to set the LOAD
s.sendall(b'MODe:CURRENT\n')
s.sendall(b'ISET 2\n')
s.sendall (b'VOLTage:PROTection:UNDer:STATe ON\n')
s.sendall (b'VOLT:PROT:UND 12.8\n')
# Send SCPI command to enable the DC input
s.sendall(b'INPUT ON\n')

# Open data stream to .csv file. .csv has two columns:
# Column 1: Current set point in amperes
# Column 2: Time in seconds
# The first row is headers
with open('example_profile.csv', 'r') as csvfile:
    # Read the .csv file using comma (,) as the delimeter
    dataset = csv.reader(csvfile, delimiter=',')
    # Skip the header row
    next(dataset)
    # Split the dataset up to rows
    rows = list(dataset)
    # Create an empty array for measurements, the same length as .csv row numbers
    inputSamples = np.empty([len(rows), 2], dtype=float)
    # Record the time that the measurements began
    testStartTime = time.time()
    # Run for loop while there are still rows of data left
    for idx, data in enumerate(rows):
        # Store row data to array
        inputSamples[idx] = [data[0], data[1]]
        # Send SCPI command to LOAD to current set point at present row
        s.sendall(b'CURR 1\n'.format(data[0]))
        # Determine how long the MagnaLOAD should stay at this current set point
        stopTime = testStartTime + int(data[1])
        # Run while loop while there is still time left
        while time.time() < stopTime:
            # Send SCPI command to MagnaLOAD to measure all DC input variables
            s.sendall('MEAS:CURRENT?\n')
            # Get the LOAD's response and split it up into its respective variables
            [curr, volt, res, pwr] = (s.readline()).split(',')
            # Round measurements and store them to array, along with with time
            outputSamples[iSample] = ([round(float(curr), 2), round(float(volt), 2), round(time.time() - testStartTime, 2)])
            iSample += 1
            time.sleep(0.5)

# Send SCPI command to disable DC input
s.sendall(b'INPUT OFF\n')

# Create a plot series of current vs. time
plt.subplot(2, 1, 1)
plt.plot(outputSamples[0:iSample, 2], outputSamples[0:iSample, 0], 'r--')
plt.ylabel('Output Current(A)')
plt.title('I-V Profile')

# Create a plot series of voltage vs. time
plt.subplot(2, 1, 2)
plt.plot(outputSamples[0:iSample, 2], outputSamples[0:iSample, 1], 'b--')
plt.xlabel('Time (s)')
plt.ylabel('Output Voltage(V)')

# Show the plot
plt.show()
 
well , usually this kind of program is heavily related to the hardware, so even if the code is good, you need to make sure the parameters are ok, especially if you pick some code that was written for another hardware.
If there are real programming errors, the python interpeter should notify for syntax errors, invalid arguments or invalid commands.
That is easy to fix.
What is less easy is to fix the logic or the program if you do not really knows how it works.
in that case the best is to write a routine to print on screen the values you expect to read, to check if they are really here.
If you expect some value and you get a 0 instead, then you can dig into the instruction that is supposed to return this value.
here your problem could not be into python, but in scpi command.
 
The code does run but without the hardware to respond there's not a lot more to say. I have played with python and at a glance it looks OK, but coming from me that's not saying much.

Python is a multipass interpreter so syntax errors and other blunders will usually be discovered as soon as you try to run it. Soft variable typing can be a problem so you have to be careful with comparisons, math etc.
 
Yes it runs, also the scpi commands are correct for my hardware. Problem is I have zero output, just blank graphs
 
If you put some print commands in where you read the data in are you getting any values back?
After this statement
[curr, volt, res, pwr] = (s.readline()).split(',')
 
If you put some print commands in where you read the data in are you getting any values back?
After this statement
[curr, volt, res, pwr] = (s.readline()).split(',')
I'll give it a try, thanks
 
Back
Top