Python Forum
How to convert 4 bytes to an integer ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to convert 4 bytes to an integer ?
#10
(Jan-17-2022, 03:44 AM)bowlofred Wrote: Can you show the code on the sending and receiving side that transfers the data?

This is on slave side (just adding snippet related to SPI, because its a very long code):
//data transfer variables
uint32_t data_adc;
uint8_t b1,b2,b3,b4;

// Configure MOSI pin as an input
  GPIO_PinModeSet(US0MOSI_PORT, US0MOSI_PIN, gpioModeInput, 0);

  // Configure MISO pin as an output
  GPIO_PinModeSet(US0MISO_PORT, US0MISO_PIN, gpioModePushPull, 0);

  // Configure CLK pin as an input
  GPIO_PinModeSet(US0CLK_PORT, US0CLK_PIN, gpioModeInput, 0);

  // Configure CS pin as an input pulled high
  GPIO_PinModeSet(US0CS_PORT, US0CS_PIN, gpioModeInput, 1);

void initUSART0(void){
//not adding code snippet but these are the setting:
1. set as slave
2. speed: 4000000
3. MSB transferred first
4. 8 data bits
5. clock mode 0
}
while(1){
.
.
USART_SpiTransfer(USART0, 'S'); /* this function performs one 8-bit frame SPI transfer. 
This function will stall if the transmit buffer is full. When a transmit buffer becomes available, data is written and the function will wait until data is fully transmitted. The SPI return value is then read out and
 returned. */

      for(uint32_t i = 0; i < 98; i++){
          data_adc = *((uint32_t *)StartPage+i); //data read from flash memory

          b1 = (unsigned int)(data_adc & 0xff);
          b2 = (unsigned int)(data_adc >> 8) & 0xff;
          b3 = (unsigned int)(data_adc >> 16) & 0xff;
          b4 = (unsigned int)(data_adc >> 24);

          int testarray[4]={b4,b3,b2,b1};

          for (int i = 0; i < 4; i++){
                        USART_SpiTransfer(USART0, testarray[i]); //no new line character??
                    }
      }
      USART_SpiTransfer(USART0, 'D'); //'D' will be sent as an integer!! To tell RP, all data has been transferred.
.
.
}
Below is code on master side (Raspberry Pi):
# spi.py
import time
import spidev
import array as arr
import struct

# We only have SPI bus 0 available to us on the Pi
bus = 0

#Device is the chip select pin. Set to 0 or 1, depending on the connections
device = 0

# Enable SPI
spi = spidev.SpiDev()

# Open a connection to a specific bus and device (chip select pin)
spi.open(bus, device)

# Set SPI speed and mode
spi.max_speed_hz = 4000000
spi.mode = 0

print("Enter '1' to start the process")
a=input()
if a:
    print("SM1 Process started...")
    spi.xfer2([0x01])
    
    while True:
        #spi.xfer2([0,0,0,0])
        b=spi.readbytes(4)
        value=struct.unpack("<I", bytearray(b))[0]
        print (value)
     
else: 
    print("Wrong input.")
One thing, I didn't add in the slave code is that: when the slave is powered on, master sends a command, '1' to slave and slave checks if it receives '1'. If it receives '1' then this whole above process is started. So that much communication is happening correctly.
Reply


Messages In This Thread
How to convert 4 bytes to an integer ? - by GiggsB - Jan-16-2022, 11:53 PM
RE: How to convert 4 bytes to an integer ? - by GiggsB - Jan-17-2022, 06:30 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Help with implementation of the logic to convert roman numeral to integer jagasrik 2 2,410 Aug-14-2020, 07:31 PM
Last Post: deanhystad
  Pack integer values as single bytes in a struct bhdschmidt 3 2,504 Jun-09-2020, 09:23 PM
Last Post: bhdschmidt
  convert from csv row to 16 bit bytes adetheheat 1 1,723 Feb-28-2020, 03:04 PM
Last Post: Larz60+
  8-bit integer to bytes Garitron 1 3,138 Sep-14-2019, 11:22 PM
Last Post: ichabod801
  replace bytes with other byte or bytes BigOldArt 1 10,857 Feb-02-2019, 11:00 PM
Last Post: snippsat
Question [Help] Convert integer to Roman numerals? {Screenshot attached} vanicci 10 9,477 Aug-06-2018, 05:19 PM
Last Post: vanicci
  Convert integer to string Maigol 5 5,710 Mar-05-2018, 10:25 PM
Last Post: python_master89
  cannot convert float infinity to integer error despite rounding off floats Afterdarkreader 3 15,405 Dec-15-2017, 02:01 AM
Last Post: micseydel
  How to convert character to integer in python rajeev1729 2 4,000 Oct-03-2017, 04:30 PM
Last Post: nilamo
  Logic to convert an integer to YEAR / MONTH hugobaur 9 8,187 Oct-18-2016, 11:58 AM
Last Post: hugobaur

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020