浜松ホトニクスc12880maをRaspberry Pi Pico、MicroPythonで使う(4)

PCに接続されたc12880maを認識するクラスを導入してみた。一応、動作を確認したが、現状、その確認はLinuxのみなのである。

そもそも、Windowsマシンを持っていない。正確には会社支給のPCがWindowsなのだ。しかも、シンクライアントだし、Pythonのインストール許可をとっていない。以前はAnacondaのインストール許可をもらっていたのであるが…

さて、どの様に動作確認をしたら良いのだろう。あ、嘘ついた…
Windowsマシンをもっている。CNC用とレーザーカッター用に3台、Windows PCが存在するのである。すっかり存在を忘れていた。
これらでテストをしてみるか。サポート切れてるWindows 7なのでネットワークを切っている。ちょっと面倒くさいな。exe化を試してみるかな。

さて、シリアルポートに接続を試み、接続できたら所属を問い合わせをして’Spectrometer’が帰ってきたポートを使うように変更した。正確にはc12880maが接続されたRaspberry Pi Picoへの問い合わせである。

当然ながら、Raspberry Pi Pico側には今回のコマンドに対応する回答が必要なのであるが、これについては随分前に用意していたのでそれを利用した。

import platform
import serial.tools.list_ports
import time

class ident_PC():

    def __init__(self):

        self.OS = platform.system()
        self.Version = platform.version()
        self.Platform = platform.platform()
        self.uart_message = "Initialize class"

    def scan(self):

        self.ports = serial.tools.list_ports.comports()
        
        for p in self.ports:

                print(": %s" % p)
                print("device: %s" % p.device)
                print("name: %s" % p.name)
                print("description: %s" % p.description)
                print("hwid: %s" % p.hwid)
                print("vid: %s" % p.vid)
                print("pid: %s" % p.pid)
                print("serial Number: %s" % p.serial_number)
                print("location: %s" % p.location)
                print("manufacturer: %s" % p.manufacturer)
                print("product: %s" % p.product)
                print("interface: %s" % p.interface)
    
    def chk_c12880ma(self):
        
        self.ports = serial.tools.list_ports.comports()
        
        for p in self.ports:
            if p.manufacturer == "MicroPython":
                try:
                    self.uartport = serial.Serial(
                        port=p.device,
                        baudrate=115200,
                        bytesize=serial.EIGHTBITS,
                        parity=serial.PARITY_NONE,
                        stopbits=serial.STOPBITS_ONE,
                        timeout=None
                        )
                    
                    # buffer clear 
                    self.uartport.reset_input_buffer()
                    self.uartport.reset_output_buffer()
                    self.uartport.write(("identify" + "\n").encode("utf-8"))
                    
                    try:
                        data = self.uartport.readline()
                        if data.decode('utf-8').rstrip('\r\n') == 'Spectrometer':
                            self.uart_message = "Successful connection to c12880ma on %s \"%s\"." % (self.OS, p.device)
                            return()
                        else:
                            self.uartport.close()
                    except serial.SerialException:
                        self.uart_message = "Failed to receive."
       
                except serial.SerialException:
                    self.uart_message = "Failed to open serial port."
                
        self.uart_message = "not found c12880ma."
        
if __name__ == '__main__':

    pc = ident_PC()
    pc.chk_c12880ma()

    if pc.uart_message == "not found c12880ma.":
        print(pc.uart_message)
    else:
        print(pc.uart_message)
        pc.uartport.close()

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

CAPTCHA