Serial.read กับ string ครับ

Started by pacozaa, February 08, 2013, 12:21:45 PM

Previous topic - Next topic

pacozaa

char inData[20]; // Allocate some space for the string
char inChar=-1; // Where to store the character read
byte index = 0; // Index into array; where to store the character

void setup() {
    Serial.begin(9600);
    Serial.write("Power On");
}

char Comp(char* This) {
    while (Serial.available() > 0) // Don't read unless
                                   // there you know there is data
    {
        if(index < 19) // One less than the size of the array
        {
            inChar = Serial.read(); // Read a character
            inData[index] = inChar; // Store it
            index++; // Increment where to write next
            inData[index] = '\0'; // Null terminate the string
        }
    }

    if (strcmp(inData,This)  == 0) {
        for (int i=0;i<19;i++) {
            inData[i]=0;
        }
        index=0;
        return(0);
    }
    else {
        return(1);
    }
}

void loop()
{
    if (Comp("m1 on")==0) {
        Serial.write("Motor 1 -> Online\n");
    }
    if (Comp("m1 off")==0) {
        Serial.write("Motor 1 -> Offline\n");
    }
}


อยากทราบว่าโปรแกรมนี้ทำงานยังไงน่ะครับ
http://stackoverflow.com/questions/5697047/convert-serial-read-into-a-useable-string-using-arduino


ผมไม่เข้าใจอยู่สองจุดครับ

char Comp(char* This) ทำงานยังไงครับ
(strcmp(inData,This)  == 0) ทำงานยังไงครับ
ถ้าเป็นไปได้รบกวนอธิบายโปรแกรมโดยรวมให้หน่อยครับ

tumrobot

สั้นๆ นะครับ ตามความเข้าใจของผม

char Comp(char* This) เป็นฟังก์ชั่นที่รับ ข้อมูลสตริง เข้ามาแล้วนำมาเปรียบข้อมูลที่รับมาจาก Serial
โดยใน Loop main นั้นมีการส่งข้อมูล "m1 on" และ "m1 off" เข้ามาในฟังก์ชั่น

ถ้าหากข้อมูลที่ส่งเข้ามาจาก Serial ตรงกัน ตัวฟังก์ชั่น char Comp(char* This) จะคือค่า 0 ออกมา
และใน main ก็จะ print serial เป็น "Motor 1 -> Online\n" หรือ "Motor 1 -> Offline\n" น่ะครับ

ส่วน (strcmp(inData,This)  == 0) ก็คือ String Compare น่ะครับ ถ้า inData = This ก็จะเท่ากับ 0 ครับ

:)
Contact Me:
Line ID: openfog      

My Web: www.openfog.net
GPS Tracking Online: www.openfoggps.com

pacozaa

1.ทำไมต้อง มีเครื่องหมาย * ด้วยอ่ะครับ

2.String object เปลืองหน่วยความจำมากไหมครับ เห็นในเว็บ arduino.cc เขาบอกว่าเปลืองมาก ไม่แน่ใจว่า
พอทำโปรเจคแล้วจะมีปัญหาภายหลังรึเปล่าน่ะครับ พอจะทราบวิธีดูไหมครับว่าหน่วยความจำจะพอหรือเปล่า


firmware.c

ตอบข้อ 1) ให้ครับ เครื่องหมาย * คือ pointer ครับ char * คือ pointer ที่ใช้ชี้ไปยังตัวแปรประเภท character  แนะนำให้หาหนังสือ c programming มาอ่าน

ส่วนข้อสองรอสมาชิกท่านอื่นที่ใช้ arduino platform มาตอบแล้วกันนะครับ
IAR Embedded Workbench for ARM
AVR-Studio + AVR-GCC
CodeBlocks + MinGw
CodeBlocks + Gtk+

pacozaa

ที่เขาใช้ pointer เพราะอะไรหรอครับ จริงๆเขาใช้ (char This) ก็ ได้ใช่ไหมครับ
เพราะช่วยประหยัดหน่วยความจำหรือเปล่าครับ

tumrobot

ใช้ char This ไม่ได้ครับ
ก่อนอื่นคุณต้องไปทำความเข้าใจก่อน char คืออะไร และ string คืออะไร แตกต่างกันยังไง
แล้วคุณจะเข้าใจเองว่า ทำไมต้องใช้ char* This
Contact Me:
Line ID: openfog      

My Web: www.openfog.net
GPS Tracking Online: www.openfoggps.com

pacozaa

ขอบคุณครับ เดี๋ยวไปทำความเข้าใจก่อนคร้าบบ

samira

Quote from: pacozaa on February 08, 2013, 11:26:54 PM
2.String object เปลืองหน่วยความจำมากไหมครับ เห็นในเว็บ arduino.cc เขาบอกว่าเปลืองมาก ไม่แน่ใจว่า
พอทำโปรเจคแล้วจะมีปัญหาภายหลังรึเปล่าน่ะครับ พอจะทราบวิธีดูไหมครับว่าหน่วยความจำจะพอหรือเปล่า

อยากทราบว่า ใช้ arduino ตัวใหน ( ธรรมดา หรือ arduino Mega) ครับ
" If you're born poor, it's not your mistake. But if you die poor, it's your mistake"
Bill Gates.