Like Tree1Likes
  • 1 Post By Salem

read in from serial port; getchar()

This is a discussion on read in from serial port; getchar() within the C Programming forums, part of the General Programming Boards category; Hey, I am going to apologise straight up and say im kinda new to c programming and this forum so ...

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    5

    read in from serial port; getchar()

    Hey,

    I am going to apologise straight up and say im kinda new to c programming and this forum so excuse me if I miss something out.

    At this stage, I am using an FPGA board to print a message to the hyperterminal using an embedded project on a microcontroller. Now what I am trying to do is read in the characters that I type in the hyperterminal and once I hit enter, they appear back on the hyperterminal screen right under where i printed below.

    My guess is a have to use the getchar() approach! im just unsure how to execute it into this programm and put it in an array and pass it back to the WriteChar function I have.

    For example; i type

    "hello" and it print
    hello under it.

    this is the code I have so far with regards to sending message to hyperterminal.

    Code:
    void WriteChar(unsigned char Character);
    void InitSerial(void);
    void WriteString(char *Str);
    void delay(void);
    
    
    void main (void) {
      char String[]="\r\nPlease Print a Character to be returned back to you...\r\n";
        InitSerial();
    
    
      unsigned char j;
      for(j=0; j < 1; j++)
        WriteString(String);
    
    
    void InitSerial(void){
    
    
    SLR0_PCON=0x80;
     SLR0_SCON=0x40;
     SLR0_SRELL=0x5d;
     SLR0_SRELH=0x03;
     SLR0_ADCON=0x80;
    }
    
    void WriteChar(unsigned char Character){
      unsigned char variable = 0;
      SLR0_SBUF = Character;
      while (variable != 0x02) {
            variable = SLR0_SCON & 0x02;
            delay();
      }
      SLR0_SCON = 0x40;
    }
    
    
    void WriteString(char *Str){        
     unsigned char i;                   
      for(i=0; Str[i]!='\0'; i++)       
      WriteChar(Str[i]);                
    }
    
    
    // Delay
    void delay(void){
        int i;
        for (i=0; i<0xFFFF; i++) __asm("nop");
    }
    I hope this makes sense and thanks again in advance, if you need more info, more than happy to provide.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    In the same way that you write a char using this

    Code:
      unsigned char variable = 0;
      SLR0_SBUF = Character;
      while (variable != 0x02) {
            variable = SLR0_SCON & 0x02;
            delay();
      }
      SLR0_SCON = 0x40;
    Do you understand what SLR0_SBUF and SLR0_SCON really are?


    You would have something similar for reading, which would be something like
    - test SLR0_SCON (read the manual) for the character received bit
    - read the input character.

    Your readstring function would call your readchar function until it received a newline.
    s.dodd likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    5
    Thanks for the reply Salem,

    Yeah i briefly understand what they do. but this probably the only function i dont understand what is really happening! thats why i posted on here if someone could help me code it together.

    But ill give it a go and see what i come up with,

    appreciate your time

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    5
    Just a quick question for you Salem, or anyone else that knows...

    I've tried modifying the code to do what I want it to do, and read in an input from hyperterminal and display it back. But I am going around in circles about it!

    This is the updated code, can someone understand what's happening and give me a solution! I'm stumped and frustrated.

    Code:
    void WriteChar(unsigned char Character);
    void InitSerial(void);
    void WriteString(char *Str);
    void delay(void);
    //void getchar(char *Str);
    void ReadString(char *Str);
    void ReadChar(unsigned char Input);
    
    
    void main (void) {
      char String[]="\r\nPrint a Character...\r\n";
        InitSerial();
    
    
      unsigned char j;
      for(j=0; j < 1; j++)
        WriteString(String); // pass the char String to that function
    
    
      unsigned char s;
      for(s=0; s < 1; s++)
      ReadString(String);
    }
    
    
    void InitSerial(void){
    
    
     SLR0_PCON  = 0x80;
     SLR0_SCON  = 0x40;
     SLR0_SRELL = 0x5d;
     SLR0_SRELH = 0x03;
     SLR0_ADCON = 0x80;
    }
    
    
    void WriteChar(unsigned char Character){
      unsigned char variable = 0;
      SLR0_SBUF = Character;
      while (variable != 0x02) {
            variable = SLR0_SCON & 0x02;
            delay();
      }
      SLR0_SCON = 0x40;
    }
    
    
    void WriteString(char *Str){        
     unsigned char i;                   
      for(i=0; Str[i]!='\0'; i++)       
      WriteChar(Str[i]);                
    }
    
    
    void ReadChar(unsigned char Input){
      unsigned char variable = 0;
      SLR0_SBUF = Input;
      while (variable != 0x01) {
            variable = SLR0_SCON & 0x01;
            delay();
      }
      SLR0_SCON = 0x40;
    }
    
    
    void ReadString(char *Str){       
     unsigned char p;                   
      for(p=0; Str[p]!='\r'; p++)       
      ReadChar(Str[p]);              
    }
    
    
    // Delay
    void delay(void){
        int i;
        for (i=0; i<0xFFFF; i++) __asm("nop");
    }
    Where are my variables wrong and where am I calling functions wrong?

    PLEASE!

    ps, I know the code is wrong and sloppy, but please forgive me!

    Thanks heaps again.

    Need any info, please ask.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    You need to get things out of readchar (return a result) - think of getchar() when reading stdin

    ReadChar is the equivalent of getchar()
    ReadString is the equivalent of fgets()
    If you make the prototypes of your functions the same, it should give you the clue to finishing.

    Keep going, I think you're close to the finish!

    One last hint
    You should have
    Input = SLR0_SBUF; // is this the transmit buff, receive buff, or just a buff?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    5
    Hey,

    here is the definition for the serial port block i am using with regards to the SBUF...

    Code:
    Serial Data Buffer Register (SBUF)
    The serial data interface consists of two separate registers, a transmit register and a receive register (with buffer), both of which are accessed through the Special Function Register SBUF. Writing to the SBUF register loads data into the serial transmit register and starts the transmission. Reading the SBUF register, accesses data in the serial receive buffer.
    The SRL0 is full duplex, meaning that it is capable of simultaneous transmission and reception of serial data. Single-byte buffering is incorporated at the reception stage, enabling a second byte of data to be received whilst the first is still being read. Bear in mind that the first byte of received data must be read out before reception of the subsequent byte has completed, otherwise the first byte of data will be lost (overwritten with the second byte).
    


    I feel like i'm really close too, but every time I change something it doesn't work, I make a new approach and just feels like i'm going round and round in circles.

    this is actually the first time with using readChar or readString! it is the same as what ive got in there for writing char and string? i cant seem to find too much info on this stuff

    Code:
    //READS IN CHARACTERS FROM HYPERTERMINAL---------------------------------------
    void ReadChar(char Input){
      unsigned char variable = 0;
      Input = SLR0_SBUF;
      while (variable != 0x01) {
            variable = SLR0_SCON & 0x01;
            delay();
      }
      SLR0_SCON = 0x40;
    }
    
    
    void ReadString(char *Str){
     //unsigned char p;
     char message[10];
     char let;
     int index = 0;
    
    
     while((let = getchar())!='\n'){
        message[index]=let;
     for(index=0; Str[index]!='\0'; index++)
      ReadChar(Str[index]);
     }
    }
    I cannot get it hey, are you able to hint me any closer or give me more clues... this is only the start of my coding. I just need to get over this hurdle!

    I really appreciate ur time

    Thanks

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Code:
    char readChar ( void ) {
      char Input;
      unsigned char variable = 0;
      Input = SLR0_SBUF;
      while (variable != 0x01) {
            variable = SLR0_SCON & 0x01;
            delay();
      }
      SLR0_SCON = 0x40;
      return Input;
    }
    Now make your readLine call your readChar
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    5
    Hey Sorry to get ya again,

    but i tried everything you were saying and no matter what i'm trying, it keeps coming up with errors. ill explain as i go. As you can see i had your method there and for some reason it didnt like the void there at all saying it couldn't return anything. Is there anything wrong with this code you can see as to why it is not passing the string 'Input' to the write char function again. Or is that going the wrong way about it.

    Code:
    //PROTOTYPES-------------------------------------------------------------------
    void WriteChar(unsigned char Character);
    void InitSerial(void);
    void WriteString(char *Str);
    void delay(void);
    void ReadLine(char *Str2);
    void ReadChar(unsigned char Input);
    //-----------------------------------------------------------------------------
    //MAIN FUNCTION----------------------------------------------------------------
    void main (void) {
      char String[]="\r\nPrint a Character...\r\n";  // Creates a string called 'String'
      //char Input []="";
      InitSerial(); // Initialises the Serial Ports
    
    
      unsigned char j;
      for(j=0; j < 1; j++)  // Only performs this operation once
        WriteString(String); // Pass 'String' to WriteChar function
    
    
        char Input []="";
        ReadLine(Input);
    
    
      unsigned char t;
      for(t=0; t < 1; t++)  // Only performs this operation once
        WriteString(Input); 
    }
    //-----------------------------------------------------------------------------
    //INITIASE SERIAL PORTS--------------------------------------------------------
    void InitSerial(void){
    
    
     SLR0_PCON  = 0x80;
     SLR0_SCON  = 0x40;
     SLR0_SRELL = 0x5d;
     SLR0_SRELH = 0x03;
     SLR0_ADCON = 0x80;
    }
    //-----------------------------------------------------------------------------
    //WRITES TO HYPERTERMINAL------------------------------------------------------
    void WriteChar(unsigned char Character){
      unsigned char variable = 0;
      SLR0_SBUF = Character;
      while (variable != 0x02) {
            variable = SLR0_SCON & 0x02; //Enable Transmit bit and send data
            delay();
      }
      SLR0_SCON = 0x40;
    }
    
    
    void WriteString(char *Str){       
     unsigned char i;                   
      for(i=0; Str[i]!='\0'; i++)       
      WriteChar(Str[i]);
    }
    //-----------------------------------------------------------------------------
    //READS IN CHARACTERS FROM HYPERTERMINAL---------------------------------------
    void ReadChar(unsigned char Input){
    //char Input;
    unsigned char variable = 0;
      SLR0_SBUF = Input;
      while (variable != 0x01) {
            variable = SLR0_SCON & 0x01; //Enable Receive bit
            delay();
      }
      SLR0_SCON = 0x40;
    }
    
    
    void ReadLine(char *Str2){
     unsigned char p;                   
      for(p=0; Str2[p]!='\r'; p++)       
      ReadChar(Str2[p]);
    }
    //-----------------------------------------------------------------------------
    //DELAY------------------------------------------------------------------------
    void delay(void){
        int i;
        for (i=0; i<0xFFFF; i++) __asm("nop");
    }
    //-----------------------------------------------------------------------------
    i am using some of the code u supplied to me above and it doesnt like the return line at all. No matter what input or variable I put there it says " 'return' with a value in function returning void.

    and with the main function, how do i call readLine function when i have nothing to put in the brackets, like i'm unsure what arguments to put in there to call it as everything i put is not working. if i put the variable name Input, it doesnt like it.

    Thanks again.

  9. #9
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Dunno then - your code still looks the same as before.
    It certainly doesn't have anything like a modified readChar()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  10. #10
    Registered User
    Join Date
    Oct 2011
    Posts
    1
    Just at a glance, it appears your SLR0_SCON setting on the readchar should be different. 0x40 sets the 7th bit which triggers the transmit interrupt, great for transmitting things, however if you're receiving, you want to bring the 8th bit high (0x80) which triggers the receive interrupt.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can't Read From Serial Port
    By HalNineThousand in forum Linux Programming
    Replies: 14
    Last Post: 03-20-2008, 05:56 PM
  2. serial port read
    By mackrackit in forum C++ Programming
    Replies: 6
    Last Post: 03-22-2006, 12:40 AM
  3. How to read from a serial port
    By WDT in forum C++ Programming
    Replies: 6
    Last Post: 05-10-2004, 06:31 AM
  4. read from and write to serial port
    By wazilian in forum Networking/Device Communication
    Replies: 3
    Last Post: 04-25-2004, 08:22 AM
  5. Serial port read..can someone tell me..
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 06-27-2002, 08:21 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21