Data Passing

This is the place for queries that don't fit in any of the other categories.

Data Passing

Postby alabview » Mon Sep 16, 2013 1:54 pm

Hello

I am new to python. I need help as below,

I need to open a file which contains a set of floating data as below,
1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0

Code: Select all
fp = open('C:\\ExamplePytoLV\\Invensys Python calling LabVIEW\\TestDocument1.txt')


Meantime, I need to pass as above set of floating data into an array,
Code: Select all
degC_array =TenDegC(fp)


But, I have an error message,
Code: Select all
degC_array  = TenDegC(fp)
TypeError: a float is required 


Please advice!Possible to share/post the code. :D

Many Thanks.
Walter.
Last edited by stranac on Mon Sep 16, 2013 2:38 pm, edited 1 time in total.
Reason: Added code tags and locked first post
alabview
 
Posts: 22
Joined: Mon Sep 16, 2013 1:48 pm

Re: Data Passing

Postby stranac » Mon Sep 16, 2013 2:37 pm

I have no idea what TenDegC is or where it comes from, but it seems to want a float as argument.
fp is clearly not a float but an open file.

If you need more help, you'll need to either show us the code for TenDegC(if you wrote it) or tell us where it comes from.

Edit: I SENT YOU A PM YESTERDAY. FUCKING READ IT.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1136
Joined: Thu Feb 07, 2013 3:42 pm

Re: Data Passing

Postby alabview » Tue Sep 17, 2013 10:09 am

Hi Stranac,

Sorry for not explain clearly.

Basically, I need to use Python to open a file which has a set of data (1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0) and pass those data into LABVIEW in DLL form.

I try to use command below to open a file and read data from file but I have no idea how to pass argument into LABVIEW.DLL. I managed to do it with constant values in degC_array.

Open a TXT file.
Code: Select all
fp = open('C:\\ExamplePytoLV\\Invensys Python calling LabVIEW\\TestDocument1.txt')
fp.read()


Display to User
Code: Select all
print ("Starting")


Declare variables
Code: Select all
from ctypes import CDLL, c_double,c_int32
print ("Starting")
N = 12
TenDegC = (c_double * N)
TenF = (c_double * N)
degC_array  = TenDegC(1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0)
F_array = TenF(0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0)
error = c_int32


Display degC_array to User
Code: Select all
for i in degC_array: print (i)
print


Load DLL into memory
Code: Select all
CtoFDLL = CDLL ("C:\\ExamplePytoLV\\Invensys Python calling LabVIEW\\CtoF.dll")
CtoFDLL.CtoF(degC_array,c_int32(N),F_array,c_int32(N))


Display Final result to User
Code: Select all
for i in F_array: print (i)
print
alabview
 
Posts: 22
Joined: Mon Sep 16, 2013 1:48 pm

Re: Data Passing

Postby stranac » Tue Sep 17, 2013 12:33 pm

First, you need to assing the result of fp.read() to a name, otherwise the rewul will be lost:
Code: Select all
thing_i_read = fp.read()

fp.read() will return the contents of the file as a single string, something like '1.0, 2.0, 3.0'.
You don't want a string, but a list of floats.

To get that, first use str.split() to split the string into a list, and then use float() on the elements of the list.
You might want to use map or a list comprehension for the last part.

I would add the links to the docs, but I'm typing this on a phone, and it's a pain.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1136
Joined: Thu Feb 07, 2013 3:42 pm

Re: Data Passing

Postby alabview » Tue Sep 17, 2013 1:24 pm

Hi Stranac,

Thanks for your information,

I have tried as you mentioned but I can't get it. Perhaps, I am new to python and make me confuse, sorry to trouble you.

Open a file.
Code: Select all
fp = open('C:\\ExamplePytoLV\\Invensys Python calling LabVIEW\\TestDocument1.txt')


Assigned the result to a name -> as you mentioned
Code: Select all
fp.read()
thing_I_read = fp.read()


Split the string -> as you mentioned
Code: Select all
str.split(thing_I_read)


Float() the list -> as you mentioned and I have no idea how to do it,follow by using map.

Would you mind to list in more details?

Really sorry.


Many Thanks.
Walter
alabview
 
Posts: 22
Joined: Mon Sep 16, 2013 1:48 pm

Re: Data Passing

Postby stranac » Tue Sep 17, 2013 2:22 pm

Code: Select all
fp.read()
thing_I_read = fp.read()

Delete the first fp.read().
The way it is now, you will end up with an empty string.

For splitting, it will be something like:
Code: Select all
parts = thing_I_read.split(',')


And finally, to convert it to a list of floats:
Code: Select all
numbers = map(float, parts)


If the function expects to be passed individual numbers, and not a list of them, you will have to unpack the list like this:
Code: Select all
TenDegC(*numbers)
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1136
Joined: Thu Feb 07, 2013 3:42 pm

Re: Data Passing

Postby alabview » Tue Sep 17, 2013 3:03 pm

Hi Stranac,

You are awesome!! I managed to do it via your instruction. Anyway, I start to learn Python and do you have any good recommendation?

In future, I might need your help again, really sorry if I keep you trouble.

MAny Thanks.
Walter.
alabview
 
Posts: 22
Joined: Mon Sep 16, 2013 1:48 pm

Re: Data Passing

Postby alabview » Tue Sep 17, 2013 3:34 pm

Hi Stranac,

I have question again.Sorry.

Now, I need to write the output data from F_array to file, so I assume I need to convert from c_double to string, izzit?if yes, how am I going to do it?

Below is my code

Open a file and write
Code: Select all
outfile = open("C:\\ExamplePytoLV\\Invensys Python calling LabVIEW\\write.txt","w")


Write to a file
Code: Select all
str(F_array)
outfile.write(F_array)


Error message appear as below
outfile.write(F_array)
TypeError: must be str, not c_double_Array_12

Please advice,

Many Thanks.
Walter
alabview
 
Posts: 22
Joined: Mon Sep 16, 2013 1:48 pm

Re: Data Passing

Postby stranac » Tue Sep 17, 2013 5:43 pm

Well, I don't know much about using c arrays with ctypes, so I'm not sure what you can do with one.

Depending on what you want as a result, a simple str(F_array) might give you a string you could use.
If c arrays can behave like other sequences in python, you can use map() once again, this time with a ','.join:
Code: Select all
outfile.write(','.join(map(str, F_array)))
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1136
Joined: Thu Feb 07, 2013 3:42 pm

Re: Data Passing

Postby Marbelous » Tue Sep 17, 2013 5:52 pm

Is there any reason you don't just read the file with LabVIEW's built-in file functions? I can see needing to hook python scripts into LabVIEW sometimes which is why we have LabPython http://labpython.sourceforge.net/ but to read an array of floats is easily done with one LabVIEW VI. Why make things so much more difficult by using a DLL???
Marbelous
 
Posts: 138
Joined: Fri May 31, 2013 8:12 pm

Re: Data Passing

Postby alabview » Wed Sep 18, 2013 8:36 am

Hi Marbelous,

I wish too, Marbelous.

But, this is company policy. Python must be a HOST and LABVIEW as a slave.Before Python there is an another app as well. headace!!!

Walter.
alabview
 
Posts: 22
Joined: Mon Sep 16, 2013 1:48 pm


Return to General Coding Help

Who is online

Users browsing this forum: Majestic-12 [Bot], snippsat and 3 guests