I want to improve efficiency with Python even in an experimental system (2) RS232C and pySerial

Motivation

-See (1)

environment

Target

--Serial communication with devices with RS232C interface using pySerial

Insert pySerial

Why put

--Many measuring instruments are equipped with RS232C interface and command control by serial communication. --If you have pyserial and a command control mechanism, you should be able to basically control it. --For details, please see Official Site

How to put

--From conda prompt

conda install -c anaconda pyserial

How to use

It is assumed to be executed in Jupyter Notebook. (It should be the same for python scripts)

Example

First, import the package. Please note that it is ** different from the package name **.

import serial
import time

Connect your device to your PC by converting DSub to USB and connecting. For Windows, look for something like COM (number) in Device Manager. If the device labeled COM3 is the corresponding device

COM="COM3"
bitRate=9600
ser = serial.Serial(COM, bitRate, timeout=0.1)

BitRate is your choice. Serial communication is possible with this, so after that, look at the command collection of each measuring instrument

ser.write(b"H:2-\r\n")

For example, enter Sigma Kouki SHOT102 Command on the second axis. On the other hand, it is a command to return to the origin.)

--\ r \ n is a newline character. For more information [here](https://ja.stackoverflow.com/questions/12897/%E6%94%B9%E8%A1%8C%E3%81%AE-n%E3%81%A8-rn%E3 Please see% 81% AE% E9% 81% 95% E3% 81% 84% E3% 81% AF% E4% BD% 95% E3% 81% A7% E3% 81% 99% E3% 81% 8B) ..

-B in () means that it is a character string. If you do not write b, you can only send one character.

# time.sleep(0.1)
print(ser.read_all())

Depending on the device used, read () may return whether the instruction was successful.

--read () One character --read_line () One line --read_all () All

It seems that the character string returned by is readable. When running the old code at the same time, it may fail if you don't wait a few seconds, such as time.sleep (0.1).

ser.close()

Don't forget to end the serial communication after the instruction.

that's all. Thank you very much. In the next article, I will write the code that runs continuously, puts it in a list, makes it a data frame with pandas, and spits it in csv.

Recommended Posts

I want to improve efficiency with Python even in an experimental system (2) RS232C and pySerial
I want to improve efficiency with Python even in an experimental system (1) Install Anaconda with Chocolatey
I want to improve efficiency with Python even in an experimental system (3) I want to do something like Excel with Pandas
I want to improve efficiency with Python even in an experimental system (4) Use ser.close () when an error is thrown using try syntax
I want to improve efficiency with Python even in the experimental system (5) I want to send a notification at the end of the experiment with the slack API
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
I want to handle optimization with python and cplex
Even in JavaScript, I want to see Python `range ()`!
I want to work with a robot in python.
I want to debug with Python
I want to run Rails with rails s even in vagrant environment
I tried to improve the efficiency of daily work with Python
I want to analyze logs with Python
I want to play with aws with python
Environment maintenance made with Docker (I want to post-process GrADS in Python
I want to color a part of an Excel string in Python
I want to do Dunnett's test in Python
I want to use MATLAB feval with python
I want to create a window in Python
I want to make a game with Python
I want to be an OREMO with setParam!
I want to merge nested dicts in Python
I want to use Temporary Directory with Python2
#Unresolved I want to compile gobject-introspection with Python3
I want to solve APG4b with Python (Chapter 2)
I want to write to a file with Python
I want to display the progress in Python!
[Python] I want to know the variables in the function when an error occurs!
I want to send Gmail with Python, but I can't because of an error
I want to write an element to a file with numpy and check it.
I want to get the file name, line number, and function name in Python 3.4
I want to write in Python! (1) Code format check
I want to convert an image to WebP with lollipop
Even beginners want to say "I fully understand Python"
I want to embed a variable in a Python string
I want to easily implement a timeout in python
I want to write in Python! (2) Let's write a test
I want to randomly sample a file in Python
I was addicted to scraping with Selenium (+ Python) in 2020
I tried to implement an artificial perceptron with python
I want to inherit to the back with python dataclass
I want to write in Python! (3) Utilize the mock
I want to AWS Lambda with Python on Mac!
[ML Ops] I want to do multi-project with Python
I want to use the R dataset in python
I want to run a quantum computer with Python
I want to do something in Python when I finish
I want to manipulate strings in Kotlin like Python!
[Introduction to system trading] I drew a Stochastic Oscillator with python and played with it ♬
I want to replace the variables in the python template file and mass-produce it in another file.
I want to write a triple loop and conditional branch in one line in python
I want to get an error message in Japanese with django Password Change Form
What to do if you run python in IntelliJ and end with an error
I want to be able to analyze data with Python (Part 3)
I want to specify another version of Python with pyvenv
Python and hardware-Using RS232C with Python-
How to swap elements in an array in Python, and how to reverse an array.
I want to be able to analyze data with Python (Part 1)
I want to do something like sort uniq in Python
I got an error in vim and zsh in Python 3.7 series
I want to be able to analyze data with Python (Part 2)