1
\$\begingroup\$

I am new to this kind of communication. I am able to send command from PC to AT90USB1287 microcontroller as well as able to get response using USB communication. Now i have connected another microcontroller(ATmega32) to this board via USART communication. I am trying to send command from PC to this board. Both controllers have different commands. So i have created an array containing both controllers commands in AT90USB1287 controller. My concept is compare the incoming command from PC with all commands in array based on result i am trying to send commands to ATmega32 controller using USART. I have the code as shown below.

#include <90usb1287.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <interface.h>
#include <uart_drv.h>

#define CMD_SIZE    57 
#define USB_CMD_SIZE    52 

flash unsigned char * flash cmdList[CMD_SIZE] = { 
"",             // [0] no com 
"INFO",          // [1] Displayes version date etc. 
"SET",        // [2] Reset CPU 
"boot",         // [3] Goto boot 
"UMP",         // [4] Display manual debug info 
"AUTO",          // [5] Start automatic scanning 
"STRP",         // [6] Stop scanning 
"STAF",         // [7] Set start frequency 
"STOF",         // [8] Set stop frequency 
"RES",          // [9] Display manual debug info 
"RATES",         // [10] Display manual debug info 
"GAINAD",         // [11] Set gain 
"SCANSD",         // [12] Start custom scan 
"SETUP",        // [13] Display manual scan setup info 
"TEMP",         // [14] Set temperature (Celsius) 
..................... 
......................  
"COM",       // [52] no com 
"PINF",         // [53] Displays version date etc. 
"PSCAN",         // [54] 
"PWGF",          // [55] 
"PADC"          // [56] Clear Flash memmory (stopLog)       
};

unsigned char SerIn[SIBUFSIZE];     // Input buffer (raw data)
unsigned char RxCnt;                // Location of next byte to be written
unsigned char RdCnt;                // Location of next byte to be read
unsigned char BufCnt;               // Size of unread contents in ring buffer
unsigned char CompIndex;            // Index in Copmare array
unsigned char Compare[COMPBUFSIZE]; // Command string tokenizer

 unsigned char Uart_CompIndex;            // Index in Copmare array

 unsigned char Command;              // Current Command is executed
 unsigned int  Param;                // Parameter used in command
   float  Param2;               // Optional (second) parameter used in command
  unsigned long Param3;               // Optional (third) parameter in command

 extern unsigned char Plot;
 unsigned char Step;


 // USART1 Receiver interrupt service routine
 interrupt [USART1_RXC] void usart1_rx_isr(void){   

 char status,data;
 status=UCSR1A;
  data=UDR1;
  printf("%c", data);
 }

 // USB Receive
 void catchString(void){

 while(UEBCLX){

    if(++BufCnt >= SIBUFSIZE){               // Increment & check for buffer overflow  
        BufCnt = SIBUFSIZE-1;                // Set to max value   
        return;                              // Skip char
    }else{                                   // Else: if buffer ok
       if(++RxCnt >= SIBUFSIZE) RxCnt = 0;// Increment read counter, if 10 -> 0 (max 9)
    SerIn[RxCnt] = UEDATX;               // Write to SBUF (load the transmit register)
    }
  }  
  }


 // Read from ringbuffer
 char getcharb(void){

    if(BufCnt){                                 // If anything
    BufCnt--;                               // Decrement buffer counter
    if(++RdCnt >= SIBUFSIZE) RdCnt = 0;   // Increment read counter, if 10 -> 0 (max 9)
    return SerIn[RdCnt];                    // Read from SBUF (access receive register)
 }
 return 0;
 }

void getcom(void){

  unsigned char c;

 // Read from ring-buffer and fill in Compare buffer
  while(BufCnt){                          
        c = getcharb();                    
    if(CompIndex >= COMPBUFSIZE) CompIndex = 0;
        // Analyze char
        if(c == '#'){                     
              CompIndex = 0;
        }else if(c == '\r'){                            
              Compare[CompIndex]='\0';    
              break;                      
        }else if(c == '\n'){              
              // Do nothing (ignore)
        }else if(c == 8){                 
              if(CompIndex) CompIndex--;  

        }else if(c == 9){      // Horizontal TAB 
              help();          // Write out cmds

        }else if(c == 27){        // ESC button
              Command = 0;// Stop current command

              Param = 0;       // Clear argument
              Plot = 0;     // Stop plotting
              Step = 0;
        }else{
              Compare[CompIndex++]=c;     
        }if(!BufCnt) return;                                     
  }CompIndex=0;                           

  c = 1;
  while(c<CMD_SIZE){          // For each command       
        if(strncmpf(Compare,cmdList[c],strlenf(cmdList[c])) == 0) break;
        c++; 
  }   

  if(c> USB_CMD_SIZE){    // this is for ATmega32
      Command = c;
       if(isdigit(Compare[strlenf(cmdList[c])])){
              Param = atoi(&Compare[strlenf(cmdList[c])]);
              c = strpos(Compare,':');                 
              if(c > 0){
                    Param2 = atof(&Compare[c+1]);
                    c = strrpos(Compare,':');
                    if(c > strpos(Compare,':')) Param3 = atol(&Compare[c+1]);
                    else  Param3 = 0; 
              }else{ 
                Param2 = 0;
                Param3 = 0;
              }
        }else{
            Param  = 0;
            Param2 = 0;
            Param3 = 0;
        }              
           uart_putchar(Compare);
    //printf("@%s\r\n",&Compare); //Ack command
      }

  if(c<USB_CMD_SIZE){    //If match on normal cmnd in usb
        Command = c;      
        if(isdigit(Compare[strlenf(cmdList[c])])){
              Param = atoi(&Compare[strlenf(cmdList[c])]);
              c = strpos(Compare,':');                 
              if(c > 0){
                    Param2 = atof(&Compare[c+1]);
                    c = strrpos(Compare,':');
                    if(c > strpos(Compare,':')) Param3 = atol(&Compare[c+1]);
                    else  Param3 = 0; 
              }else{ 
                Param2 = 0;
                Param3 = 0;
              }
        }else{
            Param  = 0;
            Param2 = 0;
            Param3 = 0;
        }
        printf("@%s\r\n",&Compare); //Ack command
  }else{

  if(c>CMD_SIZE-1){             // If match on normal commands

        printf("&E;1;\r\n");  // Command not found
       printf("->Unknown command: 's'\r\n",&Compare); // If no match
        Command = 0;
         Param  = 0;
         Param2 = 0;
         Param3 = 0;
}
  }       
}

in the above code i am able to get response from USB controller but not from ATmega32A. I am checking the index is >52(USB_CMD_SIZE) then send to ATmega32 otherwise send to USB)=1287.

I am getting error in this line

uart_putchar(Compare);

I have the function for uart_putchar like this.

void uart_putchar (char ch)
{
   while(!Uart_tx_ready());
   Uart_set_tx_busy(); // Set Busy flag before sending (always)
   Uart_send_byte(ch);   
   return;
 }

even i tried like this

  uart_puts(Compare);

for that i have written like this

  void uart_puts(const char *s)
  {
    while(*s)
      {
         uart_putchar(*s++);
      //    printf("*");
   }    
   }

But nothing is working for me. Can anyone help me how to fix this? Do I need any conversion here? If so, please help me with an example.

When i use this code then i am able to get response from atmega32 but what was the problem is this code is sending all commands to both controllers so at that time i have problem so i am trying the above one.

 void getcom(void){

  unsigned char c;

  // Read from ring-buffer and fill in Compare buffer
  while(BufCnt){                          // while unread contents in ring-buffer
        c = getcharb();                    // fetch next byte

        if(CompIndex >= COMPBUFSIZE) CompIndex = 0;// overflow protection                    
        // Analyze char
        if(c == '#'){                     // Manual start
              CompIndex = 0;
      uart_putchar(c);
        }else if(c == '\r'){              // CR continue (end of cmd without argument)                         
              Compare[CompIndex]='\0';    // fill in end character of comp string
       uart_putchar(c);
              break;                      // possible valid cmd received -> check out
        }else if(c == '\n'){              // New line (ignore)                         
              // Do nothing (ignore)
        }else if(c == 8){                 // Backspace
              if(CompIndex) CompIndex--;  // decrement index
          uart_putchar(c);
        }else if(c == 9){                 // Horizontal TAB 
              help();                     // Write out cmds
            uart_putchar(c);

        }else if(c == 27){                // ESC button
              Command = 0;                // Stop current command
    uart_putchar(c);

              Param = 0;                  // Clear argument
              Plot = 0;                   // Stop plotting
              Step = 0;
        }else{
              Compare[CompIndex++]=c;     // Default action: Store character

        uart_putchar(c);
        }if(!BufCnt) return;              // if no more data to read -> exit                                          
  }CompIndex=0;                           // reset, ready for next command

  c = 1;


  while(c<CMD_SIZE){          // For each command       
        if(strncmpf(Compare,cmdList[c],strlenf(cmdList[c])) == 0) break;
        c++; 
  }   

  if(c<USB_CMD_SIZE){             // If match on normal commands
        Command = c;      
        if(isdigit(Compare[strlenf(cmdList[c])])){
              Param = atoi(&Compare[strlenf(cmdList[c])]);
              c = strpos(Compare,':');                 
              if(c > 0){
                    Param2 = atof(&Compare[c+1]);
                    c = strrpos(Compare,':');
                    if(c > strpos(Compare,':')) Param3 = atol(&Compare[c+1]);
                    else  Param3 = 0; 
              }else{ 
                Param2 = 0;
                Param3 = 0;
              }
        }else{
            Param  = 0;
            Param2 = 0;
            Param3 = 0;
        }
        printf("@%s\r\n",&Compare); //Ack command
  }else{

  if(c>CMD_SIZE-1){             // If match on normal commands

     //   printf("&E;1;\r\n");  // Command not found
    //    printf("->Unknown command: '%s'\r\n",&Compare); // If no match
        Command = 0;
         Param  = 0;
         Param2 = 0;
         Param3 = 0;
}
  }      
 }

this code is sending each and every character to both controllers.

In Compare i have the command like "PSCAN".

printf("@%s\r\n",&Compare); //Ack command

this line printing like

@PSCAN
\$\endgroup\$
9
  • \$\begingroup\$ USB901287 is probably an AT90USB1287. \$\endgroup\$ Jun 14, 2013 at 12:56
  • \$\begingroup\$ @abdullahkahraman Yes \$\endgroup\$
    – reddy
    Jun 14, 2013 at 13:03
  • \$\begingroup\$ How do you define Compare, what does it contain? Could you show more code? \$\endgroup\$ Jun 14, 2013 at 13:04
  • \$\begingroup\$ @abdullahkahraman sure, i have updated now have a look at my new updated code. \$\endgroup\$
    – reddy
    Jun 14, 2013 at 13:42
  • 1
    \$\begingroup\$ I see that Compare is not initialized with any values and is only filled when CompIndex >= COMPBUFSIZE. Try assigning some dummy values to it, in the initialization, maybe. But I am not sure if this is the problem. \$\endgroup\$ Jun 14, 2013 at 13:54

2 Answers 2

2
\$\begingroup\$

Your variable Compare is an array of chars. Your void uart_putchar(char ch) expects a char, not a char array, so you can't use that one to print Compare. Your function void uart_puts(const char *s) does expect a char array, but expects it to be constant as well! You might want to try to change that to void uart_puts(char *s).

Also, it's always a good idea to initalize your variable, like this: unsigned char Compare[COMPBUFSIZE] = {0} - that way you don't get unexpected results when the program didn't write to the variable yet.

By default, stdout is the UART module. So you can also use the standard puts and putchar functions to write a string or a character to the UART, like this: puts(Compare) or putchar(Compare[0]).

If nothing of this works, I'd recommend you to use the printf function. The compiler should optimize that.

\$\endgroup\$
0
\$\begingroup\$

Have you used

    fflush() 

to flush out all the data?The data stays in the buffer unless you do this.I had similar problem.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.