serial programing- portb wrap around

Hi guys, i'm doing my first serial programing at uni, and i'm stuck on an error check. This is my error check i have been given;

ERROR CHECK 4: DYNAMIC PERORMANCE CHECKS: values written to portb, wrap around.

If after addition of the increment value the value calculated for portb is greater than 255,then the modulus of 256 is to be written to portb.This is referred to as 'wrapping around'.
Eg.: 256 modulus 256 is 0
513 modulus 256 is 1

If AFTER addition of the increment value the value calculated for portb is less than zero then 256 is to be added to the calculated value and this new value written to portb Example: if the increment value is -10 and the current value of portb is 8 then the result after addition the calculated value is -2. Therefore -2 plus 256 gives 254 which is the new value to write to portb.
This is also sometimes referred to as 'wrapping around', but in the other direction.

At no time should a value greater than 255 or less than zero be written to portb. NOTE: no error check or action has been specified for the case that a number greater than 255 or less than zero is written to portb, because this would require another independent process to verify the error condition and take action. In an industrial system such independent dynamic error checking would be be required, possibly using another microsystem which would act as a 'watchdog' system, or a supervisory system.

so my question is how would i go about doing this? my code is currently this.

[code]#include <iostream>
#include <cstdlib>
#include <time.h>
#include <sys/time.h>
#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>

using namespace std;


int main(int argc, char *argv[])


{
    int num1,num2;
    char* end;
    
    if(argc==1)
    {
       cout<<"S3333435B,s3333435@student.rmit.edu.au,Matthew_Merigan" << endl ;
       system("pause");
       return(0);
    }

    if(argc>3)
    {
       cout<<"P"<<endl;
       system("pause");
       return(0);
    }
    FILE *fpipe;
    float param1=atof(argv[1]), param2=atof(argv[2]);
    if(argc==2||argc==3)
    {
        
    string comma1(argv[1]);

    string::size_type i = comma1.find(',', 0);
    while (i != string::npos)
    {
    comma1.erase(i,1);
    i = comma1.find(',', i); 
    }
    

   double num1=strtod(comma1.c_str(),&end);
   if(*end!='\0')
   {
       cout<<"X"<<endl;
       system("pause");
       return (0);
   }


   double param1=(int)num1;
   if(param1!=num1)
   {
       cout<<"X"<<endl;
       system("pause");
       return (0);
   }
  
    string comma2(argv[2]);

    string::size_type j = comma2.find(',', 0);
    while (j != string::npos)
    {
    comma2.erase(j,1);
    j = comma2.find(',', j);
    }
    

   double num2=strtod(comma2.c_str(),&end);
   if(*end!='\0')
   {
       cout<<"X"<<endl;
       system("pause");
       return (0);
   }


   double param2=(int)num2;
   if(param2!=num2)
   {
       cout<<"X"<<endl;
       system("pause");
       return (0);
   } 
   
   if(param1 > 255|| param1 < -255)
   {
        cout<<"R1"<<endl;
        system("pause");
        return 0;
    }

    if (param2 > 65535 || param2 < 1)
    {
        cout << "R2" << endl;
        system("pause");
        return 0;
    } 
    
       char *command="ousb io PINC"; // Read the switch state.
          char line[256];
       if ( !(fpipe = (FILE*)popen(command,"r")) )
       { // error if fpipe returns NULL
       perror("Problems with pipe");
       exit(1);
       }
       while ( fgets( line, sizeof (line), fpipe))
       { printf("%s", line);
       }
    }
    system("pause");
pclose(fpipe);
}
[/code]


my code will eventually flash LEDs on usb board, but for now i'm just sorting out my error checks.
Last edited on
your code only deals with pipe. That's where the error occurs?

But yes you can read/write only 8 bits from/to this kind of port (AVR-Mikrocontroller?)

You might find this site interesting: http://www.usbmicro.com/
Topic archived. No new replies allowed.