在多个串行端口上编写Zaber设备脚本

时间:2012-04-19 19:09:37

标签: .net serial-port motion zaber

是否可以将Zaber设备连接到多个串行端口并从单个脚本控制它们?该脚本在Zaber Console中运行,并且应该能够协调任一端口上设备之间的运动。

1 个答案:

答案 0 :(得分:0)

是的,您可以在脚本中打开更多串行端口。您只需复制我们的Zaber Console程序通常为您执行的所有配置。串口打开后,您可以正常使用对话对象。协调两者之间的运动 设备,您使用对话主题对象等待响应。有关更多详细信息,请阅读Zaber库帮助文件。您可以在脚本编辑器的帮助菜单中找到它。

// Example C# script showing how to open a second serial port and coordinate 
// moves between the two.
#template(methods)

public override void Run()
{
    // First port is the one that Zaber Console has already opened.
    var portFacade1 = PortFacade;
    // Now, we're going to open COM2 in the script.
    // The using block makes sure we don't leave it open.
    using (var portFacade2 = CreatePortFacade())
    {
        portFacade2.Open("COM2");

        // Start a conversation with a device on each port.
        // Note that the device numbers can be the same because they're on
        // separate ports.
        var conversation1 = portFacade1.GetConversation(1);
        var conversation2 = portFacade2.GetConversation(1);

        while ( ! IsCanceled)
        {
            MoveBoth(conversation1, conversation2, 0);
            MoveBoth(conversation1, conversation2, 10000);
        }
    }
}

private void MoveBoth(
    Conversation conversation1, 
    Conversation conversation2, 
    int position)
{
    // Start a topic to wait for the response
    var topic = conversation1.StartTopic();
    // Send the command using Device.Send() instead of Request()
    // Note the topic.MessageId parameter to coordinate request and response
    conversation1.Device.Send(
            Command.MoveAbsolute, 
            position, 
            topic.MessageId);

    // While c1 is moving, also request c2 to move. This one just uses
    // Request() because we want to wait until it finishes.
    conversation2.Request(Command.MoveAbsolute, position);

    // We know c2 has finished moving, so now wait until c1 finishes.
    topic.Wait();
    topic.Validate();
}

private ZaberPortFacade CreatePortFacade()
{
    var packetConverter = new PacketConverter();
    packetConverter.MillisecondsTimeout = 50;
    var defaultDeviceType = new DeviceType();
    defaultDeviceType.Commands = new List<CommandInfo>();
    var portFacade = new ZaberPortFacade();
    portFacade.DefaultDeviceType = defaultDeviceType;
    portFacade.QueryTimeout = 1000;
    portFacade.Port = new TSeriesPort(
        new System.IO.Ports.SerialPort(), 
        packetConverter);
    portFacade.DeviceTypes = new List<DeviceType>();
    return portFacade;
}