dc_status_t cressi_leonardo_device_open (dc_device_t **out, dc_context_t *context, const char *name) { if (out == NULL) return DC_STATUS_INVALIDARGS; // Allocate memory. cressi_leonardo_device_t *device = (cressi_leonardo_device_t *) malloc (sizeof (cressi_leonardo_device_t)); if (device == NULL) { ERROR (context, "Failed to allocate memory."); return DC_STATUS_NOMEMORY; } // Initialize the base class. device_init (&device->base, context, &cressi_leonardo_device_vtable); // Set the default values. device->port = NULL; // Open the device. int rc = serial_open (&device->port, context, name); if (rc == -1) { ERROR (context, "Failed to open the serial port."); free (device); return DC_STATUS_IO; } // Set the serial communication protocol (115200 8N1). rc = serial_configure (device->port, 115200, 8, SERIAL_PARITY_NONE, 1, SERIAL_FLOWCONTROL_NONE); if (rc == -1) { ERROR (context, "Failed to set the terminal attributes."); serial_close (device->port); free (device); return DC_STATUS_IO; } // Set the timeout for receiving data (1000 ms). if (serial_set_timeout (device->port, 1000) == -1) { ERROR (context, "Failed to set the timeout."); serial_close (device->port); free (device); return DC_STATUS_IO; } // Clear the DTR and set the RTS line. if (serial_set_dtr (device->port, 0) == -1 || serial_set_rts (device->port, 1) == -1) { ERROR (context, "Failed to set the DTR/RTS line."); serial_close (device->port); free (device); return DC_STATUS_IO; } *out = (dc_device_t *) device; return DC_STATUS_SUCCESS; }
device_status_t suunto_eon_device_open (device_t **out, const char* name) { if (out == NULL) return DEVICE_STATUS_ERROR; // Allocate memory. suunto_eon_device_t *device = (suunto_eon_device_t *) malloc (sizeof (suunto_eon_device_t)); if (device == NULL) { WARNING ("Failed to allocate memory."); return DEVICE_STATUS_MEMORY; } // Initialize the base class. suunto_common_device_init (&device->base, &suunto_eon_device_backend); // Set the default values. device->port = NULL; // Open the device. int rc = serial_open (&device->port, name); if (rc == -1) { WARNING ("Failed to open the serial port."); free (device); return DEVICE_STATUS_IO; } // Set the serial communication protocol (1200 8N2). rc = serial_configure (device->port, 1200, 8, SERIAL_PARITY_NONE, 2, SERIAL_FLOWCONTROL_NONE); if (rc == -1) { WARNING ("Failed to set the terminal attributes."); serial_close (device->port); free (device); return DEVICE_STATUS_IO; } // Set the timeout for receiving data (1000ms). if (serial_set_timeout (device->port, -1) == -1) { WARNING ("Failed to set the timeout."); serial_close (device->port); free (device); return DEVICE_STATUS_IO; } // Clear the RTS line. if (serial_set_rts (device->port, 0)) { WARNING ("Failed to set the DTR/RTS line."); serial_close (device->port); free (device); return DEVICE_STATUS_IO; } *out = (device_t*) device; return DEVICE_STATUS_SUCCESS; }
static void rl78_set_reset(port_handle_t fd, int mode, int value) { if (MODE_RESET_RTS == (mode & MODE_RESET)) { serial_set_rts(fd, value); } else { serial_set_dtr(fd, value); } }
static device_status_t suunto_d9_device_packet (device_t *abstract, const unsigned char command[], unsigned int csize, unsigned char answer[], unsigned int asize, unsigned int size) { suunto_d9_device_t *device = (suunto_d9_device_t *) abstract; if (device_is_cancelled (abstract)) return DEVICE_STATUS_CANCELLED; // Clear RTS to send the command. serial_set_rts (device->port, 0); // Send the command to the dive computer. int n = serial_write (device->port, command, csize); if (n != csize) { WARNING ("Failed to send the command."); return EXITCODE (n); } // Wait until all data has been transmitted. serial_drain (device->port); // Receive the echo. unsigned char echo[128] = {0}; assert (sizeof (echo) >= csize); n = serial_read (device->port, echo, csize); if (n != csize) { WARNING ("Failed to receive the echo."); return EXITCODE (n); } // Verify the echo. if (memcmp (command, echo, csize) != 0) { WARNING ("Unexpected echo."); return DEVICE_STATUS_PROTOCOL; } // Set RTS to receive the reply. serial_set_rts (device->port, 1); // Receive the answer of the dive computer. n = serial_read (device->port, answer, asize); if (n != asize) { WARNING ("Failed to receive the answer."); return EXITCODE (n); } // Verify the header of the package. if (answer[0] != command[0]) { WARNING ("Unexpected answer header."); return DEVICE_STATUS_PROTOCOL; } // Verify the size of the package. if (array_uint16_be (answer + 1) + 4 != asize) { WARNING ("Unexpected answer size."); return DEVICE_STATUS_PROTOCOL; } // Verify the parameters of the package. if (memcmp (command + 3, answer + 3, asize - size - 4) != 0) { WARNING ("Unexpected answer parameters."); return DEVICE_STATUS_PROTOCOL; } // Verify the checksum of the package. unsigned char crc = answer[asize - 1]; unsigned char ccrc = checksum_xor_uint8 (answer, asize - 1, 0x00); if (crc != ccrc) { WARNING ("Unexpected answer CRC."); return DEVICE_STATUS_PROTOCOL; } return DEVICE_STATUS_SUCCESS; }
dc_status_t oceanic_vtpro_device_open (dc_device_t **out, dc_context_t *context, const char *name) { if (out == NULL) return DC_STATUS_INVALIDARGS; // Allocate memory. oceanic_vtpro_device_t *device = (oceanic_vtpro_device_t *) malloc (sizeof (oceanic_vtpro_device_t)); if (device == NULL) { ERROR (context, "Failed to allocate memory."); return DC_STATUS_NOMEMORY; } // Initialize the base class. oceanic_common_device_init (&device->base, context, &oceanic_vtpro_device_vtable); // Override the base class values. device->base.multipage = MULTIPAGE; // Set the default values. device->port = NULL; // Open the device. int rc = serial_open (&device->port, context, name); if (rc == -1) { ERROR (context, "Failed to open the serial port."); free (device); return DC_STATUS_IO; } // Set the serial communication protocol (9600 8N1). rc = serial_configure (device->port, 9600, 8, SERIAL_PARITY_NONE, 1, SERIAL_FLOWCONTROL_NONE); if (rc == -1) { ERROR (context, "Failed to set the terminal attributes."); serial_close (device->port); free (device); return DC_STATUS_IO; } // Set the timeout for receiving data (3000 ms). if (serial_set_timeout (device->port, 3000) == -1) { ERROR (context, "Failed to set the timeout."); serial_close (device->port); free (device); return DC_STATUS_IO; } // Set the DTR and RTS lines. if (serial_set_dtr (device->port, 1) == -1 || serial_set_rts (device->port, 1) == -1) { ERROR (context, "Failed to set the DTR/RTS line."); serial_close (device->port); free (device); return DC_STATUS_IO; } // Give the interface 100 ms to settle and draw power up. serial_sleep (device->port, 100); // Make sure everything is in a sane state. serial_flush (device->port, SERIAL_QUEUE_BOTH); // Initialize the data cable (MOD mode). dc_status_t status = oceanic_vtpro_init (device); if (status != DC_STATUS_SUCCESS) { serial_close (device->port); free (device); return status; } // Switch the device from surface mode into download mode. Before sending // this command, the device needs to be in PC mode (manually activated by // the user), or already in download mode. status = oceanic_vtpro_device_version ((dc_device_t *) device, device->base.version, sizeof (device->base.version)); if (status != DC_STATUS_SUCCESS) { serial_close (device->port); free (device); return status; } // Calibrate the device. Although calibration is optional, it's highly // recommended because it reduces the transfer time considerably, even // when processing the command itself is quite slow. status = oceanic_vtpro_calibrate (device); if (status != DC_STATUS_SUCCESS) { serial_close (device->port); free (device); return status; } // Override the base class values. if (OCEANIC_COMMON_MATCH (device->base.version, oceanic_wisdom_version)) { device->base.layout = &oceanic_wisdom_layout; } else { device->base.layout = &oceanic_vtpro_layout; } *out = (dc_device_t*) device; return DC_STATUS_SUCCESS; }
void serial_bb_set_pgc(uint8_t pgc) { serial_set_rts(serial_port, pgc); }