View unanswered posts | View active topics It is currently Mon Jul 28, 2014 2:29 pm






Reply to topic  [ 8 posts ] 
File IO Reading Text Files 
Author Message
Rookie

Joined: Tue Jul 03, 2012 5:16 am
Posts: 43
Post 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]


Tue Jul 03, 2012 5:24 am
Profile
Expert

Joined: Tue Feb 28, 2012 3:10 pm
Posts: 195
Post 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.

_________________
Mike aka Spiked3
http://www.spiked3.com


Tue Jul 03, 2012 12:39 pm
Profile
Rookie

Joined: Tue Jul 03, 2012 5:16 am
Posts: 43
Post Re: File IO Reading Text Files
I originally did one second.


Tue Jul 03, 2012 11:19 pm
Profile
Rookie

Joined: Tue Jul 03, 2012 5:16 am
Posts: 43
Post Re: File IO Reading Text Files
Sorry. My fault. I did do one second but did not pay attention to the debug screen.


Tue Jul 03, 2012 11:23 pm
Profile
Expert

Joined: Tue Feb 28, 2012 3:10 pm
Posts: 195
Post 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

_________________
Mike aka Spiked3
http://www.spiked3.com


Wed Jul 04, 2012 2:52 am
Profile
Rookie

Joined: Tue Jul 03, 2012 5:16 am
Posts: 43
Post Re: File IO Reading Text Files
What do you mean by filename (picture.txt?).

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


Wed Jul 04, 2012 3:07 am
Profile
Rookie

Joined: Tue Jul 03, 2012 5:16 am
Posts: 43
Post 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();
}


Wed Jul 04, 2012 3:09 am
Profile
Expert

Joined: Tue Feb 28, 2012 3:10 pm
Posts: 195
Post 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.

_________________
Mike aka Spiked3
http://www.spiked3.com


Wed Jul 04, 2012 4:20 am
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 8 posts ] 

Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.