/**
 * Java file created by JControl/IDE
 *
 * @author RSt
 * @date 04.11.04 17:30
 *
 */
import java.io.IOException;
import jcontrol.comm.RS232;
import jcontrol.io.Flash;

public class FlashBufferedWriteRead {

	static Flash myFlash;
	static RS232 myRS232;

    public FlashBufferedWriteRead() {
    	try{
    		//Initiate Flash object with context to bank 0:
			myFlash = new Flash(0);
			//Initiate RS232 object for printing messages:
			myRS232 = new RS232();//default = 19200 baud
		}catch(IOException e){
		}
    }

    public static void main(String[] args) {
        new FlashBufferedWriteRead();
        String firstWrite = "012345678901234567890123456789";//30 characters
        byte[] firstData = firstWrite.getBytes();
        String writeThis = "Welcome to JControl!!!";//String to write
        //Assumed usage of standard JControl/SmartDisplay or -/Sticker!!!
        byte[] buffer = new byte[128];//Buffer for existing sector content
        byte[] data = writeThis.getBytes();//Bytes representing the String
        byte[] read = new byte[40];//For controlling written data
        int targetPos = 5;//Position of the first byte of 'data' in memory
        try{
        	//write firstWrite into sector 0:
        	myFlash.write(firstData, 0, firstData.length, 0);
        	//read complete sector 0:
        	myFlash.read(buffer, 0, buffer.length, 0);
        	//integrate data into buffer:
        	for(int i=0 ; i < data.length ; i++){
	        	buffer[i+targetPos] = data[i];
	        }
	        //write bytes into memory (complete sector):
	        myFlash.write(buffer, 0, buffer.length, 0);
	        //read Flash content:
	        myFlash.read(read, 0, read.length, 0);
	        //write read data to serial port:
	        myRS232.println(new String(read));
        }catch(IOException e){
		}
		for(;;);//Stops execution of this program.
    }
}