Python接口用于外星RFID读取器

0 投票
2 回答
1686 浏览
提问于 2025-04-17 16:15

已经修正。请看我自己问题的答案在下面。

我正在尝试通过TCP/IP接口,用Python 2.7与一个Alien RFID 9800读卡器进行通信。
但是,附带的测试代码在读卡器登录时就卡住了,读卡器没有处理“获取读卡器名称”的命令。
我使用的是默认的用户名(alien)和密码(password)。在Alien的界面上操作一切正常。登录的过程有什么问题吗?哪里不对呢?

import socket

cmdHost, cmdPort = '192.168.1.106', 23

CmdDelim = '\n'               # Corrected from '\n\r' to '\n'.  Delimiter of Alien commands (sent to reader).
ReaderDelim = '\r\n\0'        # Delimiter of Alien reader responses (received from reader).
CmdPrefix = chr(1)            # Causes Alien reader to suppress prompt on response.

def getResponse( conn ):
    ''' Get the reader's response with correct terminator. '''
    response = ''
    while not response.endswith( ReaderDelim ):
        more = conn.recv( 4096 )
        if not more:
            break
        response += more
    return response

def GetReaderName():
    ''' Log into the reader, get the reader name, then quit. '''
    print 'Sending commands to the Alien reader...'
    cmdSocket = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
    try:
        cmdSocket.connect( (cmdHost, int(cmdPort)) )
    except Exception as inst:
        log( 'Reader Connection Failed: CmdAddr=%s:%d' % (cmdHost, cmdPort) )
        log( '%s' % inst )
        cmdSocket.close()
        return False

    # Read the initial header from the reader.
    response = getResponse( cmdSocket )
    print response

    # UserName
    cmdSocket.sendall( 'alien%s' % CmdDelim )
    response = getResponse( cmdSocket )
    print response

    # Password
    cmdSocket.sendall( 'password%s' % CmdDelim )
    response = getResponse( cmdSocket )
    print response

    # Get ReaderName command
    cmdSocket.sendall( '%sGet ReaderName%s' % (CmdPrefix, CmdDelim) )
    response = getResponse( cmdSocket )
    print response

    # Quit
    cmdSocket.sendall( '%sQuit%s' % (CmdPrefix, CmdDelim) )
    response = getResponse( cmdSocket )
    print response

    cmdSocket.close()
    return True

if __name__ == '__main__':
    GetReaderName()

2 个回答

0

你有一些 print response 的命令。这个命令会打印出任何东西,还是不打印呢?

0

经过进一步的实验,我可以确认,对于TCP接口,命令的结束符只是'\n' [换行符],而不是'\r\n' [回车][换行]。所以如果把上面的代码改成:

CmdDelim = '\n'

现在,一切都正常工作了。

不幸的是,Alien的文档非常明确地指出[CR][LF]是命令的结束符。也许这对于串口接口是正确的,但在TCP接口上并不适用。

撰写回答