-See (1)
--Serial communication with devices with RS232C interface using pySerial
--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
--From conda prompt
conda install -c anaconda pyserial
It is assumed to be executed in Jupyter Notebook. (It should be the same for python scripts)
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