来自COM端口的数据比较

时间:2018-04-10 17:07:40

标签: python-3.x pyserial

我是Python的新手,我需要帮助,这个MAC地址比较不起作用

  
    
      
        

if(joined_seq)==“38aa3c06f813”:

      
    
  
从com端口

我得到mac地址,但我无法比较它们

dc7144xxxxxx

e49e12xxxxxx

e49e12xxxxxx

f4cae5xxxxxx

e0cec3xxxxxx

如果你能提出正确的方法,我将非常感激 谢谢!

import serial

ser = serial.Serial(
    port='COM3',\
    baudrate=115200,\
    parity=serial.PARITY_NONE,\
    stopbits=serial.STOPBITS_ONE,\
    bytesize=serial.EIGHTBITS,\
        timeout=0)

print("connected to: " + ser.portstr)

#this will store the line
seq = []

while True:
    for c in ser.read():
        seq.append(chr(c)) #convert from ANSII
        joined_seq = ''.join(str(v) for v in seq) #Make a string from array
        #f = open("data2.txt", "w")
        #f.write(joined_seq)      # str() converts to string
        #f.close()
        		
        if chr(c) == '\n':
            #print(joined_seq)
            if (joined_seq) == "38aa3c06f813":
                print("bingo")
            
            break


ser.close()

1 个答案:

答案 0 :(得分:0)

现在可以添加

  
    

(\ n)在mac地址的末尾

  

import serial

ser = serial.Serial(
    port='COM3',\
    baudrate=115200,\
    parity=serial.PARITY_NONE,\
    stopbits=serial.STOPBITS_ONE,\
    bytesize=serial.EIGHTBITS,\
        timeout=0)

print("connected to: " + ser.portstr)

#this will store the line
seq = []


mac = "38aa3cxxxxxx\n"

while True:
    for c in ser.read():
        seq.append(chr(c)) #convert from ANSII
        joined_seq = ''.join(str(v) for v in seq) #Make a string from array
        #f = open("data2.txt", "w")
        #f.write(joined_seq)      # str() converts to string
        #f.close()
        		
        if chr(c) == '\n':
            #print(joined_seq)
            seq = []
            if (joined_seq) == mac:
                print("BingoBongo")
            
            break


ser.close()