TTL Serial Camera - Nothing seems to work

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
maveck
 
Posts: 6
Joined: Mon Nov 14, 2011 4:46 pm

Re: TTL Serial Camera - Nothing seems to work

Post by maveck »

Thank you, Mr, It's a really good study, in my case, I'm having still the same issue:

if (r[0] == 0x76 and r[1] == SERIALNUM and r[2] == b and r[3] == 0x00):IndexError: list index out of range

I took your suggestions and the code that I'm compiling is:

# python code for interfacing to VC0706 cameras and grabbing a photo
# pretty basic stuff
# written by ladyada. MIT license

import serial
import time

BAUD = 38400
PORT = "COM15"
TIMEOUT = 0.2

SERIALNUM = 0 # start with 0

COMMANDSEND = 0x56
COMMANDREPLY = 0x76
COMMANDEND = 0x00

CMD_GETVERSION = 0x11
CMD_RESET = 0x26
CMD_TAKEPHOTO = 0x36
CMD_READBUFF = 0x32
CMD_GETBUFFLEN = 0x34

FBUF_CURRENTFRAME = 0x00
FBUF_NEXTFRAME = 0x01
FBUF_STOPCURRENTFRAME = 0x00

getversioncommand = [COMMANDSEND, SERIALNUM, CMD_GETVERSION, COMMANDEND]
resetcommand = [COMMANDSEND, SERIALNUM, CMD_RESET, COMMANDEND]
takephotocommand = [COMMANDSEND, SERIALNUM, CMD_TAKEPHOTO, 0x01, FBUF_STOPCURRENTFRAME]
getbufflencommand = [COMMANDSEND, SERIALNUM, CMD_GETBUFFLEN, 0x01, FBUF_CURRENTFRAME]

def checkreply(r, b):
r = map (ord, r)
#print 'checkreply: completed map, len=', len(r)
#print 'r =', r
if (r[0] == 0x76 and r[1] == SERIALNUM and r[2] == b and r[3] == 0x00):
return True
print 'checkReply() failed'
return False

def reset():
cmd = ''.join (map (chr, resetcommand))
s.write(cmd)
reply = s.read(100)
r = list(reply)
if checkreply(r, CMD_RESET):
return True
print 'reset(): failure'
return False

def getversion():
cmd = ''.join (map (chr, getversioncommand))
s.write(cmd)
reply = s.read(16)
r = list(reply);
if checkreply(r, CMD_GETVERSION):
return True
return False

def takephoto():
cmd = ''.join (map (chr, takephotocommand))
s.write(cmd)
reply = s.read(5)
r = list(reply);
if (checkreply(r, CMD_TAKEPHOTO) and r[3] == chr(0x0)):
return True
return False

def getbufferlength():
cmd = ''.join (map (chr, getbufflencommand))
s.write(cmd)
reply = s.read(9)
r = list(reply);
if (checkreply(r, CMD_GETBUFFLEN) and r[4] == chr(0x4)):
l = ord(r[5])
l <<= 8
l += ord(r[6])
l <<= 8
l += ord(r[7])
l <<= 8
l += ord(r[8])
return l

return 0

readphotocommand = [COMMANDSEND, SERIALNUM, CMD_READBUFF, 0x0c, FBUF_CURRENTFRAME, 0x0a]

READSIZE=2048

def readbuffer(bytes):
addr = 0
photo = []

while (addr < bytes + READSIZE):
command = readphotocommand + [(addr >> 24) & 0xFF, (addr >> 16) & 0xFF,
(addr >> 8) & 0xFF, addr & 0xFF]
command += [0, 0, (READSIZE>>8) & 0xff, READSIZE & OXFF] # 32 bytes at a time
command += [0x10,0] # delay of 20ms (was 10 ms)
#print map(hex, command)
cmd = ''.join(map (chr, command))
s.write(cmd)
reply = s.read(READSIZE+5)
r = list(reply)
if (len(r) != READSIZE+5):
continue
#print r
if (not checkreply(r, CMD_READBUFF)):
print "ERROR READING PHOTO"
exit()
return
photo += r[5:]
addr += READSIZE
print 'photo len=', len(photo)
return photo



######## main

s = serial.Serial(PORT, baudrate=BAUD, timeout=TIMEOUT)

reset()

time.sleep(2)

if (not getversion()):
print "Camera not found"
exit
print "VC0706 Camera found"

if takephoto():
print "Snap!"

bytes2read = getbufferlength()

print bytes2read, "bytes to read"

photo = readbuffer(bytes2read)

f = open("photo.jpg", 'w')
photodata = ''.join(photo)
f.write(photodata)
f.close()
#print length(photo)



I'm using Python 2.7.2 running in Win 7, what version are you using Mr ?

Thank you

mrtundraman
 
Posts: 41
Joined: Tue Apr 10, 2012 6:00 pm

Re: TTL Serial Camera - Nothing seems to work

Post by mrtundraman »

I am running Python 2.7 on Ubuntu.

Here's my code which works for me. https://raw.github.com/douggilliland/Do ... age0706.py

You may need to determine which function is failing. I can see it's the checkreply() function but you need to post the entire dump to see which function is dying since there are a bunch of locations which call checkReply().

I thought that my checkreply fail was due to the reset function but it was a later call that was the problem.

maveck
 
Posts: 6
Joined: Mon Nov 14, 2011 4:46 pm

Re: TTL Serial Camera - Nothing seems to work

Post by maveck »

Hi Mr;

This is the output of the complete error:

Traceback (most recent call last):
File "C:\Users\Personal\Documents\Trabajo de grado\phto.py", line 128, in <module>
reset()
File "C:\Users\Personal\Documents\Trabajo de grado\phto.py", line 54, in reset
if checkreply(r, CMD_RESET):
File "C:\Users\Personal\Documents\Trabajo de grado\phto.py", line 38, in checkreply
if (r[0] == 0x76 and r[1] == SERIALNUM and r[2] == b and r[3] == 0x00):
IndexError: list index out of range

mrtundraman
 
Posts: 41
Joined: Tue Apr 10, 2012 6:00 pm

Re: TTL Serial Camera - Nothing seems to work

Post by mrtundraman »

OK, that helps. Your error is happening in checkreply() when it's called by reset(). That is the first time it is called. The error message is saying that the packet which should have come back from the camera wasn't returned or was too short. I had that one when my camera wasn't wired up right.

The message
if (r[0] == 0x76 and r[1] == SERIALNUM and r[2] == b and r[3] == 0x00):
IndexError: list index out of range
...that happens when the return value (r) isn't as many bytes as expected.

Seems like your camera isn't returning data. Have you checked it lately with the windows utility http://www.adafruit.com/datasheets/VC07 ... 0V1-00.exe?

If you are able to communicate with the camera and grab a picture that way, then your hardware is connected correctly.

You could try these mods to the checkreply function and see if it returns anything. It should give a length for the r value of at least 4. Otherwise the comparison won't work.

Code: Select all

def checkreply(r, b):
    r = map (ord, r)
    print 'checkreply: completed  map, len=', len(r)    #added for debug
    print 'r =', r          #added for debug
    if (r[0] == 0x76 and r[1] == SERIALNUM and r[2] == b and r[3] == 0x00):
        return True
    print 'checkReply(): failed, r=', r
    return False

mrtundraman
 
Posts: 41
Joined: Tue Apr 10, 2012 6:00 pm

Re: TTL Serial Camera - Nothing seems to work

Post by mrtundraman »

About the wiring in the first part of the tutorial http://www.ladyada.net/products/camera/. It seems counter-intuitive to wire it like it is shown, but the wiring is actually correct. The Tx of the camera really does get connected to the out arrow of the Arduino.

Image

I think that the tutorial could use an explanation for those who got caught up like I did by thinking "too hard". Here's the deal..

The arrows on the D0 and D1 pins on the Arduno are for when the ATMEGA is driving the pins. For this "null" sketch, the USB interface is driving the lines so the arrows on the board are actually backwards.

User avatar
moallen
 
Posts: 13
Joined: Thu Nov 08, 2012 6:23 pm

Re: TTL Serial Camera - Nothing seems to work

Post by moallen »

MrTundraMan

Been trying to get my VC0706 working with Adafruit's Python script and have read all of your posts. Everything does seem to work ok from the CommTool, but I get the following error running the Python script.

Camera not found
VC0706 Camera found
Snap!
8240 bytes to read
ERROR READING PHOTO

Traceback (most recent call last):
File "C:\Python27\getimage0706.py", line 137, in <module>
photodata = ''.join(photo)
TypeError

When I use the script you referenced up on github with you changes/additions, I get this msg:

reset: Successful
checkReply(): failed, r= [86, 67, 48, 55, 48, 51, 32, 49, 46, 48, 48, 13, 10, 67, 116, 114]
Camera not found
VC0706 Camera found
checkReply(): failed, r= [108, 32, 105, 110, 102]
checkReply(): failed, r= [114, 32, 101, 120, 105, 115, 116, 13, 10]
0 bytes to read

I think I'm seeing the same condition you have already talked about where it is picking up bytes from the previous read, but I've tried the changes in your script without any luck. Hoping you might have some additional ideas to try - thanks.

Btw, I'm using Python 2.7.2 on a Win7 machine.

mrtundraman
 
Posts: 41
Joined: Tue Apr 10, 2012 6:00 pm

Re: TTL Serial Camera - Nothing seems to work

Post by mrtundraman »

moallen - Not really sure what the difference is but it could be Linux vs windoze?

It's been a long time since I messed with this, but I do remember the delay time being important:

command += [0x10,0] # delay of 40.96 ms (was 10 ms)

Also, I remember the block size making a big difference:

READSIZE = 4096

Have you tried changing those lines to different values than what I have working?

Wish I could help more.

User avatar
moallen
 
Posts: 13
Joined: Thu Nov 08, 2012 6:23 pm

Re: TTL Serial Camera - Nothing seems to work

Post by moallen »

Thanks for getting back so quickly! Yes, I have tried changing the delay and block sizes. Nothing made a difference that I could see. I should also mention I have used RealTerm to send the very same commands in the Python script to the camera a command at a time, and all seems to work just fine. So, I'm pretty sure it's some kind of timing issue. Maybe I'll try adding delays at different points in the script (to slow windows down... :lol: ) and see what happens.

Nearpoint
 
Posts: 38
Joined: Fri Jan 13, 2012 6:40 pm

Re: TTL Serial Camera - Nothing seems to work

Post by Nearpoint »

Do you think the issue is that the serial ports are disabled for program use on the raspberry pi by default? Maybe we just need to enable the serial ports? I was getting the same issue as you with the errors in the Python script. I am going to try enabling the serial ports for program use with these instructions and see if it works: http://www.hobbytronics.co.uk/raspberry-pi-serial-port

Nearpoint
 
Posts: 38
Joined: Fri Jan 13, 2012 6:40 pm

Re: TTL Serial Camera - Nothing seems to work

Post by Nearpoint »

I connected the Weatherproof TTL camera to the Raspberry Pi and when running the script it fails at the function 'def getVersion():'

In terminal it prints 'Camera not found'

The camera is not reporting itself, what is going on?

I have Tx wire connected directly to Tx and Rx wire to Rx.

Nearpoint
 
Posts: 38
Joined: Fri Jan 13, 2012 6:40 pm

Re: TTL Serial Camera - Nothing seems to work

Post by Nearpoint »

Alright so I have debugged the program to the point where I know where it is failing.

It is failing here:

Code: Select all

def checkreply(r, b):
	r = map( ord, r )
	if( r[0] == COMMANDREPLY and r[1] == SERIALNUM and r[2] == b and r[3] == 0x00):
		return True
	return False
r is [86, 0, 38, 0]

and it expects [118, 0, 38, 0]

I know this because I printed out both array r and COMMANDREPLY, and b each time the method was called.

I check and 86 in hexadecimal is 0x56 and 118 in hexadecimal is 0x76

0x56 is COMMANDSEND and 0x76 is COMMANDREPLY

It seems like COMMANDSEND and COMMANDRELY are reversed....

when I switch there values, the values in the array are switched to with the same mismatched error.

Whats going on!?

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”