Hello Everyone,
I am using the uCam RS-232 for a vision based underwater vehicle that recognizes colors and objects. Right now I have hooked up the camera to a power source and my laptop. I have run my code that sends the SYNC command and have verified this with an oscilloscope as seen in the attached picture. The camera is sending back an ACK and SYNC command, but for some reason, my code is not reading/capturing it. Here is my code, if anyone could give me advice on how to correct this issue that would be so helpful.
Thanks Brian
/*
============================================================================
Name : uCam.c
Author : Brian Weber
Version :
Copyright : Your copyright notice
Description : uCam 4D Systems Serial Camera
============================================================================
*/
#include <stdio.h> /* Standard input/output definitions */
#include <stdlib.h>
#include <time.h> /* Clock Functions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
// #include <math.h>
#include "ucam.h"
int mainfd = 0; /* File descriptor for the port */
/*
* 'open_port()' - Open the serial port.
* Returns the file descriptor on success or -1 on error.
*/
int open_port (char portName[]) {
int fd; /* File descriptor for the port */
fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1) { /* Could not open the port */
// errors are sent to stderr ..... Prints erros logged under errno
fprintf(stderr, "\nopen port: Unable to open %s - %s\n", portName, strerror(errno));
exit(EXIT_FAILURE); // EXIT
}
else {
printf("\nopen port: port %s has been opened correctly.\n", portName);
printf("fd = %i\n", fd);
}
return (fd);
}
void config_port(int mainfd) {
struct termios options;
// Get the current settings of the serial port.
tcgetattr (mainfd, &options);
// Set the read and write speed to 57600 BAUD.
// All speeds can be prefixed with B as a settings.
cfsetispeed (&options, B57600);
cfsetospeed (&options, B57600);
// Now to set the other settings. Here we use the no parity example. Both will assumme 8-bit words.
// PARENB is enabled parity bit. This disables the parity bit.
options.c_cflag &= ~PARENB;
// CSTOPB means 2 stop bits, otherwise (in this case) only one stop bit.
options.c_cflag &= ~CSTOPB;
// CSIZE is a mask for all the data size bits, so anding with the negation clears out the current data size setting.
options.c_cflag &= ~CSIZE;
// CS8 means 8-bits per work
options.c_cflag |= CS8;
// Enable Receiver and local
options.c_cflag = (CLOCAL | CREAD);
}
void close_port(int mainfd, char portName[]) // Subroutine to close port
{
/* Close the serial port */
if (close(mainfd) == -1) {
fprintf(stderr, "close port: Unable to close %s - %s\n", portName, strerror(errno));
exit(EXIT_FAILURE); // EXIT
}
else {
printf("close port: The port %s has been closed correctly.\n", portName);
exit(EXIT_SUCCESS);
}
}
// Set up memory to hold the commands
const unsigned char SYNC[] = {0xAA, 0x0D, 0x00, 0x00, 0x00, 0x00};
const unsigned char ACK[] = {0xAA, 0x0E, 0x0D, 0x00, 0x00, 0x00};
unsigned char buf[6];
int i;
int ack_received = 0;
unsigned char ack_counter;
int send_SYNC(int mainfd)
{
for (i=0; i<60; i++) {
write(mainfd, SYNC, 6);
printf("\nWrote SYNC command\n");
for(i=0; i<6000; i++){
fcntl(mainfd, F_SETFL, 0);
read(mainfd, &buf, 1);
printf("\nRead port %i\n", mainfd);
if (buf[0] == 0xAA && buf[1] == 0x0E && buf[2] == 0x0D && buf[4] == 0x00 && buf[5] == 0x00) {
printf("\nACK command sent back from camera\n");
ack_counter = buf[3];
ack_received = 1;
break;
}
else {
printf("\nACK command not sent back from camera\n");
}
}
sleep(2);
}
if(ack_received)
return(0);
else
return(-1);
}
int main(int argc, char* argv[]) // arguments entered into terminal
{
if (argc != 2) {
printf("USAGE: %s PORT. i.e. %s /dev/ttyUSB0.\n", argv[0], argv[0]);
exit(EXIT_FAILURE); // EXIT
}
mainfd = open_port(argv[1]); // port location given by user
config_port(mainfd); // configuring port
if (tcflush(mainfd, TCIOFLUSH) == 0)
printf("\nThe input and output queues have been flushed\n");
else
perror("tcflush error");
sleep(2); // Wait two seconds
send_SYNC(mainfd);
if (ack_received){
printf("\nACK received\n");
return(0);
}
else {
printf("\nACK not received\n");
return(-1);
}
}
I am using the uCam RS-232 for a vision based underwater vehicle that recognizes colors and objects. Right now I have hooked up the camera to a power source and my laptop. I have run my code that sends the SYNC command and have verified this with an oscilloscope as seen in the attached picture. The camera is sending back an ACK and SYNC command, but for some reason, my code is not reading/capturing it. Here is my code, if anyone could give me advice on how to correct this issue that would be so helpful.
Thanks Brian
/*
============================================================================
Name : uCam.c
Author : Brian Weber
Version :
Copyright : Your copyright notice
Description : uCam 4D Systems Serial Camera
============================================================================
*/
#include <stdio.h> /* Standard input/output definitions */
#include <stdlib.h>
#include <time.h> /* Clock Functions */
#include <string.h> /* String function definitions */
#include <unistd.h> /* UNIX standard function definitions */
#include <fcntl.h> /* File control definitions */
#include <errno.h> /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */
// #include <math.h>
#include "ucam.h"
int mainfd = 0; /* File descriptor for the port */
/*
* 'open_port()' - Open the serial port.
* Returns the file descriptor on success or -1 on error.
*/
int open_port (char portName[]) {
int fd; /* File descriptor for the port */
fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1) { /* Could not open the port */
// errors are sent to stderr ..... Prints erros logged under errno
fprintf(stderr, "\nopen port: Unable to open %s - %s\n", portName, strerror(errno));
exit(EXIT_FAILURE); // EXIT
}
else {
printf("\nopen port: port %s has been opened correctly.\n", portName);
printf("fd = %i\n", fd);
}
return (fd);
}
void config_port(int mainfd) {
struct termios options;
// Get the current settings of the serial port.
tcgetattr (mainfd, &options);
// Set the read and write speed to 57600 BAUD.
// All speeds can be prefixed with B as a settings.
cfsetispeed (&options, B57600);
cfsetospeed (&options, B57600);
// Now to set the other settings. Here we use the no parity example. Both will assumme 8-bit words.
// PARENB is enabled parity bit. This disables the parity bit.
options.c_cflag &= ~PARENB;
// CSTOPB means 2 stop bits, otherwise (in this case) only one stop bit.
options.c_cflag &= ~CSTOPB;
// CSIZE is a mask for all the data size bits, so anding with the negation clears out the current data size setting.
options.c_cflag &= ~CSIZE;
// CS8 means 8-bits per work
options.c_cflag |= CS8;
// Enable Receiver and local
options.c_cflag = (CLOCAL | CREAD);
}
void close_port(int mainfd, char portName[]) // Subroutine to close port
{
/* Close the serial port */
if (close(mainfd) == -1) {
fprintf(stderr, "close port: Unable to close %s - %s\n", portName, strerror(errno));
exit(EXIT_FAILURE); // EXIT
}
else {
printf("close port: The port %s has been closed correctly.\n", portName);
exit(EXIT_SUCCESS);
}
}
// Set up memory to hold the commands
const unsigned char SYNC[] = {0xAA, 0x0D, 0x00, 0x00, 0x00, 0x00};
const unsigned char ACK[] = {0xAA, 0x0E, 0x0D, 0x00, 0x00, 0x00};
unsigned char buf[6];
int i;
int ack_received = 0;
unsigned char ack_counter;
int send_SYNC(int mainfd)
{
for (i=0; i<60; i++) {
write(mainfd, SYNC, 6);
printf("\nWrote SYNC command\n");
for(i=0; i<6000; i++){
fcntl(mainfd, F_SETFL, 0);
read(mainfd, &buf, 1);
printf("\nRead port %i\n", mainfd);
if (buf[0] == 0xAA && buf[1] == 0x0E && buf[2] == 0x0D && buf[4] == 0x00 && buf[5] == 0x00) {
printf("\nACK command sent back from camera\n");
ack_counter = buf[3];
ack_received = 1;
break;
}
else {
printf("\nACK command not sent back from camera\n");
}
}
sleep(2);
}
if(ack_received)
return(0);
else
return(-1);
}
int main(int argc, char* argv[]) // arguments entered into terminal
{
if (argc != 2) {
printf("USAGE: %s PORT. i.e. %s /dev/ttyUSB0.\n", argv[0], argv[0]);
exit(EXIT_FAILURE); // EXIT
}
mainfd = open_port(argv[1]); // port location given by user
config_port(mainfd); // configuring port
if (tcflush(mainfd, TCIOFLUSH) == 0)
printf("\nThe input and output queues have been flushed\n");
else
perror("tcflush error");
sleep(2); // Wait two seconds
send_SYNC(mainfd);
if (ack_received){
printf("\nACK received\n");
return(0);
}
else {
printf("\nACK not received\n");
return(-1);
}
}
Comment