I have been struggling for the past few days to solve this problem but it end up fails.
I connect serial SRAM to my Arduino and read the SRAM's start-up value which is for example (010101011001....) to make a Physical Unclonable Function.
So, I am trying to use Python to automate my Arduino to read the start-up value of the SRAM and record it into Excel. The Python will send byte H to the serial monitor and once it has been detected, the Arduino will read the start-up value of SRAM.
The problem I face is that Python returns an empty string of start-up value from the serial monitor. The output should be as mentioned above, a stream of random 0 and 1.
When I send H command in the serial monitor, it works fine, but when I try using Python to run it, the Arduino's LED did blink but Python returned an empty string instead of returning stream of 0 and 1.
Any help would be much appreciated.
Arduino code:
#include <SPI.h>
/************SRAM opcodes: commands to the 23LC1024 memory chip ******************/
#define RDMR 5 // Read the Mode Register
#define WRMR 1 // Write to the Mode Register
#define READ 3 // Read command
#define WRITE 2 // Write command
#define RSTIO 0xFF // Reset memory to SPI mode
#define ByteMode 0x00 // Byte mode (read/write one byte at a time)
#define Sequential 0x40 // Sequential mode (read/write blocks of memory)
#define CS 10 // Chip Select Line for Uno
#define PS 7
#define MI 12
#define SC 13
#define MO 11
#define HO 4
/******************* Create Global Variables *********************/
byte i = 0; // loop counter variable
/******* Set up code to define variables and start the SCI and SPI serial interfaces *****/
void setup(void) {
int var;
uint32_t address = 0; // create a 32 bit variable to hold the address
byte value; // create variable to hold the data value read
byte data; // create variable to hold the data value sent
pinMode(PS,OUTPUT);
pinMode(CS,OUTPUT); // Make pin 10 of the Arduino an output
pinMode(HO,OUTPUT);
Serial.begin(9600); // set communication speed for the serial monitor
SPI.begin(); // start communicating with the memory chip
digitalWrite(PS, HIGH);
digitalWrite(HO, HIGH);
}
void loop() { // nothing to do in the loop
/********* Read a single Byte Each Time to Memory *********************/
int var;
uint32_t address = 0; // create a 32 bit variable to hold the address
byte value; // create variable to hold the data value read
byte data;
if (Serial.available() > 0) {
var = Serial.read();
if (var == 'H') {
SetMode(ByteMode); // set to send/receive single byte of data
for(i = 0; i <=63; i++) { // start at memory location 0 and end at 64
address = i; // use the loop counter as the memory address
value = ReadByte(address); // reads a byte of data at that memory location
for (int j = 0; j < 8; j++) {
bool b = value & 0x80;
Serial.print(b);
value = value << 1;
}
}
//Serial.println();
}
}
}
/* Functions to Set the Mode, and Read and Write Data to the Memory Chip ***********/
/* Set up the memory chip to either single byte or sequence of bytes mode **********/
void SetMode(char Mode) { // Select for single or multiple byte transfer
digitalWrite(CS, LOW); // set SPI slave select LOW;
SPI.transfer(WRMR); // command to write to mode register
SPI.transfer(Mode); // set for sequential mode
digitalWrite(CS, HIGH); // release chip select to finish command
}
/************ Byte transfer functions ***************************/
byte ReadByte(uint32_t address) {
char read_byte;
digitalWrite(CS, LOW); // set SPI slave select LOW;
SPI.transfer(READ); // send READ command to memory chip
SPI.transfer((byte)(address >> 16)); // send high byte of address
SPI.transfer((byte)(address >> 8)); // send middle byte of address
SPI.transfer((byte)address); // send low byte of address
read_byte = SPI.transfer(0x00); // read the byte at that address
digitalWrite(CS, HIGH); // set SPI slave select HIGH;
return read_byte; // send data back to the calling function
}
Python code:
import serial
import time
import openpyxl, math
from openpyxl import Workbook, load_workbook
data_file='SRAM_TEST.xlsx'
wb = load_workbook(filename= data_file)
sheet_data = wb['Sheet1']
for x in range(1,3):
with serial.Serial('COM4', 9600, timeout=1) as ser:
time.sleep(0.5)
ser.write(b'H')
data=ser.read(size=1)
data1=data.decode()
sheet_data.cell(row=x, column=1).value=data1
print(data1)
Solution: change the with serial.Serial('COM4', 9600, timeout=1) as ser: to this solve the problem.
if (Serial.available() > 0){
var = Serial.read();
if (var == 'H'){
question from:
https://stackoverflow.com/questions/65559820/python-arduino-returns-empty-string-pyserial