/* * sends a command and receives the answer to this * buffer: Strores the answer * length: length of answer or null */ int mdc800_rs232_sendCommand (char* command, char* buffer, int length) { char answer; int fault=0; struct timeval timeout; int i; if (mdc800_io_device_handle == 0) { printCError ("(mdc800_rs232_sendCommand) Camera is not open !\n"); return 0; } printFnkCall ("(mdc800_rs232_sendCommand) id:%i (%i,%i,%i),answer:%i\n",command[1],command[2],command[3],command[4],length); timeout.tv_usec = MDC800_DEFAULT_COMMAND_DELAY*1000; timeout.tv_sec = 0; select (1 , NULL, NULL, NULL, &timeout); gpio_set_timeout (mdc800_io_device_handle, MDC800_DEFAULT_TIMEOUT ); // Send command for (i=0; i<6; i++) { if (gpio_write (mdc800_io_device_handle, &command[i] ,1) != GPIO_OK) { printCError ("(mdc800_rs232_sendCommand) sending Byte %i fails!\n",i); fault=1; } if (gpio_read (mdc800_io_device_handle,&answer,1) != GPIO_OK) { printCError ("(mdc800_rs232_sendCommand) receiving resend of Byte %i fails.\n",i); fault=1; } if (command [i] != answer) { printCError ("(mdc800_rs232_sendCommand) Byte %i differs : send %i, received %i \n",i,command[i],answer); fault=1; } } if (fault) return 0; // Receive answer if (length) { // Some Commands needs a download. switch (command[1]) { case COMMAND_GET_IMAGE: case COMMAND_GET_THUMBNAIL: if (!mdc800_rs232_download (buffer,length)) { printCError ("(mdc800_rs232_sendCommand) download of %i Bytes fails.\n",length); fault=1; } break; default: if (!mdc800_rs232_receive (buffer,length)) { printCError ("(mdc800_rs232_sendCommand) receiving %i Bytes fails.\n",length); fault=1; } } } if (fault) return 0; // commit if (!(command[1] == COMMAND_CHANGE_RS232_BAUD_RATE)) if (!mdc800_rs232_waitForCommit (command[1])) { printCError ("(mdc800_rs232_sendCommand) receiving commit fails.\n"); fault=1; } if (fault) return 0; // all right return 1; }
/* * sends a command and receives the answer to this * buffer: Strores the answer * length: length of answer or null */ int mdc800_rs232_sendCommand(GPPort *port,unsigned char* command, unsigned char* buffer, int length) { char answer; int fault=0,ret; int i; printFnkCall ("(mdc800_rs232_sendCommand) id:%i (%i,%i,%i),answer:%i\n",command[1],command[2],command[3],command[4],length); usleep(MDC800_DEFAULT_COMMAND_DELAY*1000); gp_port_set_timeout (port, MDC800_DEFAULT_TIMEOUT ); /* Send command */ for (i=0; i<6; i++) { if (gp_port_write (port, (char*)&command[i] ,1) < GP_OK) { printCError ("(mdc800_rs232_sendCommand) sending Byte %i fails!\n",i); fault=1; } ret = gp_port_read (port,&answer,1); if ( ret != 1) { printCError ("(mdc800_rs232_sendCommand) receiving resend of Byte %i fails.\n",i); fault=1; } if (command [i] != answer) { printCError ("(mdc800_rs232_sendCommand) Byte %i differs : send %i, received %i \n",i,command[i],answer); fault=1; } } if (fault) return GP_ERROR_IO; /* Receive answer */ if (length) { /* Some Commands needs a download. */ switch (command[1]) { case COMMAND_GET_IMAGE: case COMMAND_GET_THUMBNAIL: if (!mdc800_rs232_download(port,buffer,length)) { printCError ("(mdc800_rs232_sendCommand) download of %i Bytes fails.\n",length); fault=1; } break; default: if (!mdc800_rs232_receive(port,buffer,length)) { printCError ("(mdc800_rs232_sendCommand) receiving %i Bytes fails.\n",length); fault=1; } } } if (fault) return GP_ERROR_IO; /* commit */ if (!(command[1] == COMMAND_CHANGE_RS232_BAUD_RATE)) { if (!mdc800_rs232_waitForCommit(port,command[1])) { printCError ("(mdc800_rs232_sendCommand) receiving commit fails.\n"); fault=1; } } if (fault) return GP_ERROR_IO; return GP_OK; }