Serial communication with X20BC008U

Step-by-step guide

  1. Place X20CS1020 behind X20BC008U-rack
  2. Restore factory settings of X20BC008U, which will activate automatic configuration of X20BC008U
  3. With UaExpert additional settings can be found on the X20CS1020 module like Baud rate, Parity, Stop bits, Termination characters, Receive idle time and so on.
  4. With UaExpert methods can be found on the X20CS1020 module like ApplyChanges, LoadDefault, Restore, Open, Close, Read and Write.
  5. The correct settings can be made by writing to these variables like Baud rate, Parity, Stop bits and so on.
  6. By calling method ApplyChanges, these settings are saved and the module is rebooted.
  7. With method Open the communication is activated.
  8. By reading of variable FramesReceived, the application can check if the method Read must be called.
  9. By calling method Write frames can be written.
  10. Method Close will stop the communication.
  11. This can be tested with HyperTerminal or Putty.

Information Model

Data Access View

Structured Text example

Serial.zip (3.3 KB)

Python example

from opcua import Client
from opcua import ua
    
if __name__ == "__main__":
    url = "opc.tcp://192.168.1.1:4840"
    mode =  ['']
    nodemethod = ['2:ST2','2:MethodSet']
    nodeopen = ['2:ST2','2:MethodSet','2:Open']
    noderead = ['2:ST2','2:MethodSet','2:Read']
    nodewrite = ['2:ST2','2:MethodSet','2:Write']
    nodeframe = ['2:ST2','2:ProcessData','2:FramesReceived']
  
    startnode = "ns=2;i=11" # SubDevices X20BC008U
    
    def RelativePath(nodes):
        rpath = ua.RelativePath()
        for node in nodes:
            el = ua.RelativePathElement()
            el.ReferenceTypeId = ua.FourByteNodeId(ua.ObjectIds.HierarchicalReferences)
            el.IsInverse = False
            el.IncludeSubtypes = True
            if isinstance(node, ua.QualifiedName):
                el.TargetName = node
            else:
                el.TargetName = ua.QualifiedName.from_string(node)
            rpath.Elements.append(el)
        return rpath
  
    def Translate(node,startnode):
        bpath = ua.BrowsePath()
        bpath.StartingNode = ua.NodeId.from_string(startnode)
        bpath.RelativePath = RelativePath(node)
        nodeids = client.uaclient.translate_browsepaths_to_nodeids([bpath])
        return nodeids[0].Targets[0].TargetId
  
    client = Client(url=url)
    try:
        client.connect()
  
        NodeM = Translate(nodemethod,startnode)
        NodeO = Translate(nodeopen,startnode)
        NodeR = Translate(noderead,startnode)
        NodeW = Translate(nodewrite,startnode)
        NodeF = Translate(nodeframe,startnode)
  
        if NodeM.Identifier > 0:
            print('NameSpaceIndex: ',NodeM.NamespaceIndex,' Identifier: ',NodeM.Identifier)
        if NodeO.Identifier > 0:
            print('NameSpaceIndex: ',NodeO.NamespaceIndex,' Identifier: ',NodeO.Identifier)
        if NodeR.Identifier > 0:
            print('NameSpaceIndex: ',NodeR.NamespaceIndex,' Identifier: ',NodeR.Identifier)
        if NodeW.Identifier > 0:
            print('NameSpaceIndex: ',NodeW.NamespaceIndex,' Identifier: ',NodeW.Identifier)                                
        if NodeF.Identifier > 0:
            print('NameSpaceIndex: ',NodeF.NamespaceIndex,' Identifier: ',NodeF.Identifier)
  
        MethodSet = client.get_node(NodeM)
        Open = client.get_node(NodeO)
        Read = client.get_node(NodeR)
        Write = client.get_node(NodeW)
  
        resOpen = MethodSet.call_method(Open,mode)
        node = client.get_node(NodeF)
        val = node.get_value()
        while val == 0:
            val = node.get_value()
        print('FramesReceived: ',val)
        res = MethodSet.call_method(Read)
        MethodSet.call_method(Write,res)
        print(res)
  
    finally:
        client.disconnect()
4 Likes