steve041264
Posts: 3
Joined: Wed Jan 03, 2018 8:34 am

Send 64 bit from one Raspberry PI 3 B to another

Fri Mar 16, 2018 9:50 am

Good evening,
I'm trying send 64 bit from one raspberry to another, using gpio. My problem is synchronize to raspberry, so I can receive correct data.
TX program is:

Code: Select all

import RPi.GPIO as GP
import numpy as np
import bitstring as bs

GP.setmode(GP.BCM)
GP.setup(17,GP.OUT)

SIGIFS=np.array([0.0000,0.79308,0.05000,3.4908,0.10000,7.6360,.......900,25.976,86.950,25.933,87.000,25.889,87.050,25.848,87.100,25.813,87.150]);
SIGIF=SIGIFS[1::2]#take only odds values from SIGIFS
lung_SIGIF=len(SIGIF)#length of SIGIF
lung_SIGIFS=len(SIGIFS)#length of SIGIFS
length_stop=bs.Bits(int=9999,length=64);#convert 9999 to binary with length 64 bit to stop TX
length_start=bs.Bits(int=1000, length=64);#convert 1000 to binary with length 64 bit to stop RX
Y=np.zeros(64);#I create an array of 64 positions to insert the bits that make up the val value in binary of the i-th value of SIGIF
arrive_SIGIF=np.zeros(lung_SIGIF);#I create an array of the length necessary to contain the starting array
start=np.zeros(64);#I create an array of the length necessary to contain the start int 1000


#send array to start rx
for i in range(0,64):
    GP.output(17,length_start[i])

for i in range(0,lung_SIGIF):
    val=SIGIF[i];#take 'i' value of SIGIF
    binary=bs.Bits(float=val,length=64);#convert val to bin  
    for x in range(0,64):
        GP.output(17,int(binary.bin[x]));#write bit on port number 17

 
#send array to stop tx
for i in range(0,64):
    GP.output(17,length_stop[i])

GP.cleanup();

and the RX code is:

Code: Select all

import RPi.GPIO as GP
import numpy as np
import bitstring as bs

GP.setmode(GP.BCM)
GP.setup(27,GP.IN)

SIGIFS=np.array([0.0000,0.79308,0.05000,3.4908,0.10000,7.6360,.......900,25.976,86.950,25.933,87.000,25.889,87.050,25.848,87.100,25.813,87.150]);
SIGIF=SIGIFS[1::2]#take only odds values from SIGIFS
lung_SIGIF=len(SIGIF)#length of SIGIF
lung_SIGIFS=len(SIGIFS)#length of SIGIFS
length_stop=bs.Bits(int=9999,length=64);#convert 9999 to binary with length 64 bit to stop TX
length_start=bs.Bits(int=1000, length=64);#convert 1000 to binary with length 64 bit to stop RX
Y=np.zeros(64);#I create an array of 64 positions to insert the bits that make up the val value in binary of the i-th value of SIGIF
arrive_SIGIF=np.zeros(lung_SIGIF);#I create an array of the length necessary to contain the starting array
start=np.zeros(64);#I create an array of the length necessary to contain the start int 1000

#read array to start rx
while 1:
    for x in range(0,64):
        start[x]=GP.input(22);
    str1 = ''.join(str(e) for e in start.astype(int))#I transform the array created in string str1
    aa=bs.Bits(bin=str1);#I recreate the starting binary number
    g=aa.int
  
    if str1==g:
        break;    

while 1:
    for i in range(0,lung_SIGIF):
        for x in range(0,64):
            k=GP.input(22);#I read the received bit
            Y[x]=int(k);#I insert the received bit inside the position 
        str2 = ''.join(str(e) for e in Y.astype(int))#I transform the array created in string str1
        aa=bs.Bits(bin=str2);#I recreate the starting binary number
        g2=aa.float;#I convert the binary number received in float
        arrive[i]=g2;#insert the value val sent in the same position of the starting array SIGIF
#stop to receive
        if int(g)==9999:
            break;
            
I have to recreate the same SIGIF array in the arrival Raspberry. My problem is that the second value of the array is not in the arrival array either as a position or as a value. How can I synchronize the two raspberry?

Thank you

Steve

harms
Posts: 24
Joined: Tue Feb 13, 2018 7:38 pm

Re: Send 64 bit from one Raspberry PI 3 B to another

Sun Mar 18, 2018 8:14 am

Are you stuck to use a GPIO connection? If not, by far the easiest and most simple approach would be serial communication via Bluetooth - the serial transmission already takes care of synchronisation at the bit and byte level. At the application level (8-byte messages), a combination of Unix Select and buffered or unbuffered I/O will do the rest (Select also can serve to handle event sequencing).

Documentation on Bluetooth is shockingly bad, but I just - and finally - got communication between 2 Pi's working - in fact came to this forum branch looking for a place where to document how that works for me.

PS: I have quite some experience with synchronising micro-processors - and developed a strong preference to SPI which is ideal for this kind of task (but dont know how software support is implemented on Raspberry). But if you want to got this "hardware way" (and assuming that you will also will spend time in getting familiar with the support) the effort will certainly be a multiple of that to be spent on making a bluetooth connection work

Return to “Interfacing (DSI, CSI, I2C, etc.)”

Who is online

Users browsing this forum: MoreBloodWine and 4 guests