[英]What is the equivalent of GetEncoding("28591") of C# in Python?
我想從 COM6 獲取設備的代碼,我很容易使用以下代碼從 C# 獲取輸出:
serialPort1.Encoding = System.Text.Encoding.GetEncoding(28591);
但我不知道如何在 Python 中做到這一點。
我已經嘗試過:
import serial
import time
ser = serial.Serial()
ser.port='COM6'
ser.baudrate=9600
ser.parity=serial.PARITY_NONE
ser.stopbits=serial.STOPBITS_ONE
ser.bytesize=serial.EIGHTBITS
ser.timeout=2
if ser.is_open:
ser.close()
else:
ser.open()
print("connected to: " + ser.portstr)
while True:
ser.flushInput()
time.sleep(0.01)
data_raw = (ser.readline())
print(data_raw.decode("utf-8"))
您可以使用 pythonnet 訪問 .Net 庫。 當我運行下面的代碼時,我得到 System.Text.Latin1Encoding
import clr
from System import Text
result = Text.Encoding.GetEncoding(28591)
print(result)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.