test_manual_screen.cpp
Go to the documentation of this file.
00001 
00031 #include <stdio.h>
00032 #include "sr_communications/serial_port/rs232.h"
00033 
00034 #define DEF_SERIAL_PORT_FILE    "/dev/ojo"
00035 #define DEF_SEMFILE             "semfile.sem"
00036 
00037 // If you want to use an specific command to test the communication, this is the default one :)
00038 #define DEFAULT_COMMAND         "U"
00039 
00040 int main(int argc, char **argv)
00041 {
00042     Rs232 rs;
00043 
00044     char spFile[SP_MAX_FILENAME_SIZE];
00045     char command[SP_MSG_SIZE], response[SP_MSG_SIZE];
00046 
00047     int i;
00048     int result;
00049 
00050     semaphore_t sem;        // Semaphore data type
00051 
00052     int baud = 9600;        // Baudrate
00053 
00054     sprintf(sem.semFile, DEF_SEMFILE);
00055     sprintf(spFile, DEF_SERIAL_PORT_FILE);
00056 
00057     printf("Semaphore file: %s\n", sem.semFile);
00058     printf("Serial port file: %s\n", spFile);
00059 
00060     printf("Initializing the communication with the serial port...\n");
00061     if (rs.initCommunication(baud, spFile, sem.semFile) < ERR_NOERR) {
00062         perror("Error in initSerial\n");
00063         exit(-1);
00064     }
00065 
00066     sleep(1);
00067 
00068     sprintf(command, DEFAULT_COMMAND);
00069     printf("Sending command through the serial port...\n");
00070     if (rs.writeToRS232(command, 1/*sizeofchar(command)*/) < ERR_NOERR) {
00071         perror("Error writing in the serial port\n");
00072         exit(-1);
00073     }
00074 
00075     printf("Waiting the response...\n");
00076     bzero(response, SP_MSG_SIZE);
00077     result = rs.readSerial(response, 1);
00078     if (result < ERR_NOERR) {
00079         printf("The error was: %d\n", result);
00080         perror("Error reading from the serial port\n");
00081         exit(-1);
00082     }
00083 
00084     //sumamos 0x30 para que sean caracteres imprimibles
00085     for (i = 0; i < result; i++) {
00086         response[i] += 0x30;
00087     }
00088     printf("Response received.\n");
00089     printf("The command was: %s\n", command);
00090     printf("And the received response was: %s\n", response);
00091     printf("\n");
00092 
00093     sleep(5);
00094 
00095     bzero(command, SP_MSG_SIZE);
00096     command[0] = 0x56;
00097     command[1] = 0x01;
00098 
00099     printf("Sending command through the serial port...\n");
00100     if (rs.writeToRS232(command, 2/*sizeofchar(command)*/) < ERR_NOERR) {
00101         perror("Error writing in the serial port\n");
00102         exit(-1);
00103     }
00104 
00105     printf("Waiting the response...\n");
00106     bzero(response, SP_MSG_SIZE);
00107     result = rs.readSerial(response, 5);
00108     if (result < ERR_NOERR) {
00109         printf("The error was: %d\n", result);
00110         perror("Error reading from the serial port\n");
00111         //exit (-1);
00112     }
00113 
00114     //sumamos 0x30 para que sean caracteres imprimibles
00115     for (i = 0; i < result; i++) {
00116         response[i] += 0x30;
00117     }
00118     printf("Response received.\n");
00119     printf("The command was: %s\n", command);
00120     printf("And the received response was: %s\n", response);
00121     printf("\n");
00122 
00123     sleep(5);
00124 
00125     bzero(response, SP_MSG_SIZE);
00126     bzero(command, SP_MSG_SIZE);
00127     command[0] = 0x56;
00128     command[1] = 0x01;
00129 
00130     printf("(Asking) Sending command through the serial port and waiting the response...\n");
00131     if (rs.askToSerial(command, 2, response, 5) < ERR_NOERR) {
00132         perror("Error asking in the serial port\n");
00133         exit(-1);
00134     }
00135 
00136     //sumamos 0x30 para que sean caracteres imprimibles
00137     for (i = 0; i < result; i++) {
00138         response[i] += 0x30;
00139     }
00140     printf("Response received.\n");
00141     printf("The command was: %s\n", command);
00142     printf("And the received response was: %s\n", response);
00143     printf("\n");
00144 
00145     sleep(10);
00146 
00147     return 0;
00148 }


sr_communications
Author(s): Raul Perula-Martinez , Victor Gonzalez
autogenerated on Wed Aug 26 2015 16:26:47