icc
Posts: 1
Joined: Fri May 10, 2013 2:55 am

Re: TTL Serial Camera

Fri May 10, 2013 2:58 am

Did you ever get this working Feroze?

sujith
Posts: 1
Joined: Mon Jul 15, 2013 1:45 pm

Re: TTL Serial Camera

Mon Jul 15, 2013 1:49 pm

icc wrote:Did you ever get this working Feroze?
I had very similar issue. Turned out to be a bad solder. Check for shorts.

dany246
Posts: 11
Joined: Wed Jul 17, 2013 3:55 pm

Re: TTL Serial Camera

Wed Jul 17, 2013 4:02 pm

Hi, thanks a lot for this tutorial i got it working in 2 minutes :D , i have two questions, i see that the resolution of a picture is 320x240 is there a way to increase it (i have this camera http://www.adafruit.com/products/397 and as you can see it arrive up to 640x480) and the second question, to change the name of a picture do i have only to change the "f = open( "photo.jpg", 'w' )" line? (i would like to save the picture as date-time.jpg)

Anyway thanks a lot, Best Regards :D

feroze
Posts: 27
Joined: Wed Mar 27, 2013 5:20 pm

Re: TTL Serial Camera

Tue Dec 30, 2014 10:01 am

Over a year later, I decided to try again and this time I got it to work!

2 issues:

1) Need tweaks to the software
2) Serial and Power were not sharing the same ground connection.

1) The first issue was a few software tweaks. After calling reset() in main add time.sleep(2). In the function readbuffer, find the line where you append the delay to the command and change it to 'command += [0x10, 0]

Here is the full code I used:

Code: Select all

#!/usr/bin/python
# python code for interfacing to VC0706 cameras and grabbing a photo
# pretty basic stuff
# written by ladyada. MIT license
# revisions for Raspberrry Pi by Gordon Rush

import serial 
import time

BAUD = 38400
# this is the port on the Raspberry Pi; it will be different for serial ports on other systems.
PORT = "/dev/ttyAMA0"

TIMEOUT = 0.5    # I needed a longer timeout than ladyada's 0.2 value
SERIALNUM = 0    # start with 0, each camera should have a unique ID.

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 )
	if( r[0] == COMMANDREPLY and r[1] == SERIALNUM and r[2] == b and r[3] == 0x00):
		return True
	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
	return False

def getversion():
	cmd = ''.join( map( chr, getversioncommand ))
	s.write(cmd)
	reply = s.read(16)
	r = list(reply)
	#print r
	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)
	# print r
	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]


def readbuffer(bytes):
	addr = 0   # the initial offset into the frame buffer
	photo = []

	# bytes to read each time (must be a mutiple of 4)
	inc = 8192

	while( addr < bytes ):
 		# on the last read, we may need to read fewer bytes.
                chunk = min( bytes-addr, inc );

		# append 4 bytes that specify the offset into the frame buffer
		command = readphotocommand + [(addr >> 24) & 0xff, 
				(addr>>16) & 0xff, 
				(addr>>8 ) & 0xff, 
				addr & 0xff]

		# append 4 bytes that specify the data length to read
		command += [(chunk >> 24) & 0xff, 
				(chunk>>16) & 0xff, 
				(chunk>>8 ) & 0xff, 
				chunk & 0xff]

		# append the delay
		command += [0x10,0]

		# print map(hex, command)
		print "Reading", chunk, "bytes at", addr

		# make a string out of the command bytes.
		cmd = ''.join(map(chr, command))
	        s.write(cmd)

		# the reply is a 5-byte header, followed by the image data
		#   followed by the 5-byte header again.
		reply = s.read(5+chunk+5)

 		# convert the tuple reply into a list
		r = list(reply)
		if( len(r) != 5+chunk+5 ):
			# retry the read if we didn't get enough bytes back.
			print "Read", len(r), "Retrying."
			continue

		if( not checkreply(r, CMD_READBUFF)):
			print "ERROR READING PHOTO"
			return
		
		# append the data between the header data to photo
		photo += r[5:chunk+5]

		# advance the offset into the frame buffer
		addr += chunk

	print addr, "Bytes written"
	return photo


######## main

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

resetResult = reset()
if (resetResult == False):
	print "reset failed"
	exit(0)

time.sleep(2)

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

print "VC0706 Camera found"

if takephoto():
	print "Snap!"

bytes = getbufferlength()

print bytes, "bytes to read"

photo = readbuffer( bytes )

f = open( "photo.jpg", 'w' )

photodata = ''.join( photo )

f.write( photodata )

f.close()
The second issue was that I was powering the camera using the Arduino instead of using the Pi to power the camera, even though I had the rx & tx wires hooked up to the Pi.

I did this because I was already using the 5v power on the Pi for something else so I wanted to power the camera from another source. I had the power and ground for the camera going to my Arduino and the serial connections going to the Pi.

When I moved the power and ground cables to the raspberry pi, then the camera started to work!

I believe I know why this was causing an error. This is because the camera needed to share the same ground for serial and power connections. Because I was using the power and ground from the arduino, the serial connections had no way to ground. When I hooked up the ground to the raspberry pi, the power and serial connections shared this ground. So it works. :)

LinuxCircle
Posts: 26
Joined: Sat May 04, 2013 2:03 am
Contact: Website

Re: TTL Serial Camera

Thu Mar 24, 2016 1:10 pm

This is my screen output

pi@raspberrypi:~/Projects $ sudo python raspi_camera.py
['v', '\x00', '\x11', '\x00', '\x0b', 'V', 'C', '0', '7', '0', '3', ' ', '1', '.', '0', '0']
VC0706 Camera found
Snap!
12228 bytes to read
Reading 8192 bytes at 0
Reading 4036 bytes at 8192
12228 Bytes written

And the photo.jpg beautifully saved in that same folder.

I realised the camera's IR are lit up when 5V and GND are connected.
What went wrong: RX supposed to be TX, TX supposed to be RX, so just swap green and white cable, it worked like a charm!
www.linuxcircle.com

LinuxCircle
Posts: 26
Joined: Sat May 04, 2013 2:03 am
Contact: Website

Re: TTL Serial Camera

Tue Apr 05, 2016 2:55 am

You can also run this VC0706 camera with Python 3:

http://www.linuxcircle.com/2016/04/05/c ... -python-3/
www.linuxcircle.com

bluelip
Posts: 2
Joined: Wed Sep 28, 2016 4:05 pm

Re: TTL Serial Camera

Wed Sep 28, 2016 4:09 pm

To help out others, who are having trouble 4 years after most others solved the problem, my issue was that the camera board was set to 115200 for me.

In the raspi_camera.py file, change the speed by modifying the BAUD variable.

#BAUD = 38400
BAUD = 115200

BTW, the randterm program is prettty sweet.

bluelip
Posts: 2
Joined: Wed Sep 28, 2016 4:05 pm

Re: TTL Serial Camera

Wed Sep 28, 2016 5:18 pm

For those that are just now playing with these cameras like myself, my issue that resulting in the camera not being recognized was that the speed was incorrect. On my board, it was set to 115200.

In the source file, make these changes (if you're experiencing the same issue):

#BAUD = 38400
BAUD = 115200


Best o'luck!

k.a.z.mcc95
Posts: 2
Joined: Sun Aug 28, 2016 5:45 pm

Re: TTL Serial Camera

Fri Sep 30, 2016 4:31 pm

I have just hooked up the TTL camera to my raspberry pi.

When I run the code at the top of the page on my RPi I get

Code: Select all

[128, 2,0,6,132,1]
[128, 2,0,6,132,1]
Camera not found
I run it again, without changing anything, and the output is now:

Code: Select all

Traceback (most recent call last):
  File "code.py", line 150 in <module>
    reset()
  File "code.py", line 50, in reset
    if checkreply(r, CMD_RESET):
  File "code.py", line 41, in checkreply
    if (r[0] == COMMANDREPLY and r[1] == SERIALNUM  and r[2] == b and r[3] ==0x00):
IndexError: list index out of range
I don't understand how the output changes when the input hasn't!

anshumantech
Posts: 51
Joined: Fri Aug 05, 2016 5:20 am

Re: TTL Serial Camera

Fri Feb 16, 2018 7:34 am

I am trying to interface 0.3 megapixel Serial JPEG camera (By ZM technology) with PC (using RS232) but it didn’t respond on hyperterminal. Can you suggest any solution?
I have attached the image of 0.3 megapixel Serial JPEG camera.
Attachments
jpeg-color-camera-serial-uart-interface-250x250.jpg
jpeg-color-camera-serial-uart-interface-250x250.jpg (9.42 KiB) Viewed 1263 times

Return to “Automation, sensing and robotics”