public bool connect(String port_name) { // Setup serial port values port.BaudRate = 38400; port.DataBits = 8; port.NewLine = new string((char)0xFF, 1); // This is the visca terminator, we don't use "readline" but its here for clarity port.Parity = Parity.None; port.PortName = port_name; port.StopBits = StopBits.One; port.ReadTimeout = 2000; try { port.Open(); } // Open serial connection catch { return hardware_connected; } // No connection, nothing else to do // Get camera number connect_command connect_cmd = new connect_command(); port.Write(connect_cmd.raw_serial_data, 0, connect_cmd.raw_serial_data.Length); List<int> response; int response_type = get_response(out response); while (response_type == VISCA_CODE.RESPONSE_ACK) // Skip acknowledge messages response_type = get_response(out response); if (response_type != VISCA_CODE.RESPONSE_ADDRESS) // Got camera number return hardware_connected; // Note: if an error or timeout occurs, connected is false (as expected) camera_num = response[2] - 1; // Get the current pan/tilt/zoom position // Note: This is hard coded here rather than using the dispatch thread because at this point we don't know if we are connected yet command inquiry = new pan_tilt_inquiry_command(camera_num); port.Write(inquiry.raw_serial_data, 0, inquiry.raw_serial_data.Length); dispatched_cmd = inquiry; response_type = get_response(out response); // Should only be command complete CameraHardwareErrorEventHandler backup = position_data_error; // Backing up user event handlers position_data_error = new CameraHardwareErrorEventHandler(initilization_error_handler); receive_response_completed(response); // Will put the response into "pan" and "tilt" inquiry = new zoom_inquiry_command(camera_num); port.Write(inquiry.raw_serial_data, 0, inquiry.raw_serial_data.Length); dispatched_cmd = inquiry; response_type = get_response(out response); // Should only be command complete receive_response_completed(response); // Will put the response into "zoom" position_data_error = backup; // Restore user event handlers // If we can't get the current positions, then we aren't connected to the camera if (!initilization_error) { hardware_connected = true; // Start the background threads receive_thread.Start(); dispatch_thread.Start(); inquiry_after_stop_thread.Start(); pid_thread.Start(); } return hardware_connected; }
public connect_command(connect_command rhs) { }