Announcement

Collapse
No announcement yet.

SYNC Cmd Sent but I am unable to read the port using Linux

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • SYNC Cmd Sent but I am unable to read the port using Linux

    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, &amp;options);

    // Set the read and write speed to 57600 BAUD.
    // All speeds can be prefixed with B as a settings.
    cfsetispeed (&amp;options, B57600);
    cfsetospeed (&amp;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 &amp;= ~PARENB;

    // CSTOPB means 2 stop bits, otherwise (in this case) only one stop bit.
    options.c_cflag &amp;= ~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 &amp;= ~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, &amp;buf, 1);
    printf("\nRead port %i\n", mainfd);
    if (buf[0] == 0xAA &amp;&amp; buf[1] == 0x0E &amp;&amp; buf[2] == 0x0D &amp;&amp; buf[4] == 0x00 &amp;&amp; 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);
    }

    }
    Color Based Vision Navigation System - Biomimetic Unmanned Underwater Vehicle

  • #2


    Do you think that it is a camera or coding problem?
    Color Based Vision Navigation System - Biomimetic Unmanned Underwater Vehicle

    Comment


    • #3


      I think, the way you said "The camera is sending back an ACK and SYNC command, but for some reason, my code is not reading/capturing it." the problem must be in your program.

      A few things to remember at this point, the ACK and the SYNC come back together, so your comms must be able to buffer 12 bytes, or you will lose some of it.

      Is this what's happenning?

      What about this?
      read(mainfd, &buf, 1);
      Isn't the 1 saying read 1 byte? And then you are looking at 6 bytes....
      Mark

      Comment


      • #4


        The issue that I think I am having is that in my code the read() function does not seem to read back the commands sent from the camera. I verified that the SYNC command was going out and the camera responding with the ACK and SYNC commands with an oscilloscope. The Baud rate is set to 57600 as well.

        Note: I did vary the buffer and read size to 6 and 12 and still not luck. I did replace the && (ands) with || (ors) and the buffer read the 0x00's but not the other characters like 0xAA, 0x0E, etc.
        The uCam RS-232 camera will be connected to a TS7800 single board computer where it will receive it's signals. At this time, I believe it is just a program issue since I verified the camera is in fact working through the oscilloscope and the uCam test software.
        I just started using Linux and Eclipse this semester to start developing and compiling code so I may have something wrong in the code. I appreciate any help you can give me. Thank you.

        Brian
        Color Based Vision Navigation System - Biomimetic Unmanned Underwater Vehicle

        Comment

        Working...
        X