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?
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

izrunas

USA
3 Posts

Posted - 03/11/2014 :  07:57:11  Show Profile  Reply with Quote
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!


Reply #1

njc

USA
16 Posts

Posted - 03/11/2014 :  08:51:30  Show Profile  Reply with Quote
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

Go to Top of Page
Reply #2

izrunas

USA
3 Posts

Posted - 03/11/2014 :  09:07:30  Show Profile  Reply with Quote
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!
Go to Top of Page
Reply #3

njc

USA
16 Posts

Posted - 03/11/2014 :  09:48:03  Show Profile  Reply with Quote
try adding:

import struct

to your module imports
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
GQ Electronics Technical Support Forum © Copyright since 2011 Go To Top Of Page
Generated in 0.08 sec. Snitz's Forums 2000