• AVR Freaks

AnsweredHot!Serial Communication Example / UART Meaningless Data

Page: < 123 Showing page 3 of 3
Author
du00000001
Just Some Member
  • Total Posts : 3985
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: Serial Communication Example / UART Meaningless Data 2020/05/25 17:02:40 (permalink)
+2 (2)
OK  - - -  "PIC rookie" == "C rookie" (== "microcontroller rookie"(?)")
Which will usually match what's demonstrated by "EE students" (that seem to routinely not attend the "C 1o1" course).
A decent book about C programming might help, e.g. "C for Dummies". This one is not exactly for embedded programming, but many of my former embedded colleagues started with this. Quite useful.
 
Why the break didn't work when comparing?
... strcmp(Output,"F",1) ...

It would have breaked on "F000" (already at the first character).
"Output" is synonymous to "Output[0]", while you should have compared to "Output[ i ]". (Maybe you intended this, just missing the index "", maybe you didn't grasp it yet.)
 
Why your comparison once worked?
As you don't initialize data[10], by dumb luck. Seems data[4] (5th element) held an ASCIIZ (character equal 0x00) - by chance.
BTW: to compareva single character, str(n)cmp is overkill. You're free to just compare a variable to a single character that is given between single quotes ('F') as opposed to a string (full name "string of characters") that has to be given between double quotes ("F"). The fine difference: any string definition adds an ASCIIZ to the string you give (that's about what the "" affect) while the single quotes ('') don't.

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#41
sevincom
New Member
  • Total Posts : 14
  • Reward points : 0
  • Joined: 2020/05/12 16:13:18
  • Location: 0
  • Status: offline
Re: Serial Communication Example / UART Meaningless Data 2020/05/25 17:15:51 (permalink)
0
ric, You are totally right about my knowledge level, also I am not good on any text based programming languages. Mainly I am dealing with plc programming and G.
I am trying to develop C skills and those responses were really helpful. It will probably take some time for me to read again, research and fully understand every tip.
 
Thanks.
post edited by sevincom - 2020/05/25 17:18:00
#42
sevincom
New Member
  • Total Posts : 14
  • Reward points : 0
  • Joined: 2020/05/12 16:13:18
  • Location: 0
  • Status: offline
Re: Serial Communication Example / UART Meaningless Data 2020/05/25 17:26:03 (permalink)
0
du00000001
Why your comparison once worked?
As you don't initialize data[10], by dumb luck. Seems data[4] (5th element) held an ASCIIZ (character equal 0x00) - by chance.
 



Actually I also initialized data's length = 10, as you can see the code in my post.
So, both Read commands are same with the version including analog or not. Below is the version not including analog and works, undocumented but just to show.
 
 
/*
 * File: serial_main.c
 * Author: PC4
 *
 * Created on May 21, 2020, 11:45 PM
 */
// PIC16F877A Configuration Bit Settings

// 'C' source line config statements

// CONFIG
#pragma config FOSC = XT // Oscillator Selection bits (XT oscillator)
#pragma config WDTE = ON // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#define _XTAL_FREQ 4000000
#include <xc.h>
#include <stdint.h>
#include <string.h>
#include <stdio.h>
#include "config_lcd.h"
#include "myuart.h"


void main(void) {
    TRISA=0XFF;
    TRISB=0X00;
    PORTB=0X00;
    TRISD=0b00000011;
    unsigned char sayici;
    unsigned char adres=0x00;
    sayici=eeprom_read(adres);
    Lcd_Init();
    Lcd_Clear();
    Lcd_Set_Cursor(1,5);
    Lcd_Write_String("ACILIS");
    __delay_ms(200);
    Lcd_Clear();

    char data[10];
    char tetik[1];
    char s[4];
    
    UART_Init(9600);
    OPTION_REGbits.PSA=1;
    OPTION_REGbits.PS0=0;
    OPTION_REGbits.PS1=1;
    OPTION_REGbits.PS2=1;
    CLRWDT();
    while(1){
        __delay_ms(10);
        if(PORTDbits.RD0==1){
            sayici++;
            eeprom_write(adres,sayici);
            if(sayici==55){
                sayici=0;
            }
            sprintf(s, "%2d", sayici);
            Lcd_Set_Cursor(1,10);
            Lcd_Write_String(s);
            UART_Write_Text(s);
            __delay_ms(200);
        }

        if(UART_Data_Ready()){
            UART_Read_Text(tetik,1);
            Lcd_Set_Cursor(1,1);
            Lcd_Write_String(tetik);
            if(strcmp(tetik,"k")==0){
                UART_Read_Text(data,4);
                Lcd_Write_String(data);
                if(strcmp(data,"0001")==0){
                    PORTB=0b00000001;
                    UART_Write_Text("o");
                    __delay_ms(100);
                }
                else if(strcmp(data,"000A")==0){
                    PORTB=0b00000000;
                    UART_Write_Text("o");
                    __delay_ms(100);
                }
                else{
                   UART_Write_Text("N");
                   __delay_ms(100);
                }
                
            }
        }
        CLRWDT();
    }
}

#43
ric
Super Member
  • Total Posts : 28735
  • Reward points : 0
  • Joined: 2003/11/07 12:41:26
  • Location: Australia, Melbourne
  • Status: online
Re: Serial Communication Example / UART Meaningless Data 2020/05/25 17:31:53 (permalink)
+1 (1)
sevincom
Actually I also initialized data's length = 10, as you can see the code in my post.

That is NOT initialising the variable, just telling the compiler how much RAM to set aside for it.
You can often get away with that because the compiler clears all global variables to zero on startup.
 

So, both Read commands are same with the version including analog or not. Below is the version not including analog and works, undocumented but just to show.
 

Is that code using the UART_Read_Text() from your previous posts?
If yes, then it's just dumb luck that this code works, as that function does not correctly terminate recieved strings.
 
If you fill a 10 byte array with 10 characters, then any functions expecting a string will end up looking at the 11th position (i.e. whatever variable is next in memory) to find the end of the string.
 
post edited by ric - 2020/05/25 17:34:00

I also post at: PicForum
Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
NEW USERS: Posting images, links and code - workaround for restrictions.
To get a useful answer, always state which PIC you are using!
#44
du00000001
Just Some Member
  • Total Posts : 3985
  • Reward points : 0
  • Joined: 2016/05/03 13:52:42
  • Location: Germany
  • Status: offline
Re: Serial Communication Example / UART Meaningless Data 2020/05/25 17:37:20 (permalink)
+1 (1)
Hi,
while you gave data[]'s length (10), you did not initialize the elements of data[]. That one would e.g. have been
char data[10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };

 
P.S.:
"G" as in LabView or as in "G-code" ?  (Just curious.)
post edited by du00000001 - 2020/05/26 07:31:48

PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
#45
sevincom
New Member
  • Total Posts : 14
  • Reward points : 0
  • Joined: 2020/05/12 16:13:18
  • Location: 0
  • Status: offline
Re: Serial Communication Example / UART Meaningless Data 2020/05/25 17:42:48 (permalink)
0
Thank you both again.
 
I have used G-code too for mach3, but I meant LabVIEW. Also currently sending the serial data from LabVIEW.
#46
mlp
boots too small
  • Total Posts : 967
  • Reward points : 0
  • Joined: 2012/09/10 15:12:07
  • Location: previously Microchip XC8 team
  • Status: offline
Re: Serial Communication Example / UART Meaningless Data 2020/05/26 07:17:26 (permalink)
+2 (2)
ric
If you fill a 10 byte array with 10 characters, then any functions expecting a string will end up looking at the 11th position (i.e. whatever variable is next in memory) to find the end of the string.

and for pedantic correctness one should refer to that end-of-string marker as NUL (note, only one "ell"), which is the character also known as '\0' or just 0. The null-pointer value NULL, confusingly also able to be called 0, is a different thing.
 
Names matter.
post edited by mlp - 2020/05/26 07:19:59

Mark (this opinion available for hire)
#47
Page: < 123 Showing page 3 of 3
Jump to:
© 2020 APG vNext Commercial Version 4.5