Hey. Im just adding in some tips in case any one else stumbles upon this (as I just did).
About a year ago i made arduino code for connecting to Canon lenses (that worked), and Fuji lenses, that i never got to test.
When looking at my code, i see that i first connect to the lens, checking response code, then asking for lens data if connection was successful. It looks to me like you skipped straight to the "asking for data" part.
Other than that. Adding a 1ms delay when spec is saying MAX 1ms sounds bad. The 1ms delay of an arduino can easily be a bit longer than 1ms, so i would play it safe and use delay_us(500) instead.
Here is my UNTESTED code:
// UDP STUFF
#include <SPI.h> // needed for Arduino versions later than 0018
#include <Ethernet2.h>
#include <EthernetUdp2.h> // UDP library from: bjoern@cs.stanford.edu 12/30/2008
EthernetUDP Udp;
IPAddress remote_ip(10, 0, 10, 10);
unsigned int localPort = 4444;
//END UDP STUFF
//#include <SoftwareSerial_rs422.h>
#include <SoftwareSerial.h>
#include "stdio.h"
#define ERROR_RETURN_NUM -1
//ip and port stuff
#define CAMERA_NUM 10
unsigned int remote_port = 4444+CAMERA_NUM;
IPAddress local_ip(10, 0, 10, 240+CAMERA_NUM);
byte mac[] = {0x90, 0xA2, 0xDA, 0x10, 0xE5, 0xD0+CAMERA_NUM}; //this is on a sticker on the backside
//config
enum message_types {CANON,CMOTION,FUJI,MOSYS};
#define UDP_MESSAGE_TYPE CMOTION
#define START_STATE HANDSHAKE
#define UDP_ENABLED 0
#define ENABLE_DEBUG_PRINT 1
#define INPUT_BUF_LEN 128
#define OUTPUT_BUF_LEN 64
#define experimental_rx_wait 1
#define experimental_rx_wait_us 100
#define LOOP_DELAY 5 //5ms delay gives ~120 hz
#define RETRY_COUNT_MAX 10
#define RETRY_DELAY_MS 100
#define PATIENCE 4
enum tx_methods {MOXA,UDP};
#define TX_METHOD UDP
#define TX_FREQUENCY 120
#define TX_INTERVAL_US (1000000/TX_FREQUENCY)
//printing
#if ENABLE_DEBUG_PRINT
#define PRINTLN(a) (Serial.println(a))
#define PRINT(a) (Serial.print(a))
#else
#define PRINTLN(a)
#define PRINT(a)
#endif
//#include <SoftwareSerial.h>
int PIN_TX = A0;
int PIN_RX = A1;
SoftwareSerial fuji_port(PIN_RX, PIN_TX, false); // RX, TX
enum states {BOOT,HANDSHAKE,LENS_NAME,COMPLETE_INIT,BAUD_CHANGE,OPERATIONAL};
int state = START_STATE;
unsigned long prev_tx_time = 0;
int focus_position = 0;
int zoom_position = 0;
int extender_value = 0;
int iris_position = 0;
uint8_t input_buf[INPUT_BUF_LEN];
char output_buf[OUTPUT_BUF_LEN];
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
PRINTLN("Goodnight moon!");
PRINT("TX INTERVAL_US: ");PRINTLN(TX_INTERVAL_US);
// set the data rate for the SoftwareSerial port
fuji_port.begin(38400);
state = START_STATE;
udp_setup();
//TODO SET DSR PIN
main_loop();
}
void loop() { // run over and over
//request_lens_data();
//lens_read_response();
//delay(loop_delay);
}
uint8_t zero = 0;
void main_loop(){
uint8_t connect_request[3] = {0x00,0x01,0xFF};
uint8_t connect_request_response[3] = {0x00,0x01,0xFF};
uint8_t reset_request[4] = {0x01,0x01,0x00,0xFE};
uint8_t reset_request_response[4] = {0x01,0x01,0x00,0xFE};
//uint8_t lens_name_request[7] = {0xBE,0x80,0x81,zero,zero,zero,0xBF};
//uint8_t complete_init[6] = {0x86,0xC0,zero,zero,zero,0xBF};
uint8_t lens_focus_request[3] = {0x00,0x32,0xCE};
uint8_t lens_zoom_request[3] = {0x00,0x31,0xCF};
uint8_t lens_iris_request[3] = {0x00,0x30,0xD0};
//uint8_t lens_extender_request[3] = {0x94,0xC0,0xBF};
//uint8_t lens_baud_request[3] = {0x9F,0xC1,0xBF};
int retry_count = RETRY_COUNT_MAX;
while(1){
retry_count = RETRY_COUNT_MAX;
while (state == HANDSHAKE){
PRINTLN("Doing handshake");
lens_send_cmd(connect_request,3);
//TODO sjekk om den sender reset command eller ikke
if (lens_confirm_response(connect_request_response, 3)){
state = OPERATIONAL;
retry_count = RETRY_COUNT_MAX;
}
delay(RETRY_DELAY_MS);
}
while(state == OPERATIONAL){
while (micros()-prev_tx_time <TX_INTERVAL_US){
//wait
}
lens_send_cmd(lens_focus_request,3);
focus_position = lens_read_response();
lens_send_cmd(lens_zoom_request,3);
zoom_position = lens_read_response();
lens_send_cmd(lens_iris_request,3);
iris_position = lens_read_response();
//PRINT("iris_position: ");PRINT(iris_position);
//TODO check response
bool extraction_success = true;
if ((focus_position + zoom_position + iris_position) == (3*ERROR_RETURN_NUM)){
state = HANDSHAKE;
retry_count = RETRY_COUNT_MAX;
PRINTLN("Response invalid, Going to handshake");
break;
}
create_and_send_packet();
prev_tx_time = micros();
//delay(LOOP_DELAY);
}
}
}
bool print_rx(){
while(!fuji_port.available()){
//wait
}
while (fuji_port.available()) {
//PRINT(fuji_port.read(),HEX);
delay(experimental_rx_wait); //to catch all bytes and not quit too early. Can wait for oxBF as well
}
PRINTLN("");
return true;
}
int lens_read_response(){
uint8_t counter = 0;
memset(input_buf,0,INPUT_BUF_LEN);
uint8_t tempy,tempy2;
uint8_t msg_len,msg_type,msg_checksum;
int return_value = 0;
//wait for data
int patience_counter = PATIENCE;
while (!fuji_port.available() && (patience_counter-- > 0)){
//PRINTLN("Waiting for data");
delay(1);
}
while (fuji_port.available()) {
input_buf[counter] = (uint8_t)fuji_port.read();
counter++;
if (counter >=INPUT_BUF_LEN) break;
delayMicroseconds(experimental_rx_wait_us); //to catch all bytes and not quit too early. Can wait for oxBF as well
}
//PRINTLN(counter);
if (counter >=4){
msg_len = input_buf[0];
msg_type = input_buf[1];
uint8_t checksum_index = msg_len+2;
if (checksum_index <=counter){
msg_checksum = input_buf[checksum_index];
}
if (msg_len !=2){
PRINTLN("UNSUPPORTED MESSAGE LENGTH");
return ERROR_RETURN_NUM;
}
if ((msg_type > 0x32) || (msg_type <0x30)){
PRINTLN("UNSUPPORTED MESSAGE TYPE");
return ERROR_RETURN_NUM;
}
tempy = input_buf[2];
return_value = tempy <<8;
tempy2 = input_buf[3];
return_value += tempy2;
return return_value;
}
else {
PRINTLN("ERROR IN RECEIVING FROM CAMERA");
return ERROR_RETURN_NUM;
}
}