Developing a GUI application

Discuss Sugar, upgrades and developer perspectives.

Re: Developing a GUI application

Postby cuprum » Wed Apr 15, 2009 4:17 am

This code did not work as I expected. It should have had two bytes of padding to run on the 32-bit architecture:
Code: Select all
d=struct.pack("ihxx",a,b)

cuprum wrote:
Code: Select all
import struct
a= 65
b= -10

# an integer and a short
d=struct.pack("ih",a,b)
cuprum
 
Posts: 87
Joined: Fri Mar 28, 2008 5:34 am

Re: Developing a GUI application

Postby yuvvrajsingh » Wed Apr 15, 2009 5:04 am

I dont have a clue about what u are trying to explain ?
Why use struct ??
What is the code u uploaded supposed to do ?
Any new ideas on how to write to the hiddev0 ?? My time is running out .....
ONe thing i know for sure is unless the Report ID value is 00 , the write will not work ......
Thats probably why"20" dint work out earlier...
BUt the format in which i need to send the data is still unknown

I NEED TO SEND

total 3 BYTES in write operation

REPORT ID | eeprom location | value

Report Id is always 00

What about rest ...and is dumping to HIDDEV0 just enough ?????

Pls Enlighten
yuvvrajsingh
 
Posts: 54
Joined: Sun Jan 25, 2009 3:30 am

Re: Developing a GUI application

Postby cuprum » Wed Apr 15, 2009 6:05 am

yuvvrajsingh wrote:I dont have a clue about what u are trying to explain ?
Why use struct ??

Linux is written in C, so it uses C structs in many places to store data. For example, ektoric has posted the structure you are reading from the thermometer. You can use python's struct.unpack to turn this into variables you can read from python.
yuvvrajsingh wrote:What is the code u uploaded supposed to do ?
Any new ideas on how to write to the hiddev0 ?? My time is running out .....

What do you mean by this? I hope this isn't a school project, because the teacher will know you got help from outside. No beginning student would succeed at something this hard.
cuprum
 
Posts: 87
Joined: Fri Mar 28, 2008 5:34 am

Re: Developing a GUI application

Postby yuvvrajsingh » Wed Apr 15, 2009 6:19 am

I hope this isn't a school project, because the teacher will know you got help from outside. No beginning student would succeed at something this hard.


Im an undergraduate seeking information on a project where the faculties I know have limited knowledge in this area.I did not find enough material on the internet( my search would not have been perfec..t i agree).
So im here to seek information/data from knowledgeable people.Nothin Less or nothin more !!!

Ok ill try on the struct ...and let u know
yuvvrajsingh
 
Posts: 54
Joined: Sun Jan 25, 2009 3:30 am

Re: Developing a GUI application

Postby cuprum » Wed Apr 15, 2009 6:43 am

yuvvrajsingh wrote:I dont have a clue about what u are trying to explain ?
Why use struct ??
What is the code u uploaded supposed to do ?
Any new ideas on how to write to the hiddev0 ?

This, as ektoric and I have pointed out, may not be possible. We just do not know. The correct system call is ioctl(),and although you can call it from python, you need to at least be able to read C so you can do the translation.
yuvvrajsingh wrote: My time is running out .....
ONe thing i know for sure is unless the Report ID value is 00 , the write will not work ......

yuvvrajsingh wrote:Thats probably why"20" dint work out earlier...
BUt the format in which i need to send the data is still unknown

Exactly correct. What the USB calls a "report" is just gibberish to me. I would imagine it is a struct of some kind, and that you call ioctl() with that struct as one of the function arguments. Normally, you call open(), and instead of calling read or write, you call ioctl().

Whether writing the same struct to the hiddev0 file works is anyone's guess.
cuprum
 
Posts: 87
Joined: Fri Mar 28, 2008 5:34 am

Re: Developing a GUI application

Postby yuvvrajsingh » Wed Apr 15, 2009 10:04 am

hi cuprum & ektoric
I have smell something fishy.
I noticed this thing when i was using read
ie, when the HID device was in 'READ' mode , i used the following command instead of hexdump
and i got the following output

Code: Select all
>>> f=open('/dev/usb/hiddev0',"r")
>>> f.readline()
'\x01\x00\x00\xfff\x00\x00\x00\x02\x00\x00\xff\x81\xff\xff\xff\x03\x00\x00\xff\n'


Pretty similar to the struct method cuprum was mentioning 'BYTES'.

Code: Select all
>>> f.readline()
'\x00\x00\x00\x04\x00\x00\xff\xf0\xff\xff\xff\x05\x00\x00\xff\x88\xff\xff\xff\x06\x00\x00\xff\x88\xff\xff\xff\x07\x00\x00\xff*\x00\x00\x00\x08\x00\x00\xff\xd4\xff\xff\xff\x08\x00\x00\xff\x10\x00\x00\x00\x08\x00\x00\xff\x1c\x00\x00\x00\x08\x00\x00\xff\x01\x00\x00\x00\x08\x00\x00\xff\x84\xff\xff\xff\x08\x00\x00\xff\xa1\xff\xff\xff\x08\x00\x00\xff\x10\x00\x00\x00\x08\x00\x00\xff!\x00\x00\x00\x08\x00\x00\xff\xd1\xff\xff\xff\x08\x00\x00\xff\x00\x00\x00\x00\x08\x00\x00\xff\x9f\xff\xff\xff\x08\x00\x00\xff_\x00\x00\x00\x08\x00\x00\xff4\x00\x00\x00\x08\x00\x00\xff\xcd\xff\xff\xff\x08\x00\x00\xff1\x00\x00\x00\x08\x00\x00\xff$\x00\x00\x00\x08\x00\x00\xff\n'
>>>


You can see that when i give the same command, (in ordinary sense ,it is to read the next line...but i dont know what the hiddev0 interprets it as) I get the above output,i dont know what it means but it seems similar to CUPRUM's struct approach

i found the site below

http://docs.python.org/library/struct.h ... .pack_into

CUPRUM as mentioned earlier said about packing....
i want to pack 3 bytes and send it to the hiddev0
so i tried

Code: Select all
import struct
a= 00
b= 20
c= 10
d=struct.pack("ihxx",a,b,c)

BUT I GOT ERROR
[/code]

root@nevinalex-myworld:/home/nevinalex/Desktop/python# python nevin3.py
Traceback (most recent call last):
File "nevin3.py", line 5, in <module>
d=struct.pack("ihxx",a,b,c)
File "/usr/lib/python2.5/struct.py", line 63, in pack
return o.pack(*args)
struct.error: pack requires exactly 2 arguments


what is the solution to pack the three items up??
Also i found from the site above
struct.pack_into(fmt, buffer, offset, v1, v2, ...)

Pack the values v1, v2, ... according to the given format, write the packed bytes into the writable buffer starting at offset. Note that the offset is a required argument.

Is the readable buffer in this case the hiddev0 file ?? if yes what offset?
yuvvrajsingh
 
Posts: 54
Joined: Sun Jan 25, 2009 3:30 am

Re: Developing a GUI application

Postby yuvvrajsingh » Wed Apr 15, 2009 10:20 am

This is another fundamental problem regarding teh linux portioN v/s WINDOWS counterpart

The microcontroller was prgrammed to send the ADC values of teh sensors along with the channel number from whcih they are coming,Tried and tested in XP.The values read in XP where of the format
RD 00 "00 2B 02" "01 21 02" 02 17 02 03 0D 02 04 01 02 05 F6 01 06 EA 01 07 DD 01 08 D1 01 09 C4 01 0A B7 01 0B A9 01 0C 9C 01 0D 8F 01 0E 81 01 0F 74 01 10 67 01 11 5A 01 12 4E 01 13 42 01


The first 2 entries have been quoted to explain the format.first quote refers to channel 00 ----> 2B LOWER BYTE 02 HIGHER BYTE ,second quote refers to channel 01 ----> 21 LOWER BYTE 02 HIGER BYTE.....And so on.......

So if if write a python script to process the data based on the channel numbers and corresponding higher and lower its pretty simple in XP.....

But when it comes to linux the scenario changes
when i use hexdump i get the following output ....

00004910 08 00 00 ff 67 00 00 00 08 00 00 ff 97 ff ff ff |....g...........|


from which i can see neither the channel number ..nor im not sure which are my lower and higher bits????

How do i proceed or debug ( there is no error ..i know that.. but i cant find my values either)
is it because im using hexdump ???

IS There anyway out of this problem
yuvvrajsingh
 
Posts: 54
Joined: Sun Jan 25, 2009 3:30 am

Re: Developing a GUI application

Postby ektoric » Wed Apr 15, 2009 12:08 pm

yuvvrajsingh wrote:i want to pack 3 bytes and send it to the hiddev0
so i tried

Code: Select all
import struct
a= 00
b= 20
c= 10
d=struct.pack("ihxx",a,b,c)

BUT I GOT ERROR
Code: Select all
root@nevinalex-myworld:/home/nevinalex/Desktop/python# python nevin3.py
Traceback (most recent call last):
  File "nevin3.py", line 5, in <module>
    d=struct.pack("ihxx",a,b,c)
  File "/usr/lib/python2.5/struct.py", line 63, in pack
    return o.pack(*args)
struct.error: pack requires exactly 2 arguments


To quote the python documentation, http://www.python.org/doc/2.5.2/lib/module-struct.html
Python Doc: struct.pack() wrote:...The arguments must match the values required by the format exactly.

So we look at the format specifier: "ihxx".
i = integer
h = short
x = pad (no value)
x = pad (no value)
So we are indeed telling pack() that we are sending it 2 and exactly 2 arguments to pack(). It if give it any more or any less, it would be correct for pack() to complain.
ektoric
Support Team
 
Posts: 470
Joined: Wed Jan 02, 2008 3:46 pm
Location: Austin, TX

Re: Developing a GUI application

Postby ektoric » Wed Apr 15, 2009 12:15 pm

Freemor wrote:For this you will need to find someone with more Python experience then I. I have no direct experience with pyUSB. Sorry

Perhaps someone else here??

For this you will need to find someone with more USB driver experience than I. I have no experience with USB driver development. Sorry.

Perhaps someone else here??

(Or the mailing list and IRC channel I suggested in a previous post where linux USB developers hang out?)
ektoric
Support Team
 
Posts: 470
Joined: Wed Jan 02, 2008 3:46 pm
Location: Austin, TX

Re: Developing a GUI application

Postby yuvvrajsingh » Wed Apr 15, 2009 1:19 pm

So we look at the format specifier: "ihxx".
i = integer
h = short
x = pad (no value)
x = pad (no value)
So we are indeed telling pack() that we are sending it 2 and exactly 2 arguments to pack(). It if give it any more or any less, it would be correct for pack() to complain.


But i tried the other one too with one more 'x' ,but still error

Code: Select all
import struct
a= 00
b= 20
c= 10
d=struct.pack("ihxxx",a,b,c)



Code: Select all
root@nevinalex-myworld:/home/nevinalex/Desktop/python# python nevin3.py
Traceback (most recent call last):
  File "nevin3.py", line 5, in <module>
    d=struct.pack("ihxxx",a,b,c)
  File "/usr/lib/python2.5/struct.py", line 63, in pack
    return o.pack(*args)
struct.error: pack requires exactly 2 arguments
yuvvrajsingh
 
Posts: 54
Joined: Sun Jan 25, 2009 3:30 am

Re: Developing a GUI application

Postby cuprum » Wed Apr 15, 2009 8:44 pm

Sorry to be brief, but I am in a hurry and network is slow.

You need to understand that format for struct.pack: "i" creates 4 bytes from a number, "h" creates 2, and "x" does not use up any variables, so it won't help to add more.

I'm looking at hiddev.c, and I wonder if python's ioctl() can even cope with stuff like:

Code: Select all
HIDIOCGUSAGES      _IOWR('H', 0x13, struct hiddev_usage_ref_multi)


According to http://wiki.maemo.org/Programming_FM_radio, it does not. Anyone know?
cuprum
 
Posts: 87
Joined: Fri Mar 28, 2008 5:34 am

Re: Developing a GUI application

Postby ektoric » Wed Apr 15, 2009 11:58 pm

No idea, have yet to use http://docs.python.org/library/fcntl.html

On the other hand, one could write Python-C bindings where C libraries made low level calls and extrapolated to high level functions.
http://libhid.alioth.debian.org/
ektoric
Support Team
 
Posts: 470
Joined: Wed Jan 02, 2008 3:46 pm
Location: Austin, TX

Re: Developing a GUI application

Postby yuvvrajsingh » Thu Apr 16, 2009 1:10 am

You need to understand that format for struct.pack: "i" creates 4 bytes from a number, "h" creates 2, and "x" does not use up any variables, so it won't help to add more.


If x does not use up any variables why give it in the command??? "ihxx"

Can you illustrate with an example ...The idea appears a little vague to me
yuvvrajsingh
 
Posts: 54
Joined: Sun Jan 25, 2009 3:30 am

Re: Developing a GUI application

Postby cuprum » Thu Apr 16, 2009 4:37 am

yuvvrajsingh wrote:
If x does not use up any variables why give it in the command??? "ihxx"

Can you illustrate with an example ...The idea appears a little vague to me


I needed them to make the example work with my compiler.
Code: Select all
struct example {
   int a
   short b
};


When I wrote that struct, it was 8 bytes. 4 bytes for the int, 2 for the short int, and two bytes of padding. I am not sure this matters for you.
cuprum
 
Posts: 87
Joined: Fri Mar 28, 2008 5:34 am

Re: Developing a GUI application

Postby ektoric » Thu Apr 16, 2009 11:38 am

yuvvrajsingh wrote:If x does not use up any variables why give it in the command??? "ihxx"
Can you illustrate with an example ...The idea appears a little vague to me

The origional code (with sample values) was:
Code: Select all
a=45
b=17
d=struct.pack("ihxx",a,b)

Parsing the pack()'s format specifier, it says to take the first variable, and stuff it in an "i" space. In a 32-bit architecture (which the XO is), the value of "a" of 45 becomes the following in hex: "00 00 00 2d". Also, since we are in an ix86 architecture, we store values in Little Endian: "2d 00 00 00"

Continuing to parse, the next format specifier is "h", so we take the next variable and stuff it in space specified by h. h is a "short", which is 2 bytes, and b in two bytes in hex is: "00 11". Again, in little endian, the representation becomes "11 00".

Continuing to parse, the next format specifier is "x", so we do not take any parameters, and pad with one byte of 0. In hex: "00"

Continuing to parse, the next format specifier is "x", so we do not take any parameters, and pad with one byte of 0. In hex: "00"

This ends our parsing, so the end result that is stored in d is:
Code: Select all
# 2d 00 00 00   11 00   00   00
d = '\x2d\x00\x00\x00\x11\x00\x00\x00'

Notice that this is an 8 byte string.
ektoric
Support Team
 
Posts: 470
Joined: Wed Jan 02, 2008 3:46 pm
Location: Austin, TX

PreviousNext

Return to Sugar, Linux and System Software

Who is online

Users browsing this forum: No registered users and 2 guests

cron