Thank you for your kind reply and example code! I re-uploaded my codes as request. But the card dump is a actually a hex file instead of a txt, I had to change that to upload successfully.
Below is my python code:
import binascii
import os
import serial
ser = serial.Serial('/dev/ttyUSB0',9600, timeout=1)
recv = -1
counter = 0
totalcount = 0
with open('TestCard.txt', 'rb') as f:
hexdata = binascii.hexlify(f.read())
hexlist = map(''.join, zip(*[iter(hexdata)]*2))
f.close
while(1):
#Wait for arduino to ask for address
if(ser.readline()=="address\n"):
ser.write("0")
print "written 0 \n" #Give arduino address I wanted to write
break
for a in hexlist:
#Check if arduino is good to revcieve data
while(ser.readline()!="OK\n"):
ser.write("sendreq\n")
#Send data
if(counter != 16 ):
ser.write(a.encode())
print "written " + a.encode()
counter = counter + 1
totalcount = totalcount + 1
else:
while(ser.readline()!="Next Block\n" and ser.readline()!="Done\n"):
continue
counter = 0
ser.write("EndOfData\n")
ser.close()
and below is my sketch:
#include <Wire.h>
#include <SoftwareSerial.h>
byte buff[16];
int addr=-1;
int count=0;
int readed=-1;
byte recv;
bool ended = false;
void i2c_eeprom_write_byte( int deviceaddress, unsigned int eeaddress, byte data ) {
int rdata = data;
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.write(rdata);
Wire.endTransmission();
Serial.write("done");
}
// WARNING: address is a page address, 6-bit end will wrap around
// also, data can be maximum of about 30 bytes, because the Wire library has a buffer of 32 bytes
void i2c_eeprom_write_page( int deviceaddress, unsigned int eeaddresspage, byte* data, byte length ) {
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddresspage >> 8)); // MSB
Wire.write((int)(eeaddresspage & 0xFF)); // LSB
byte c;
for ( c = 0; c < length; c++)
Wire.write(data[c]);
Wire.endTransmission();
}
byte i2c_eeprom_read_byte( int deviceaddress, unsigned int eeaddress ) {
byte rdata = 0xFF;
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.endTransmission();
Wire.requestFrom(deviceaddress,1);
if (Wire.available()) rdata = Wire.read();
return rdata;
}
// maybe let's not read more than 30 or 32 bytes at a time!
void i2c_eeprom_read_buffer( int deviceaddress, unsigned int eeaddress, byte *buffer, int length ) {
Wire.beginTransmission(deviceaddress);
Wire.write((int)(eeaddress >> 8)); // MSB
Wire.write((int)(eeaddress & 0xFF)); // LSB
Wire.endTransmission();
Wire.requestFrom(deviceaddress,length);
int c = 0;
for ( c = 0; c < length; c++ )
if (Wire.available()) buffer[c] = Wire.read();
}
void setup() {
Wire.begin();
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if(ended == true){
delay(1000000);
}
while(addr==-1&& ended != true){
Serial.print("address\n");
addr = Serial.read();
delay(500);
}
while(1){
if(Serial.read()!="sendreq\n"){
Serial.write("OK\n");
}else if(Serial.read()!="EndOfData\n"){
ended = true;
}
delay(50);
}
while(readed == -1 && ended != true){
readed = Serial.read();
recv = readed;
delay(50);
}
readed = -1;
/*Serial.print(recv < 0x10 ? " 0" : " ");
Serial.print(recv,HEX);
Serial.println();*/
if(recv!=-1 && ended != true){
buff[count] = recv;
count++;
}
if(count == 16 && ended != true){
for(int i=0 ; i<16 ; i++){
addr += i;
//i2c_eeprom_read_byte(0x50,addr)!= buff[i] ? i2c_eeprom_write_byte(0x50,addr,buff[i]) : delay(1);
delay(15);
}
//i2c_eeprom_write_page(0x50, addr, buff, sizeof(buff));
//delay(10);
count = 0;
Serial.write("Next Block\n");
}
}