CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

receive data from rs232

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
karimpain



Joined: 15 Feb 2012
Posts: 39
Location: italia

View user's profile Send private message AIM Address MSN Messenger ICQ Number

receive data from rs232
PostPosted: Wed Mar 07, 2012 11:14 am     Reply with quote

hello to all!
I wrote this code thanks to some topics here in this forum.
Code:

#include <16F877a.H>
#Fuses HS,NOPROTECT,NOWDT,NOLVP
#use delay(clock = 20000000)
#use rs232(baud=2400, xmit=PIN_c6, rcv=PIN_c7, BITS =8, PARITY=n, stop=1)
#include <string.h>

char string[30];
char pass1[30];
char pass2[30];
char pass3[30];
char pass4[30];
char pass5[30];
char pass6[30];
char pass7[30];
char pass8[30];
char pass9[30];

 void main()
   {    set_tris_b(0x00);
   set_tris_d(0x00);
   output_b(0x00);
   output_d(0x04);
   delay_ms(10);

       strcpy(pass1,"mike1");
    strcpy(pass2,"mike2");
    strcpy(pass3,"mike3");
    strcpy(pass4,"mike4");
    strcpy(pass5,"mike5");
    strcpy(pass6,"mike6");
    strcpy(pass7,"mike7");
    strcpy(pass8,"mike8");
    strcpy(pass9,"mike9");

      while(true)
         {
            printf("Password: ");
         string=get_string();
         
            if(string(1)))
               {
                  output_b(0x10);
               } 
               
            if(!strcmp(string, pass2))
               {
                  output_b(0x20);
               }
               if(!strcmp(string, pass3))
               {
                  output_b(0x30);
               }
               if(!strcmp(string, pass4))
               {
                  output_b(0x40);
               }
               if(!strcmp(string, pass5))
               {
                  output_b(0x50);
               }
               if(!strcmp(string, pass6))
               {
                  output_b(0x60);
               }
               if(!strcmp(string, pass7))
               {
                  output_b(0x70);
               }
               if(!strcmp(string, pass8))
               {
                  output_b(0x80);
               }
               if(!strcmp(string, pass9))
               {
                  output_b(0x90);
               }
         }
   }

I want to send a string from my pc to the pic... if for example the data is "mike1".. put high the pin b4...but i interfaced the pic with the serial port using matlab and isis proteus.. when i used the virtual terminal to send data i have to press enter to activate this pin .but using matlab doesn't work.
Can anyone help me how i resolve this problem.!??!?

+++++++++++++++++++++
5-6 chat posts removed.

Reason: It's a support forum not a chat forum.

Stay on Topic !

- Forum Moderator
+++++++++++++++++++++
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Wed Mar 07, 2012 2:32 pm     Reply with quote

1- add "ERRORS to your #USE RS232 directive

2-consider instead a single 2 dim array
CHAR PASS [30][8]

3-get REAL!!

proteus is a buggy buggy thing - i am only able to answer questions about actual HARDWARE running on a PHYSICAL PIC -simulations mean nothing to this fellow - and you should NOT trust Proteus!!!
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

Forget Proteus
PostPosted: Wed Mar 07, 2012 2:37 pm     Reply with quote

Nobody here is going to debug Proteus or other simulator problems for you.

They're so full of bugs as to be worth a candle.

Work with real hardware, follow the CCS forum guide and we WILL try to help.

Otherwise your wasting your time and ours.

Mike
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Mar 07, 2012 3:29 pm     Reply with quote

I'm not a fan of Proteus either but what I consider worse is that the program as posted doesn't compile!

The function get_string can't be found, most likely he is using the one from <input.c>, but that I'm not sure of because the parameters don't match.

Code:
string=get_string();
This code construction suggests that get_string returns a pointer to a string. If true, then it makes no sense to declare string as an array of 30 characters. The pointer to the allocated 30 characters will be overwritten as soon as this line is executed.

Code:
if(string(1)))
This is invalid C code as well.

With bugs like this I'm sure we are not seeing the same code as being tested with. A waste of our time.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Wed Mar 07, 2012 3:34 pm     Reply with quote

with or without proteus - i think you need to go review the RULES for how
get_string() works and WHERE it works ;-))

you see i can't make it compile at ALL under CCS.

were you thinking of the function in INPUT.C perhaps ???


take a close look and see if you don't see a problem ( or 2)
with what you coded .

and be sure it actually COMPILES before complaining - ok ??
karimpain



Joined: 15 Feb 2012
Posts: 39
Location: italia

View user's profile Send private message AIM Address MSN Messenger ICQ Number

PostPosted: Wed Mar 07, 2012 4:36 pm     Reply with quote

Thanks guys. I know that Proteus is lot of bugs, but I talked to it just to make a clear vision of my problem. And I've done mistakes in the code because I posted the wrong one. Because instead of get_string() there is gets(string) // Here I don't need <input.c>
and the first if // if(!strcmp(string, pass1))
...etc.
I've done the circuit hardware and do well when I send one character, everything goes perfectly thanks to your help in an old post. Now I am facing this problem to send more than one character.
Thanks for reading and sorry for errors.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Wed Mar 07, 2012 4:46 pm     Reply with quote

post the code you have to that compiles
to get more help
karimpain



Joined: 15 Feb 2012
Posts: 39
Location: italia

View user's profile Send private message AIM Address MSN Messenger ICQ Number

PostPosted: Wed Mar 07, 2012 4:52 pm     Reply with quote

Code:
#include <16F877a.H>
#Fuses HS,NOPROTECT,NOWDT,NOLVP
#use delay(clock = 20000000)
#use rs232(baud=2400, xmit=PIN_c6, rcv=PIN_c7, BITS =8, PARITY=n, stop=1,errors)
#include <string.h>
#include <input.c>

char string[30];
char pass1[30];
char pass2[30];
char pass3[30];
char pass4[30];
char pass5[30];
char pass6[30];
char pass7[30];
char pass8[30];
char pass9[30];

 void main()
  {
   set_tris_b(0x00);
   set_tris_d(0x00);
   output_b(0x00);
   output_d(0x04);
   delay_ms(10);

    strcpy(pass1,"mike1");
    strcpy(pass2,"mike2");
    strcpy(pass3,"mike3");
    strcpy(pass4,"mike4");
    strcpy(pass5,"mike5");
    strcpy(pass6,"mike6");
    strcpy(pass7,"mike7");
    strcpy(pass8,"mike8");
    strcpy(pass9,"mike9");

      while(true)
         {
            printf("Password: ");
         gets(string) ;     
            if(!strcmp(string, pass1))
               {
                  output_b(0x10);
               } 
               
            if(!strcmp(string, pass2))
               {
                  output_b(0x20);
               }
               if(!strcmp(string, pass3))
               {
                  output_b(0x30);
               }
               if(!strcmp(string, pass4))
               {
                  output_b(0x40);
               }
               if(!strcmp(string, pass5))
               {
                  output_b(0x50);
               }
               if(!strcmp(string, pass6))
               {
                  output_b(0x60);
               }
               if(!strcmp(string, pass7))
               {
                  output_b(0x70);
               }
               if(!strcmp(string, pass8))
               {
                  output_b(0x80);
               }
               if(!strcmp(string, pass9))
               {
                  output_b(0x90);
               }
         }
   }
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Wed Mar 07, 2012 5:40 pm     Reply with quote

I'll take one item at a time:

First off- if there is no ascii 13 CR received,
the gets() call will hang the program.

Better to BUILD the string one character at a time using
KBHIT and single GETC() calls to add to your string, ignoring Linefeed ascii10 etc till the CR is found. Then instead of the CR - add and ascii Z , char 0 -to properly terminate your string and off you go to comparison land.

This takes more code but will NEVER hang waiting for a character or from bad baud rate etc. And allows you to do other things in your main "while" loop OTHER than stand on the UART - while hoping for that completed entry.
karimpain



Joined: 15 Feb 2012
Posts: 39
Location: italia

View user's profile Send private message AIM Address MSN Messenger ICQ Number

PostPosted: Fri Mar 09, 2012 11:49 am     Reply with quote

Thanks, i built string one character at time using getc() inside a for loop. and everything works . sending data from serial port to tws434 then rws434 to my pic. Thanks this is the working code:
Code:

#include <16F877a.H>
#Fuses HS,NOPROTECT,NOWDT,NOLVP
#use delay(clock = 20000000)
#use rs232(baud=2400, xmit=PIN_c6, rcv=PIN_c7, BITS =8, PARITY=n, stop=1,errors)

#include <string.h>
#include <input.c>

 int i;
char string[6];
char pass1[6];
char pass2[6];
char pass3[6];
char pass4[6];
char pass5[6];
char pass6[6];
char pass7[6];
char pass8[6];
char pass9[6];

 void main()
  {
   set_tris_b(0x00);
   set_tris_d(0x00);
   output_b(0x00);
   output_d(0x04);
   delay_ms(10);

    strcpy(pass1,"mike1");
    strcpy(pass2,"mike2");
    strcpy(pass3,"mike3");
    strcpy(pass4,"mike4");
    strcpy(pass5,"mike5");
    strcpy(pass6,"mike6");
    strcpy(pass7,"mike7");
    strcpy(pass8,"mike8");
    strcpy(pass9,"mike9");

      while(true)
         { for(i=0;i<5;i++)
         {
          string[i]=getc();         
                    }           
         if(!strcmp(string, pass1))
               {
                  output_b(0x10);
               } 
 
   if(!strcmp(string, pass2))
               {
                  output_b(0x20);
               }
               if(!strcmp(string, pass3))
               {
                  output_b(0x30);
               }
               if(!strcmp(string, pass4))
               {
                  output_b(0x40);
               }
               if(!strcmp(string, pass5))
               {
                  output_b(0x50);
               }
               if(!strcmp(string, pass6))
               {
                  output_b(0x60);
               }
               if(!strcmp(string, pass7))
               {
                  output_b(0x70);
               }
               if(!strcmp(string, pass8))
               {
                  output_b(0x80);
               }
               if(!strcmp(string, pass9))
               {
                  output_b(0x90);
               }
 
  }
   }
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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 vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group