ROBOTC.net forums
http://www.robotc.net/forums/

File IO Reading Text Files
http://www.robotc.net/forums/viewtopic.php?f=1&t=4661
Page 1 of 1

Author:  brianchen11 [ Tue Jul 03, 2012 5:24 am ]
Post subject:  File IO Reading Text Files

I'm currently trying to write a program that reads a text file.
However, every time I read from the file, it reads the last character.

The main project is to create a printer similar to PriNXT from World of Mindstorms but the printer is designed differently, more similar to the one LaurensValk has. Could someone please help?

[code] int x = 0, y = 0;
char incommingChar;

byte CR = 0x13;
byte LF = 0x10;
byte S = 0x48;

TFileHandle hFileHandle;
TFileIOResult nIOResult;
string sFileName = "picture.txt";
int nFileSize = 100000;

OpenRead(hFileHandle, nIOResult, sFileName, nFileSize);


int Width = 160, Height = 114;

for(x=0; x<Width; x++){
for(y=0; y<Height; y++){
ReadByte(hFileHandle, nIOResult, incommingChar);
nxtDisplayClearTextLine(1);
nxtDisplayTextLine(1,incommingChar);
if(incommingChar == S){

}
wait1Msec(1);
}
}[/code]

Author:  Spiked3 [ Tue Jul 03, 2012 12:39 pm ]
Post subject:  Re: File IO Reading Text Files

Are you sure it is only displaying the last character? or is it erasing the display line and displaying the next character every time, until it gets to the end?

wait1Msec(1); does not give you enough time to read it. it probably does not give it enough time to even update the lcd.

Author:  brianchen11 [ Tue Jul 03, 2012 11:19 pm ]
Post subject:  Re: File IO Reading Text Files

I originally did one second.

Author:  brianchen11 [ Tue Jul 03, 2012 11:23 pm ]
Post subject:  Re: File IO Reading Text Files

Sorry. My fault. I did do one second but did not pay attention to the debug screen.

Author:  Spiked3 [ Wed Jul 04, 2012 2:52 am ]
Post subject:  Re: File IO Reading Text Files

I just noticed your filename, I do not think that is a valid filename. try using a file called picture.dat

Author:  brianchen11 [ Wed Jul 04, 2012 3:07 am ]
Post subject:  Re: File IO Reading Text Files

What do you mean by filename (picture.txt?).

It works perfectly fine. There are no more issues now.

Author:  brianchen11 [ Wed Jul 04, 2012 3:09 am ]
Post subject:  Re: File IO Reading Text Files

Here's the current code.

Code:
#pragma config(Sensor, S2,     ,               sensorTouch)
#pragma config(Sensor, S3,     ,               sensorTouch)
#pragma config(Sensor, S4,     colour,         sensorCOLORFULL)
#pragma config(Motor,  motorA,          motorFeeder,   tmotorNormal, PIDControl, encoder)
#pragma config(Motor,  motorB,          motorPen,      tmotorNormal, PIDControl, encoder)
#pragma config(Motor,  motorC,          motorMove,     tmotorNormal, PIDControl, encoder)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

const int printSpeed=40, calibrationSpeed=60, buttonError=50, pushdown = 80;
int x = 0, y = 0;
float fullWidth, fullLength;

void movepen(){
  while(nMotorEncoder(motorB)>-pushdown){
    motor(motorB)=-printSpeed;
  }
  nMotorEncoder(motorB)=0;
  motor(motorB)=0;
  while(nMotorEncoder(motorB)<pushdown){
    motor(motorB)=printSpeed;
  }
  nMotorEncoder(motorB)=0;
  motor(motorB)=0;
}
void initialization(){
  nVolume=3;
  nMotorEncoder(motorA)=0;
  nMotorEncoder(motorB)=0;
  nMotorEncoder(motorC)=0;
}

void entrance(){
  nxtDisplayTextLine(1,"Press Enter");
  nxtDisplayTextLine(2,"To Begin");

  while(nNxtButtonPressed==-1){
  }
}

void calibration(){
  nxtDisplayTextLine(1,"Wait for Beep");
  while(SensorValue(S3)==0){
    motor(motorC)=calibrationSpeed;
  }
  wait1Msec(500);
  nMotorEncoder(motorC)=0;
  while(SensorValue(S2)==0){
    motor(motorC)=-calibrationSpeed;
  }
  motor(motorC)=0;
  wait1Msec(500);
  fullWidth=nMotorEncoder(motorC)*-1-buttonError;
  nMotorEncoder(motorC)=0;
  while(nMotorEncoder(motorC)<fullWidth/2){
    motor(motorC)=calibrationSpeed;
  }
  motor(motorC)=0;
  nMotorEncoder(motorC)=0;
  PlayTone(800,200);
}

void pullforpaper(){
  while(SensorValue(S4)!=6){
  }
  while(nMotorEncoder(motorA)<1000){
    motor(motorA)=calibrationSpeed/2;
  }
  while(SensorValue(S4)==6){
    motor(motorA)=calibrationSpeed/2;
  }
  fullLength=nMotorEncoder(motorA)*6.4/10;
  nMotorEncoder(motorA)=0;
  while(nMotorEncoder(motorA)>-fullLength){
    motor(motorA)=-calibrationSpeed/2;
  }
  nMotorEncoder(motorA)=0;
  while(nMotorEncoder(motorC)>-fullWidth/2){
    motor(motorC)=-calibrationSpeed;
  }
  motor(motorA)=0;
  motor(motorB)=0;
  motor(motorC)=0;
  nMotorEncoder(motorA)=0;
  nMotorEncoder(motorC)=0;
  wait1Msec(1000);
}

void draw(){
  char incommingChar;

  byte CR = 13;
  byte LF = 10;
  byte S = 48;

  TFileHandle   hFileHandle;
  TFileIOResult nIOResult;
  string        FileName = "picture.txt";
  int           FileSize = 10000000;

  OpenRead(hFileHandle, nIOResult, FileName, FileSize);

  int Width = 160, Height = 114;

   for(x=0; x<Width; x++){
     for(y=0; y<Height; y++){      
       ReadByte(hFileHandle, nIOResult, incommingChar);
       nxtDisplayClearTextLine(1);
       nxtDisplayTextLine(1,incommingChar);
       if(incommingChar==S || incommingChar==S+1){
          if(incommingChar == S){
            motor(motorA)=0;
            motor(motorB)=0;
            motor(motorC)=0;
            movepen();                 
          }           
          while(nMotorEncoder(motorC)<(fullWidth/Width)){
            motor(motorC)=printSpeed;
          }      
          nMotorEncoder(motorC)=0;
          wait1Msec(500);
        }
        else{
          y=y-1;
          motor(motorA)=0;
          motor(motorB)=0;
          motor(motorC)=0;
        }
     }
     while(nMotorEncoder(motorA)<fullLength/Height){
       motor(motorA)=printSpeed;      
     }
     nMotorEncoder(motorA)=0;
     while(nMotorEncoder(motorC)>-fullWidth){
       motor(motorC)=-printSpeed*1.5;
     }
     motor(motorA)=0;
     motor(motorC)=0;
     nMotorEncoder(motorC)=0;
   }
}



task main(){
  initialization();
  entrance();
  calibration();
  pullforpaper();
  drawdraw();
}

Author:  Spiked3 [ Wed Jul 04, 2012 4:20 am ]
Post subject:  Re: File IO Reading Text Files

I did not quite understand your reply, and I was thinking I ran into a problem with .txt one time, but it was probably something else.

So anyhow, glad it works.

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/