ragishtin
Posts: 12
Joined: Fri Jan 24, 2020 7:30 am

2 pi's communicating serially through an optical fiber link

Fri Jan 24, 2020 8:16 am

Hello there,

I'm working on an application where I need to send data from one pi to another over an optical fiber link ..

I have done this before with arduino .. but now I need to send bigger data like colored images or videos (maybe streaming a video) ..

Does anyone have a recommendation on how to start ?

Thanks in advance ..
Ragy Samy ..

User avatar
B.Goode
Posts: 9560
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: 2 pi's communicating serially through an optical fiber link

Fri Jan 24, 2020 11:28 am

Welcome to the Raspberry Pi forums.


If you already have the fibre in place and you want to reliably stream video as a network service I suggest looking into using a pair of utp/fo media converters.


Something like the devices available here: https://www.lanshack.com/UTP-to-Multimo ... s-C87.aspx


(That was simply the first viable result from an Internet search, with no recommendation of either the supplier or the manufacturers.)

PiGraham
Posts: 3778
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: 2 pi's communicating serially through an optical fiber link

Fri Jan 24, 2020 11:42 am

I agree with B.Goode that the obvious choice is to use network over fibre using off the shelf devices.

You can get RS232 to fibre adaptors but those are likely to be slower, less flexible and offer less support for data transfer.

Maybe you could roll your own using something like this
https://uk.rs-online.com/web/p/fibre-op ... s/0450846/

ragishtin
Posts: 12
Joined: Fri Jan 24, 2020 7:30 am

Re: 2 pi's communicating serially through an optical fiber link

Tue Feb 25, 2020 10:00 am

I've found this receiver that can receive data over an optical link and convert it to electrical signals ..

https://www.digikey.com/product-detail/ ... D/1990454

I've changed my plan a little and it is now to use serial communication between the 2 Pi's to send a picture from one to the other ..

The concept of the project will be as follows:

*The transmitter Pi will be connected to a camera and a 7'' touch screen.
*A Python code should be written with a GUI that has 3 buttons:
-One to open the camera preview in a frame on the GUI.
-Another to capture the photo.
-And the third is to start sending the photo serially via the optical fiber link through the transmitter of the receiver in the above link.
*The receiver Pi should then receive the transmitted photo, put it in a file and just displays it on the screen connected to it.

Unfortunately, I'm new to both Python and Raspberry Pi .. so now my questions are:

1 - At the transmitter side, is it possible to send the photo directly using serial communication or I'll need to convert it to an array first ?
2 - At the receiver side, if the photo is received an array how to convert it back again to a photo in order to open it.
3 - Any recommendations about accessing file and sending data serially with Raspberry Pi will very much appreciated.

Thanks in advance ..
Ragy Samy ..

richrussell
Posts: 13
Joined: Thu Jan 16, 2020 10:52 am

Re: 2 pi's communicating serially through an optical fiber link

Tue Feb 25, 2020 10:20 am

Given that you're putting the data in a file at the receiving end, maybe it's an idea to put it in a file at the sending end and then send it using a standard network connection over the fibre. Just thinking that unless you have a desire to write serial transmission routines, it might be easier to use the efficient, error correcting, reliable ethernet that's already there.

eg Use the ethernet sockets and fibre converters to connect the two Pis, then use scp to copy the file from one to the other. The receiving one then just needs to monitor a directory for a new file appearing before opening it and displaying it.

hippy
Posts: 6836
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: 2 pi's communicating serially through an optical fiber link

Tue Feb 25, 2020 11:36 am

If you put your capture image into a file on the sender, you can then transfer that file 'as is' and have it appear on the receiver. For the sender -

Code: Select all

with open("cameraimage.jpg", "rb") as fSrc:
  buffer = fSrc.read()
  SendLong(len(buffer)
  for byte in buffer:
    SendByte(byte)
For the receiver -

Code: Select all

with open("savedimage.jpg", "wb") as fDst:
  numberOfBytes = ReadLong()
  for n in range(numberOfBytes):
    fDst.write(ReadByte())
There are more robust ways to pass that file over but that's the basic principle.

ragishtin
Posts: 12
Joined: Fri Jan 24, 2020 7:30 am

Re: 2 pi's communicating serially through an optical fiber link

Tue Feb 25, 2020 12:22 pm

Thank you guys for your concern :)

@richrussell,
I need to use serial communication because it's a requirement in the project even if it's not the best way ..
However, I'll leave using the network as a last option if serial communication didn't work .. I'll then be obligated to ignore one of the project's requirements for the sake of eventually delivering a project that works ..

@hippy,
Would you please direct me to a place where I can find some details about the codes you posted .. I couldn't understand them and what is the function of each line so that I be able to adapt them to my application ..

richrussell
Posts: 13
Joined: Thu Jan 16, 2020 10:52 am

Re: 2 pi's communicating serially through an optical fiber link

Tue Feb 25, 2020 1:50 pm

OK, fair enough. You didn't state that originally though. :)

drgeoff
Posts: 10318
Joined: Wed Jan 25, 2012 6:39 pm

Re: 2 pi's communicating serially through an optical fiber link

Tue Feb 25, 2020 4:24 pm

ragishtin wrote:
Tue Feb 25, 2020 12:22 pm
I need to use serial communication because it's a requirement in the project
Ethernet is a serial communication. :)

hippy
Posts: 6836
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: 2 pi's communicating serially through an optical fiber link

Tue Feb 25, 2020 5:24 pm

ragishtin wrote:
Tue Feb 25, 2020 12:22 pm
@hippy,
Would you please direct me to a place where I can find some details about the codes you posted .. I couldn't understand them and what is the function of each line so that I be able to adapt them to my application ..
Not sure there's anywhere which will explain the code per se. The sender opens a file, reads its data, send the length of the data to the receiver and then sends the data a byte at a time. The receiver correspondingly receives the length of the data being sent, receives that number of bytes and writes it to a file. When done the file from the sender will have become a file with the same contents on the receiver.

ragishtin
Posts: 12
Joined: Fri Jan 24, 2020 7:30 am

Re: 2 pi's communicating serially through an optical fiber link

Tue Mar 24, 2020 1:33 pm

[/quote]
Ethernet is a serial communication. :)
[/quote]

@drgeoff
let's say I need one-wire one-direction image transfer solution .. not necessarily serial communication .. do you have a recommendation ?

ragishtin
Posts: 12
Joined: Fri Jan 24, 2020 7:30 am

Re: 2 pi's communicating serially through an optical fiber link

Tue Mar 24, 2020 1:40 pm

@hippy

I tried the code you posted at the transmitter side but i get this error >>

buffer= fSrc.read()
NameError: global name 'fSrc' is not defined

Am I missing something .. a library to import for example ? .. or maybe some settings are needed before using this line ?

User avatar
davidcoton
Posts: 4647
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK

Re: 2 pi's communicating serially through an optical fiber link

Tue Mar 24, 2020 2:35 pm

Do you have a file called cameraimage.jpg ready to send?
Signature retired

ragishtin
Posts: 12
Joined: Fri Jan 24, 2020 7:30 am

Re: 2 pi's communicating serially through an optical fiber link

Wed Mar 25, 2020 9:11 am

davidcoton wrote:
Tue Mar 24, 2020 2:35 pm
Do you have a file called cameraimage.jpg ready to send?
I changed the name in this line to the name of my file .. so I would say yes I have a .jpg file ready to be sent ..

User avatar
davidcoton
Posts: 4647
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK

Re: 2 pi's communicating serially through an optical fiber link

Wed Mar 25, 2020 11:58 am

OK, so post EXACTLY the code you are using (cut and paste, not retype).
Signature retired

User avatar
DougieLawson
Posts: 37488
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: 2 pi's communicating serially through an optical fiber link

Wed Mar 25, 2020 12:38 pm

davidcoton wrote:
Wed Mar 25, 2020 11:58 am
OK, so post EXACTLY the code you are using (cut and paste, not retype).
Stop it. If you don't retype you simply can't insert the extra bugs in the code.
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

drgeoff
Posts: 10318
Joined: Wed Jan 25, 2012 6:39 pm

Re: 2 pi's communicating serially through an optical fiber link

Wed Mar 25, 2020 1:57 pm

ragishtin wrote:
Tue Mar 24, 2020 1:33 pm
Ethernet is a serial communication. :)
@drgeoff
let's say I need one-wire one-direction image transfer solution .. not necessarily serial communication .. do you have a recommendation ?
1. You cannot send a signal between two points using only one conductor between them. In some cases it is possible to use the earth as one of two conductors. For example, early telegraph systems http://web.mst.edu/~kosbar/ee3430/ff/te ... index.html

2. With only two conductors, any method of transferring an image (picture) will be serial.

PiGraham
Posts: 3778
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: 2 pi's communicating serially through an optical fiber link

Wed Mar 25, 2020 2:34 pm

drgeoff wrote:
Wed Mar 25, 2020 1:57 pm

1. You cannot send a signal between two points using only one conductor between them. In some cases it is possible to use the earth as one of two conductors. For example, early telegraph systems http://web.mst.edu/~kosbar/ee3430/ff/te ... index.html

2. With only two conductors, any method of transferring an image (picture) will be serial.
Yes, although:
referring to the topic title, you can transmit over a single optical fibre

There are "one wire" schemes that use one data line plus ground.
e.g. https://en.wikipedia.org/wiki/1-Wire

drgeoff
Posts: 10318
Joined: Wed Jan 25, 2012 6:39 pm

Re: 2 pi's communicating serially through an optical fiber link

Wed Mar 25, 2020 2:58 pm

PiGraham wrote:
Wed Mar 25, 2020 2:34 pm
drgeoff wrote:
Wed Mar 25, 2020 1:57 pm

1. You cannot send a signal between two points using only one conductor between them. In some cases it is possible to use the earth as one of two conductors. For example, early telegraph systems http://web.mst.edu/~kosbar/ee3430/ff/te ... index.html

2. With only two conductors, any method of transferring an image (picture) will be serial.
Yes, although:
referring to the topic title, you can transmit over a single optical fibre

There are "one wire" schemes that use one data line plus ground.
e.g. https://en.wikipedia.org/wiki/1-Wire
Yes, although the OP did ask "let's say I need one-wire " and the ground in 1-wire is the second of the minimum 2 conducting paths any metallic wire system requires. I used the word "earth". It is no coincidence that "earth" and "ground" are frequently used interchangeably.

Optical fibre transmission is wireless in the sense of wireless=radio. The carrier is a high frequency electromagnetic wave and the propagation is guided.

PiGraham
Posts: 3778
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: 2 pi's communicating serially through an optical fiber link

Wed Mar 25, 2020 3:15 pm

drgeoff wrote:
Wed Mar 25, 2020 2:58 pm
PiGraham wrote:
Wed Mar 25, 2020 2:34 pm
drgeoff wrote:
Wed Mar 25, 2020 1:57 pm

1. You cannot send a signal between two points using only one conductor between them. In some cases it is possible to use the earth as one of two conductors. For example, early telegraph systems http://web.mst.edu/~kosbar/ee3430/ff/te ... index.html

2. With only two conductors, any method of transferring an image (picture) will be serial.
Yes, although:
referring to the topic title, you can transmit over a single optical fibre

There are "one wire" schemes that use one data line plus ground.
e.g. https://en.wikipedia.org/wiki/1-Wire
Yes, although the OP did ask "let's say I need one-wire " and the ground in 1-wire is the second of the minimum 2 conducting paths any metallic wire system requires. I used the word "earth". It is no coincidence that "earth" and "ground" are frequently used interchangeably.

Optical fibre transmission is wireless in the sense of wireless=radio. The carrier is a high frequency electromagnetic wave and the propagation is guided.
Again, yes, although FO can be a single strand of fibre and It isn't treated as radio.

drgeoff
Posts: 10318
Joined: Wed Jan 25, 2012 6:39 pm

Re: 2 pi's communicating serially through an optical fiber link

Wed Mar 25, 2020 3:28 pm

PiGraham wrote:
Wed Mar 25, 2020 3:15 pm
Again, yes, although FO can be a single strand of fibre and It isn't treated as radio.
Fibre optic is transmission by radiated waves/photons not by flow of electrons so at the base physical level it is more akin to wireless than wired. At the higher levels of a fibre optic transmission system it does much more resemble a wired system than a radio system.

ragishtin
Posts: 12
Joined: Fri Jan 24, 2020 7:30 am

Re: 2 pi's communicating serially through an optical fiber link

Wed Mar 25, 2020 7:57 pm

I know I need a minimum of two wires with one as a ground for the signal to be referred to .. I just mis-expressed what I mean ..
I meant only one signal going from the Tx of the transmitter to the Rx of the receiver .. and the data flow will only be in this direction and the receiver will not respond ..
This signal will be transmitted through a single optical fiber ..

ragishtin
Posts: 12
Joined: Fri Jan 24, 2020 7:30 am

Re: 2 pi's communicating serially through an optical fiber link

Wed Mar 25, 2020 8:02 pm

I need some help on how to write a python script that opens an image from a given location in the Pi and starts sending it serially through the UART or any other raspberry pi's peripherals on one wire (plus the ground) ..
and another script that receives this serial data, puts it in a file and then opens the image ..

ragishtin
Posts: 12
Joined: Fri Jan 24, 2020 7:30 am

Re: 2 pi's communicating serially through an optical fiber link

Wed Mar 25, 2020 8:13 pm

davidcoton wrote:
Wed Mar 25, 2020 11:58 am
OK, so post EXACTLY the code you are using (cut and paste, not retype).

Code: Select all

def sendFile():
	with open("image.jpg", "rb") as fDst:
		buffer= fSrc.read()
		SendLong(len(buffer))
		for byte in buffer:
			SendByte(byte)

ragishtin
Posts: 12
Joined: Fri Jan 24, 2020 7:30 am

Re: 2 pi's communicating serially through an optical fiber link

Wed Mar 25, 2020 8:15 pm

this is the whole code .. containing the camera interface and the gui interface ..

Code: Select all

"""
#! /usr/bin/python3
"""
from picamera import PiCamera
from time import sleep
from Tkinter import *
import tkFont
import tkFileDialog
import RPi.GPIO as GPIO
from PIL import ImageTk,Image
import serial


ser = serial.Serial('/dev/ttyAMA0', baudrate=9600,
                    parity=serial.PARITY_NONE,
                    stopbits=serial.STOPBITS_ONE,
                    bytesize=serial.EIGHTBITS
                    )
sleep(1)

"""
GPIO.setmode(GPIO.BOARD)
GPIO.setup(40, GPIO.OUT)
GPIO.output(40, GPIO.LOW)
"""

win = Tk()
win.title('User Interface')
#win.title("First GUI")
#win.geometry('800x480')
win.geometry('1024x615')
camera = PiCamera()
camera.rotation = 180
	
font1 = tkFont.Font(family = 'Helvetica', size = 19, weight = 'bold')
font2 = tkFont.Font(family = 'Helvetica', size = 17, weight = 'bold')
	
	
def sendFile():
	with open("image.jpg", "rb") as fDst:
		buffer= fSrc.read()
		SendLong(len(buffer))
		for byte in buffer:
			SendByte(byte)	

def openPreview():
	#camera.start_preview()
	#camera.start_preview(alpha=100)
	#camera.start_preview(fullscreen=False,window=(50,100,300,220))
    camera.resolution = (400, 240)
    camera.start_preview(fullscreen=False, window=(0,125,400,240))

    
def stopPreview():
	camera.stop_preview()

def exitProgram():
	print("Exit Button pressed")
        #GPIO.cleanup()
	win.quit()
	
def captureImage():
	global img
	camera.capture('/home/pi/Desktop/image.jpg')
	img = ImageTk.PhotoImage(Image.open("/home/pi/Desktop/image.jpg"))  
	canvas.create_image(0, 59, anchor=NW, image=img) 
	
def loadImage1():	
	global img
	img = Image.open('image1.jpg')
	img = img.resize((400, 240))
	img.save('resized_image.jpg')
	imgDir = "/home/pi/Desktop/resized_image.jpg"
	img = ImageTk.PhotoImage(Image.open(imgDir))  
	canvas.create_image(0, 59, anchor=NW, image=img) 

def loadImage2():	
	global img
	img = Image.open('image2.jpg')
	img = img.resize((400, 240))
	img.save('resized_image.jpg')
	imgDir = "/home/pi/Desktop/resized_image.jpg"
	img = ImageTk.PhotoImage(Image.open(imgDir))  
	canvas.create_image(0, 59, anchor=NW, image=img) 

def loadImage3():	
	global img
	img = Image.open('image3.jpg')
	img = img.resize((400, 240))
	img.save('resized_image.jpg')
	imgDir = "/home/pi/Desktop/resized_image.jpg"
	img = ImageTk.PhotoImage(Image.open(imgDir))  
	canvas.create_image(0, 59, anchor=NW, image=img) 

def browseFile():
	camera.stop_preview()
	global img
	fname = tkFileDialog.askopenfilename(filetypes = (("Image files", "*.jpg"), ("All files", "*")))
	#print fname
	img = Image.open(fname)
	img = img.resize((400, 240))
	img.save('resized_image.jpg')
	imgDir = "/home/pi/Desktop/resized_image.jpg"
	img = ImageTk.PhotoImage(Image.open(imgDir))  
	canvas.create_image(0, 59, anchor=NW, image=img) 
"""
def rotateImage():
	global img
	img = Image.open("/home/pi/Desktop/resized_image.jpg")
	img = ImageTk.PhotoImage(img.rotate(90))
	img.save('resized_image.jpg')
	img = ImageTk.PhotoImage(Image.open("/home/pi/Desktop/resized_image.jpg"))  
	canvas.create_image(0, 59, anchor=NW, image=img)
"""	
	
	 
frame1 = Frame(win, width = 400, height = 480)#, background = 'red') 
frame1.pack(side = BOTTOM)
frame1.pack(expand=True, fill=BOTH)

frame2 = Frame(win, width = 400, height = 480)#, background = 'blue') 
frame2.pack(side = BOTTOM)
frame2.pack(expand=True, fill=BOTH)

frame3 = Frame(win, width = 400, height = 480)#, background = 'green') 
frame3.pack(side = LEFT)
frame3.pack(expand=True, fill=BOTH)

frame4 = Frame(win, width = 400, height = 480)#, background = 'yellow') 
frame4.pack(side = LEFT)
frame4.pack(expand=True, fill=BOTH)

#frame5 = Frame(win, width = 200, height = 220, background = 'violet') 
#frame5.pack(side = RIGHT)
#frame5.pack(expand=True, fill=BOTH)
#frame5.pack(expand=True)
#frame5.grid(row = 0, column = 0)

openPreviewButton = Button(frame1, text = "OPEN PREVIEW", font = font1, command = openPreview, height = 1, width =14 )
openPreviewButton.pack(side = LEFT)

closePreviewButton = Button(frame1, text = "CLOSE PREVIEW", font = font1, command = stopPreview, height = 1, width =15 )
closePreviewButton.pack(side = LEFT)

captureButton = Button(frame1, text = "CAPTURE", font = font1, command = captureImage, height =1 , width = 9) 
captureButton.pack(side = LEFT)

sendButton = Button(frame1, text = "SEND", font = font1, command = sendFile, height = 1 , width = 5, bg = 'green', fg = 'white') 
sendButton.pack(side = LEFT)

exitButton = Button(frame1, text = "EXIT", font = font1, command = exitProgram, height = 1 , width = 5, bg = 'red', fg = 'white') 
exitButton.pack(side = LEFT)	

browseImageButton = Button(frame2, text = "BROWSE IMAGE", font = font1, command = browseFile, height = 1 , width = 14, bg = 'orange', fg = 'white') 
browseImageButton.pack(side = LEFT)	

loadImg1Button = Button(frame2, text = "LOAD IMG 1", font = font1, command = loadImage1, height = 1 , width = 12) 
loadImg1Button.pack(side = LEFT)	

loadImg2Button = Button(frame2, text = "LOAD IMG 2", font = font1, command = loadImage2, height = 1 , width = 12) 
loadImg2Button.pack(side = LEFT)	

loadImg3Button = Button(frame2, text = "LOAD IMG 3", font = font1, command = loadImage3, height = 1 , width = 12) 
loadImg3Button.pack(side = LEFT)	

canvas = Canvas(frame4, width = 800, height = 480)   
canvas.pack(side = LEFT)

"""   
finally:
    ser.close()
    pass
"""
win.mainloop()

Return to “Advanced users”