前提・実現したいこと
python2では以下のプログラムでシリアル通信ができていて、データを取ることが出来ていたのですが、python3に変更するとエラーが出て、データが取れません。
発生している問題・エラーメッセージ
serial.serialutil.SerialException: could not open port 'COM4': PermissionError(13, 'アクセスが拒否されました。' , None, 5)
該当のソースコード
import serial import time PORT1 = 'COM4' com1 = serial.Serial(PORT1, timeout = 0.1) raw_acc = [0, 0, 0] raw_gyro = [0, 0, 0] raw_comp = [0, 0, 0] for i in range(30): while True: if com1.read() == 'B': data = com1.read(17) node_no = int(data[0].encode('hex'), 16) >> 3 time_stamp = int(data[0].encode('hex'), 16) << 12 & 0x7000 | \ int(data[1].encode('hex'), 16) << 4 & 0x0ff0 | \ int(data[2].encode('hex'), 16) >> 4 & 0x000f raw_acc[0] = int(data[2].encode('hex'), 16) << 8 & 0x0f00 | \ int(data[3].encode('hex'), 16) raw_acc[1] = int(data[4].encode('hex'), 16) << 4 | \ int(data[5].encode('hex'), 16) >> 4 & 0x0f raw_acc[2] = int(data[5].encode('hex'), 16) << 8 & 0x0f00 | \ int(data[6].encode('hex'), 16) raw_gyro[0] = int(data[7].encode('hex'), 16) << 4 | \ int(data[8].encode('hex'), 16) >> 4 & 0x0f raw_gyro[1] = int(data[8].encode('hex'), 16) << 8 & 0x0f00 | \ int(data[9].encode('hex'), 16) raw_gyro[2] = int(data[10].encode('hex'), 16) << 4 | \ int(data[11].encode('hex'), 16) >> 4 & 0x0f raw_comp[0] = int(data[11].encode('hex'), 16) << 8 & 0x0f00 | \ int(data[12].encode('hex'), 16) raw_comp[1] = int(data[13].encode('hex'), 16) << 4 | \ int(data[14].encode('hex'), 16) >> 4 & 0x0f raw_comp[2] = int(data[14].encode('hex'), 16) << 8 & 0x0f00 | \ int(data[15].encode('hex'), 16) print (str(raw_acc[0]) + ' [m/ss?]') break else: time.sleep(0.001) print ('fin') com1.close()
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。