GQ Electronics Technical Support Forum Active Users: / Visits Today:
Highest Active Users:
GQ Electronics Technical Support Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 GQ Electronics Forums
 2.GQ Geiger Muller Counter
 Convert <GETSERIAL>> to INT in Python?

Note: You must be registered in order to post a reply.
To register, click here. Registration is FREE!

Screensize:
UserName:
Password:
Format Mode:
Format: BoldItalicizedUnderlineStrikethrough Align LeftCenteredAlign Right Horizontal Rule Insert HyperlinkInsert EmailInsert Image Insert CodeInsert QuoteInsert List Spell Checker
   
Message:

* HTML is OFF
* Forum Code is ON
Smilies
Smile [:)] Big Smile [:D] Cool [8D] Blush [:I]
Tongue [:P] Evil [):] Wink [;)] Clown [:o)]
Black Eye [B)] Eight Ball [8] Frown [:(] Shy [8)]
Shocked [:0] Angry [:(!] Dead [xx(] Sleepy [|)]
Kisses [:X] Approve [^] Disapprove [V] Question [?]

   Insert an Image File
Check here to include your profile signature.
    

T O P I C    R E V I E W
izrunas Posted - 03/11/2014 : 07:57:11
Hi,

I want to retrieve the serial number via Python and I am not sure how to convert the "bytes" response to something readable... Here's a piece from the Python script I am using:

def initCommunication(self):

        print "Initializing GMC protocol communication"
        # get firmware version
        response=self.sendCommand("<GETVER>>")

        if len(response)>0:
            print "Found GMC-compatible device, version: ", response
            # get serial number
            serialnum=self.sendCommand("<GETSERIAL>>")
            print "Device Serial Number is: ", serialnum
            # disable heartbeat, we will request data from script
            self.sendCommand("<HEARTBEAT0>>")
            print "Please note data will be acquired once per 5 seconds"

        else:
            print "No response from device"
            self.stop()
            sys.exit(1)


The problem is that the serial returns a byte value, not an integer one. I know that to get the CPM reading as an integer, I use this routine:

# send request
            response=self.sendCommand("<GETCPM>>")

            if len(response)==2:
                # convert bytes to 16 bit int
                cpm=ord(response[0])*256+ord(response[1])
            else:
                print "Unknown response to CPM request, device is not GMC-compatible?"
                self.stop()
                sys.exit(1)


But obviously the same conversion will not work in the case of the serial number. Would someone point me in the right direction? Thanks!


3   L A T E S T    R E P L I E S    (Newest First)
njc Posted - 03/11/2014 : 09:48:03
try adding:

import struct

to your module imports
izrunas Posted - 03/11/2014 : 09:07:30
When I tried this:
# get serial number
            serialnum=self.sendCommand("<GETSERIAL>>")
            serialnum.int=struct.unpack('!1H', serialnum(7))[0]
            print "Device Serial Number is: ", serialnum.int

I got this error:
Initializing GMC protocol communication
Found GMC-compatible device, version:  GMC-320Re 3.18
Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python2.7/threading.py", line 808, in __bootstrap_inner
    self.run()
  File "pyradmon.py", line 170, in run
    self.initCommunication()
  File "pyradmon.py", line 344, in initCommunication
    serialnum.int=struct.unpack('!1H', serialnum(7))[0]
NameError: global name 'struct' is not defined

I assume I am using the command wrong, though it looks "ok" from the doc page you gave me...?

Thanks!
njc Posted - 03/11/2014 : 08:51:30
Maybe try something this:

struct.unpack('!1H', ser.read(2))[0]

I use that to read the CPM from the GMC-300 (2 bytes) and it reverses the order (MSB/LSB) as is required. I honestly do not remember why I have the '[0]' there. See here for more doc:
h**p://docs.python.org/2/library/struct.html


GQ Electronics Technical Support Forum © Copyright since 2011 Go To Top Of Page
Generated in 0.05 sec. Snitz's Forums 2000