0

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

4

1 回答 1

0

是的,您可以在脚本中打开更多串行端口。您只需复制我们的 Zaber 控制台程序通常为您执行的所有配置。串口打开后,就可以正常使用会话对象了。要协调两个设备之间的移动,您可以使用对话主题对象来等待响应。有关更多详细信息,请阅读 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;
}
于 2012-04-19T19:09:55.027 回答