通过Python套接字库向Advantech ADAM6024发送命令并从中接收数据

2024-10-04 03:20:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Python向Advantech的基于ADAM-6024以太网的数据采集和控制模块发送命令,并从中接收数据。我正在使用MacBook Pro,并通过以太网连接到设备

以下是ADAM-6000系列设备的手册:

http://advdownload.advantech.com/productfile/Downloadfile4/1-1M99LTH/ADAM-6000_User_Manaul_Ed_9.pdf

我已经编写了一个名为ADAMConnection的python类,其中包含一些方法,这些方法设置到模块的套接字连接,然后向模块发送命令并请求响应

我可以进行套接字连接,但无法与设备通信。这是我写的图书馆:

class ADAMConnection:
    def __init__(self, host, eth_port):
        self.host = host
        self.eth_port = eth_port
        self.adamsock = None

    ###############################################################################
    # ConnectADAM() method
    ###############################################################################
    def OpenADAMConnection( self ):
      self.adamsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
      self.adamsock.settimeout(2)
      try:
         self.adamsock.connect((self.host,self.eth_port))
         #self.adamsock.send(str.encode("*RST\r\n")) #Example of what's done in DAQConn
         #TODO: Do we need any initialization commands?
         print("ADAM connected..")
         time.sleep(0.05)
         return True
      except socket.timeout:
         print('\n*************** ERROR *****************')
         print('Attempt to connect to the ADAM timed out. \n' + \
               'Make sure it\'s powered on and plugged in.')
         print('*************** ERROR *****************\n\n')
         return False
      except OSError:
         print('\n*************** ERROR *****************')
         print('Unable to connect to the ADAM. \n' + \
               'Make sure it\'s powered on and plugged in.')
         print('*************** ERROR *****************\n\n')
         return False

    def GetModuleName(self,commandString):
        #self.adamsock.send(str.encode("ASCII STRING")) 
        self.adamsock.send(str.encode(commandString,encoding="ascii")) 
        time.sleep(0.05)
        adam_out = self.adamsock.recv(100) #is the 100 needed?  Maybe try for now
        time.sleep(0.05)
        return adam_out

当我导入这个库并尝试进行套接字连接时,我成功了。我试图通过ASCII命令与它对话,所以我使用端口502。我知道通过此链接使用此端口:

http://advdownload.advantech.com/productfile/Downloadfile1/GF-1HBO9/Which%20TCP%20and%20UDP%20Ports%20do%20ADAM%20Ethernet%20Modules%20Use.pdf

然而,当我试图询问ADAM-6024模块的模块名时,我得到了我不理解的无意义的输出。我首先尝试发送一个命令,使用我在手册第112页(第一个链接)上找到的ASCII语法读取模块名,但收到的似乎是垃圾输出。为了确认这一点,我向模块发送了一个我知道没有意义的命令,并收到了类似的输出

下面是我用来完成上面描述的工作的python代码:

Python 3.8.5 (default, Sep  4 2020, 02:22:02) 
[Clang 10.0.0 ] :: Anaconda, Inc. on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ADAMConnection
>>> module1 = ADAMConnection.ADAMConnection("192.168.6.70",502)
>>> module1.OpenADAMConnection()
ADAM connected..
True
>>> module1.GetModuleName("01M0Dh")
b'01M0\x00\x03\x00\x80\x01'
>>> module1.GetModuleName("nonsense")
b'nons\x00\x03s\xe5\x01'

我几乎不精通Python,对Modbus/TCP/ASCII/Socket连接一无所知,所以我不知道我是否遗漏了一些非常明显的东西。任何帮助都将不胜感激


Tags: 模块to命令selfhostreturnportascii
1条回答
网友
1楼 · 发布于 2024-10-04 03:20:55

成功发送和接收数据是一个良好的开端。但是,我不知道您是如何获得命令字符串"01M0Dh"。本User Manual7.4 ADAM-6000模块的ASCII命令说明:

$aaM

Name Read module name
Description Returns the name of a specific module
Syntax $aaM(cr)
 $ is a delimiter
 aa (range 00~FF) is the 2-character hex slave address of the specified module (always 01)
 M refers to the read model number command
 (cr) is the terminating character, carriage return (0Dh)

这对应于Python字符串"$01M\r"

查看您发布的响应数据包,并根据端口502,似乎使用了Modbus TCP协议。手册对此有点不清楚,但可能是ASCII命令将嵌入Modbus TCP帧中;然后字符串将类似于e。g"..\0\0\0\5$01M\r"。但是,我不能排除ASCII命令是通过UDP发送到端口1025的。如果这样做不成功,还可以尝试从浏览器中使用HTTP,如ADAM的REST参考资料部分D.2E中所述。ghttp://host/digitalinput/all/value

相关问题 更多 >