为什么我的串行监视器 python 脚本不显示秤的重量?

问题描述 投票:0回答:0

我使用 RS-232 转 USB 转换器将旧秤 (Mettler Toledo SG 32001) 连接到我的 PC。确认读数有效,因为我已经为不同的比例编写了一个程序,并且在使用具有以下设置的程序 YAT 时建立了连接:

端口:COM3 每秒比特数:2400 数据位:7 奇偶性:偶数 停止位:1 流量控制:硬件(RFR/CTS)

现在在我的 python 脚本中,我写了以下代码:

import serial
import time
from datetime import datetime

from serial import SEVENBITS, PARITY_EVEN, STOPBITS_ONE

print('Starting Connection')

with serial.Serial() as ser:
    ser.baudrate = 2400
    ser.port = 'COM3'

    #ser.open()
    ser.bytesize = SEVENBITS
    ser.stopbits = STOPBITS_ONE
    ser.rtscts = True
    ser.stopbits = 1
    ser.timeout = 2
    #ser.dsrdtr = True
    ser.parity = PARITY_EVEN
    ser.open()
    print('Starting Serial Monitor\n')

if(ser.isOpen() == False):
    ser.open()


print(f'dsr:  {ser.dsr}')
print(f'cts:  {ser.cts}')
print(f'dtr:  {ser.dtr}')
print(f'rts:  {ser.rts}')
print(f'cd:  {ser.cd}') # data carrier detect ist false
print(f'ri:  {ser.ri}') # kein klingelndes Telefon haha


target = open("file.txt","a")
#Add command to clear a file here

#t = time.localtime()
date = datetime.utcnow() - datetime(1970, 1, 1)
secondsS = (date.total_seconds())
millisecondsS = round(secondsS * 1000)

if(ser.writable()):
    print('Writing is possible\n')


s = 'SIR'
print('Writing to Serial\n')
ser.write(ser.write(s.encode()))
time.sleep(2)
print('Attempting to read serial\n')

if (ser.readable()==False):
    print('Not readable\n')

while True:
    s = 'SIR' #this code executes the continuous output of weights from the scale
    ser.write(ser.write(s.encode()))
    time.sleep(3)
    read = ser.readline()
    readB = read.decode('utf-8')
    print(f'Weight:  {readB}')
    time.sleep(3)

PyCharm的Run Section中的输出是:

Starting Connection
Starting Serial Monitor

dsr:  True
cts:  True
dtr:  True
rts:  True
cd:  False
ri:  False
Writing is possible

Writing to Serial

Attempting to read serial

Weight:  
Weight: 

我希望这里有人知道如何修复输出。

我希望该程序能够正常工作,因为重要的握手 dsr,... 是真实的。

python serial-port scale pyserial
© www.soinside.com 2019 - 2024. All rights reserved.