{"id":3330,"date":"2021-06-12T09:59:41","date_gmt":"2021-06-12T16:59:41","guid":{"rendered":"https:\/\/www.rbdinstruments.com\/blog\/?p=3330"},"modified":"2021-06-12T10:22:08","modified_gmt":"2021-06-12T17:22:08","slug":"programming-the-9103-with-python-part-1-standard-speed","status":"publish","type":"post","link":"https:\/\/www.rbdinstruments.com\/blog\/programming-the-9103-with-python-part-1-standard-speed\/","title":{"rendered":"Programming the 9103 With Python &#8211; Part 1: Standard Speed"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">New <a href=\"https:\/\/rbdinstruments.com\/products\/picoammeter\/tech.html#resources\">sample Python co<\/a><a rel=\"noreferrer noopener\" aria-label=\"de for the 9103 (opens in a new tab)\" href=\"https:\/\/rbdinstruments.com\/products\/picoammeter\/tech.html#resources\" target=\"_blank\">de for the 9103<\/a> is now available on RBD&#8217;s website. The 9103&#8217;s API is easy to program with any language and<a href=\"https:\/\/rbdinstruments.com\/products\/picoammeter\/tech.html#resources\"> <\/a><a href=\"https:\/\/rbdinstruments.com\/products\/picoammeter\/tech.html#resources\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"documented (opens in a new tab)\">documented<\/a> fully, but as always with any device programming, a few examples always help. We chose Python because it&#8217;s well supported, easy to understand even if you&#8217;re using another language and compatible with a number of popular lab control and analysis environments.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The sample code we provide is open-source and free for you to use and distribute as you see fit.<br><br><em>(If you haven&#8217;t taken a look at the 9103 USB auto-ranging picoammeter, everything you need to know can be <a href=\"https:\/\/rbdinstruments.com\/products\/picoammeter.html\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"found here (opens in a new tab)\">found here<\/a>. Measure bi-polar DC current from picoamps to milliamps, with optional built-in bias and 5 kV isolation.)<\/em><br><br> <\/p>\n\n\n\n<figure class=\"wp-block-image\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"3331\" data-permalink=\"https:\/\/www.rbdinstruments.com\/blog\/programming-the-9103-with-python-part-1-standard-speed\/9103-python-1\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.rbdinstruments.com\/blog\/wp-content\/uploads\/2021\/06\/9103-python-1.png?fit=2200%2C1400&amp;ssl=1\" data-orig-size=\"2200,1400\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"9103-python-1\" data-image-description=\"&lt;p&gt;RBD 9103 usb picoammeter&lt;\/p&gt;\n\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.rbdinstruments.com\/blog\/wp-content\/uploads\/2021\/06\/9103-python-1.png?fit=584%2C372&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/www.rbdinstruments.com\/blog\/wp-content\/plugins\/jquery-image-lazy-loading\/images\/grey.gif?w=584&#038;ssl=1\" data-original=\"https:\/\/i0.wp.com\/www.rbdinstruments.com\/blog\/wp-content\/uploads\/2021\/06\/9103-python-1.png?fit=584%2C372&amp;ssl=1\" alt=\"Programming the 9103 with Python\" class=\"lazy wp-image-3331\"\/><noscript><img loading=\"lazy\" decoding=\"async\" width=\"2200\" height=\"1400\" data-attachment-id=\"3331\" data-permalink=\"https:\/\/www.rbdinstruments.com\/blog\/programming-the-9103-with-python-part-1-standard-speed\/9103-python-1\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.rbdinstruments.com\/blog\/wp-content\/uploads\/2021\/06\/9103-python-1.png?fit=2200%2C1400&amp;ssl=1\" data-orig-size=\"2200,1400\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"9103-python-1\" data-image-description=\"&lt;p&gt;RBD 9103 usb picoammeter&lt;\/p&gt;\n\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/www.rbdinstruments.com\/blog\/wp-content\/uploads\/2021\/06\/9103-python-1.png?fit=584%2C372&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/www.rbdinstruments.com\/blog\/wp-content\/uploads\/2021\/06\/9103-python-1.png?fit=584%2C372&amp;ssl=1\" alt=\"Programming the 9103 with Python\" class=\"wp-image-3331\" srcset=\"https:\/\/i0.wp.com\/www.rbdinstruments.com\/blog\/wp-content\/uploads\/2021\/06\/9103-python-1.png?w=2200&amp;ssl=1 2200w, https:\/\/i0.wp.com\/www.rbdinstruments.com\/blog\/wp-content\/uploads\/2021\/06\/9103-python-1.png?resize=300%2C191&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.rbdinstruments.com\/blog\/wp-content\/uploads\/2021\/06\/9103-python-1.png?resize=768%2C489&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.rbdinstruments.com\/blog\/wp-content\/uploads\/2021\/06\/9103-python-1.png?resize=1024%2C652&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.rbdinstruments.com\/blog\/wp-content\/uploads\/2021\/06\/9103-python-1.png?resize=471%2C300&amp;ssl=1 471w, https:\/\/i0.wp.com\/www.rbdinstruments.com\/blog\/wp-content\/uploads\/2021\/06\/9103-python-1.png?w=1168&amp;ssl=1 1168w, https:\/\/i0.wp.com\/www.rbdinstruments.com\/blog\/wp-content\/uploads\/2021\/06\/9103-python-1.png?w=1752&amp;ssl=1 1752w\" sizes=\"auto, (max-width: 584px) 100vw, 584px\" \/><\/noscript><\/figure>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\"><strong>A Quick Look at the Standard Speed Sampling Code<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Python&#8217;s pySerial module abstracts the calls to class-compliant USB serial COM port drivers, so setting up communications with the 9103 is a matter of a few simple calls:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>port=serial.Serial(\n    'com' + port_number,\n    baudrate=57600,\n    bytesize=serial.EIGHTBITS,\n    parity=serial.PARITY_NONE,\n    stopbits=serial.STOPBITS_ONE,\n    xonxoff=False,\n    timeout=1)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">With the addition of a few helper functions for handling ASCII, it&#8217;s simple to write functions to send individual commands to the 9103. The following sets the auto-range and filter settings:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>def message(message_string):<br>     return bytes('&amp;'+ message_string + '\\n','utf-8')<br><br> def command_range_auto():<br>     port.write(message('R0')) # put in autorange <br><br> def command_default_filter_32():<br>     port.write(message('F032')) <\/code><br><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Similarly, with the addition of a few helper functions, the 9103 samples can be read with a simple loop that reads lines from the port buffer: In this case, we&#8217;ve setup the 9103 to read in interval mode, and poll the port for incoming samples, with a simple keyboard interrupt. This code also writes the sample information to a text file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>try:<br>     while True:<br>         msg=port.readline().decode('utf-8').rstrip()<br>         print(msg)<br>         parsed = parse_message_for_sample(msg)<br>         if parsed:<br>             samplefile.write( parsed + '\\n')<br><br> except KeyboardInterrupt:<br>     pass <\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"> All of the code is commented and documented in the Python file. <\/p>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\"><strong>Using Python with LabView and MATLAB<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The Python code samples can be helpful to both LabView and MATLAB users.<br>LabVIEW now supports Python through the <a rel=\"noreferrer noopener\" aria-label=\"Python Node (opens in a new tab)\" href=\"https:\/\/zone.ni.com\/reference\/en-XX\/help\/371361R-01\/glang\/python_node\/\" target=\"_blank\">Python Node<\/a>, which features low-latency calls from a LabVIEW Block Diagram using  LabVIEW primitives. <br><br>The <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/matlab-engine-for-python.html\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\" (opens in a new tab)\">MATLAB Engine API for Python<\/a> allows you to call MATLAB as a computational engine from Python, and you can call functions and objects in Python from MATLAB.<\/p>\n\n\n\n<p class=\"has-medium-font-size wp-block-paragraph\"><strong>More to Come\u2026<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Next up we&#8217;ll be posting some sample code to write and read using the high-speed version of the 9103. Since high-speed data from the 9103 arrives in packets, there are some minor differences in parsing the message samples. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>New sample Python code for the 9103 is now available on RBD&#8217;s website. The 9103&#8217;s API is easy to program with any language and documented fully, but as always with any device programming, a few examples always help. We chose &hellip; <a href=\"https:\/\/www.rbdinstruments.com\/blog\/programming-the-9103-with-python-part-1-standard-speed\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":11,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[159,163],"tags":[116,123,371],"class_list":["post-3330","post","type-post","status-publish","format-standard","hentry","category-9103-usb-picoammeter","category-software-for-surface-analysis","tag-116","tag-programming","tag-python"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2DEXo-RI","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/www.rbdinstruments.com\/blog\/wp-json\/wp\/v2\/posts\/3330","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rbdinstruments.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rbdinstruments.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rbdinstruments.com\/blog\/wp-json\/wp\/v2\/users\/11"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rbdinstruments.com\/blog\/wp-json\/wp\/v2\/comments?post=3330"}],"version-history":[{"count":2,"href":"https:\/\/www.rbdinstruments.com\/blog\/wp-json\/wp\/v2\/posts\/3330\/revisions"}],"predecessor-version":[{"id":3339,"href":"https:\/\/www.rbdinstruments.com\/blog\/wp-json\/wp\/v2\/posts\/3330\/revisions\/3339"}],"wp:attachment":[{"href":"https:\/\/www.rbdinstruments.com\/blog\/wp-json\/wp\/v2\/media?parent=3330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rbdinstruments.com\/blog\/wp-json\/wp\/v2\/categories?post=3330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rbdinstruments.com\/blog\/wp-json\/wp\/v2\/tags?post=3330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}