CINF / PyExpLabSys

Python for Experimental Lab Systems: Serial drivers, file parsers, data and live sockets
GNU General Public License v3.0
74 stars 33 forks source link

TypeError of IP address #61

Open DangerLin opened 1 year ago

DangerLin commented 1 year ago

Hi there! When I tried to use the bio_logic module to control my potentiostat (SP-150e), it returned an error message saying TypeError. The script I used is from the test example of the package. And I am quite sure I have typed the IP address correctly as a string. Could anyone help me with this issue? Thanks a lot!

Traceback (most recent call last): File "D:\Scripts\test.py", line 310, in <module> test_cv_technique() File "D:\Scripts\test.py", line 166, in test_cv_technique sp150.connect() File "D:\Anaconda\envs\test_env_EClab\lib\site-packages\PyExpLabSys\drivers\bio_logic.py", line 260, in connect address = create_string_buffer(self.address) File "D:\Anaconda\envs\test_env_EClab\lib\ctypes\__init__.py", line 66, in create_string_buffer raise TypeError(init) TypeError: 192.168.72.01

KennethNielsen commented 1 year ago

Hi @DangerLin

I missed this in the inbox before vacation. If this is still a problem, can you share the code you use to connect?

JustAnEmployee commented 1 year ago

Hi @KennethNielsen I've got the same issue with this issue.

C:\Users\wan376\AppData\Local\Programs\Python\Python311\python.exe C:/Users/wan376/PycharmProjects/faster2/Potentiostat/test_potentiostat.py
Traceback (most recent call last):
  File "C:\Users\wan376\PycharmProjects\faster2\Potentiostat\test_potentiostat.py", line 57, in <module>
    run_ocv()
  File "C:\Users\wan376\PycharmProjects\faster2\Potentiostat\test_potentiostat.py", line 12, in run_ocv
    sp150.connect()
  File "C:\Users\wan376\PycharmProjects\faster2\Potentiostat\bio_logic_driver.py", line 262, in connect
    address = create_string_buffer(self.address)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\wan376\AppData\Local\Programs\Python\Python311\Lib\ctypes\__init__.py", line 66, in create_string_buffer
    raise TypeError(init)
TypeError: 192.168.0.257

The code I ran was a copy paste of the example code from read the docs.

def run_ocv():
    """Test the OCV technique"""
    ip_address = '192.168.0.257'  # REPLACE THIS WITH A VALID IP
    # ip_address = 'USB0'
    # Instantiate the instrument and connect to it
    sp150 = SP150(ip_address)
    sp150.connect()

    # Instantiate the technique. Make sure to give values for all the
    # arguments where the default values does not fit your purpose. The
    # default values can be viewed in the API documentation for the
    # technique.
    ocv = OCV(rest_time_T=0.2,
              record_every_dE=10.0,
              record_every_dT=0.01)

    # Load the technique onto channel 0 of the potentiostat and start it
    sp150.load_technique(0, ocv)
    sp150.start_channel(0)

The point at which the code failed was

# Communications functions
    def connect(self, timeout=5):
        address = create_string_buffer(self.address)

I'm running python 3.11.4 in a windows environment.

DangerLin commented 1 year ago

Hi @DangerLin

I missed this in the inbox before vacation. If this is still a problem, can you share the code you use to connect?

Hi Kenneth, my code is attached below:

from __future__ import print_function
from pprint import pprint
import time
from PyExpLabSys.drivers.bio_logic import SP150, CV

ip_address = '169.254.72.33'

#data save path
save_path = 'D:\Data\EC lab\Danger\20230527\epx_1.csv' # file name is to be defined.

def test_cv_technique():
    """Test the CV technique"""
    import matplotlib.pyplot as plt
    sp150 = SP150(ip_address)
    sp150.connect()
    cv_ = CV(vs_initial=(True,) * 5,
             voltage_step=(0.0, 0.5, -0.7, 0.0, 0.0),
             scan_rate=(100.0,) * 2,
             record_every_dE=0.01,
             N_cycles=1)
    sp150.load_technique(0, cv_)

    sp150.start_channel(0)
    ew_ = []
    ii_ = []
    run_time = []
    try:
        while True:
            time.sleep(0.1)
            data_out = sp150.get_data(0)
            if data_out is None:
                break
            print(data_out.technique)
            print('Ewe:', data_out.Ewe)
            print('I:', data_out.I)
            print('time/s', data_out.time)
            ew_ += data_out.Ewe
            ii_ += data_out.I
            run_time += data_out.time
            print('cycle:', data_out.cycle)
    except KeyboardInterrupt:
        sp150.stop_channel(0)
        sp150.disconnect()
    else:
        sp150.stop_channel(0)
        sp150.disconnect()
    plt.plot(ew_, ii_)
    plt.show()
    print('end')

    CV.save_data(save_path)

if __name__ == '__main__':
    test_cv_technique()
DangerLin commented 1 year ago

JustAnEmployee

Hi, sir, if you have a solution, could you please let me know? Thank you!

KennethNielsen commented 11 months ago

Sorry, for the slow answer time. This seems to be a signature that changed over time, maybe after the whole Python2/3 debacle. The create_string_buffer function now expects bytes not str. So, if that is the only place where the IP-address is used, you could make a workaround by converting your IP-address to bytes before sending it into the class. Something like:

ip_address = "192.168.1.2".encode("ascii")

if this works, I will make a fix around it.