السلام عليكم
في هذا الموضوع يشرح كيف نقرأ البيانات المرسله الى برتوكول uart اي RX, TX
للعلم كنا شرحنا في موضوع GSM طريقه قراءه حرف حرف طبعا هي بسيطه واليكم كود الي استخدمناها في القراءه البيانات
كود:
void interrupt(){
char tmp;
char i;
if (RCIF_bit == 1) { // Do we have uart rx interrupt request?
tmp = UART_Rd_Ptr(); // Get received byte
// Process reception through state machine
// We are parsing only "OK" and "> " responses
switch (gsm_state) {
case 0: {
response = -1; // Clear response
if (tmp == 'O') // We have 'O', it could be "OK"
gsm_state = 1; // Expecting 'K'
if (tmp == '>') // We have '>', it could be "> "
gsm_state = 10; // Expecting ' '
if (tmp == 'E') // We have 'E', it could be "> "
gsm_state = 30; // Expecting 'R'
if (tmp == 'S') // We have 'S', it could be "Status?" or "Set"
gsm_state = 40; // Expecting 't'
if (tmp == 'R') // We have 'R', it could be "RDx" or
gsm_state = 50; // Expecting D
if (tmp == 'C') // We have 'C', it could be "Clear"
gsm_state = 110; // Expecting l
if (tmp == 'U') // We have 'U', it could be "UNREAD"
gsm_state = 120; // Expecting l
break;
}
case 1: {
if (tmp == 'K') { // We have 'K' ->
response = GSM_OK; // We have "OK" response
gsm_state = 20; // Expecting CR+LF
}
else
gsm_state = 0; // Reset state machine
break;
}
case 10: {
if (tmp == ' ') {
response_rcvd = 1; // We have "> " response
response = GSM_Ready_To_Receive_Message; // Set reception flag
responseID = response; // Set response ID
}
gsm_state = 0; // Reset state machine
break;
}
case 20: {
if (tmp == 13) // We have 13, it could be CR+LF
gsm_state = 21; // Expecting LF
else
gsm_state = 0; // Reset state machine
break;
}
case 21: {
if (tmp == 10) { // We have LF, response is complete
response_rcvd = 1; // Set reception flag
responseID = response; // Set response ID
}
gsm_state = 0; // Reset state machine
break;
}
case 30: {
if (tmp == 'R') // We have 'R', it could be "ERROR"
gsm_state = 31; // Expecting 'R'
else
gsm_state = 0; // Reset state machine
break;
}
case 31: {
if (tmp == 'R') // We have 'R', it could be "ERROR"
gsm_state = 32; // Expecting 'O'
else
gsm_state = 0; // Reset state machine
break;
}
case 32: {
if (tmp == 'O') // We have 'O', it could be "ERROR"
gsm_state = 33; // Expecting 'R'
else
gsm_state = 0; // Reset state machine
break;
}
case 33: {
if (tmp == 'R'){ // We have 'R'
response_rcvd = 1; // We have "ERROR" response
response = GSM_ERROR; // Set reception flag
responseID = response; // Set response ID
}
gsm_state = 0; // Reset state machine
break;
}
case 40: {
if (tmp == 't') // We have 't', it could be "Status?"
gsm_state = 41; // Expecting 'a'
else
if (tmp == 'e') // We have 'e'. it could be "Set"
gsm_state = 100;
else
gsm_state = 0; // Reset state machine
}; break;
case 41: {
if (tmp == 'a') // We have 'a', it could be "Status?"
gsm_state = 42; // Expecting 't'
else
gsm_state = 0; // Reset state machine
break;
}
case 42: {
if (tmp == 't') // We have 't', it could be "Status?"
gsm_state = 43; // Expecting 'u'
else
gsm_state = 0; // Reset state machine
break;
}
case 43: {
if (tmp == 'u') // We have 'u', it could be "Status?"
gsm_state = 44; // Expecting 's'
else
gsm_state = 0; // Reset state machine
break;
}
case 44: {
if (tmp == 's') // We have 's', it could be "Status?"
gsm_state = 45; // Expecting '?'
else
gsm_state = 0; // Reset state machine
break;
}
case 45: {
if (tmp == '?'){ // We have '?'
status_req = 1; // Status has been requested!
}
gsm_state = 0; // Reset state machine
break;
}
case 50: {
if (tmp == 'D') // We have 'D', it could be "RDx"
gsm_state = 51; // Expecting number
else
gsm_state = 0; // Reset state machine
break;
}
case 51: {
if (tmp >= '0' && tmp <= '7'){ // We have pin number, it could be "RDx"
if (set_stat)
Digital_O |= 1 << (tmp - '0');
if (clr_Stat)
Digital_O &= (0xFF ^(1 << (tmp - '0')));
}
PORT_flag = 1;
gsm_state = 0; // Reset state machine
}; break;
case 100: {
if (tmp == 't'){ // We have 't', we received set command
set_stat = 1; // Set following bits
clr_stat = 0; // Do not clear following bits
}
gsm_state = 0;
}; break;
case 110: {
if (tmp == 'l'){ // We have 'l', it could be Clear
gsm_state = 111;
}
else
gsm_state = 0;
}; break;
case 111: {
if (tmp == 'e'){ // We have 'e', it could be Clear
gsm_state = 112;
}
else
gsm_state = 0;
}; break;
case 112: {
if (tmp == 'a'){ // We have 'a', it could be Clear
gsm_state = 113;
}
else
gsm_state = 0;
}; break;
case 113: {
if (tmp == 'r'){ // We have 'r', we have received Clear
clr_stat = 1; // Clear following bits
set_stat = 0; // Do not set following bits
}
gsm_state = 0;
}; break;
case 120: {
if (tmp == 'N')
gsm_state = 121;
else
gsm_state = 0;
}; break;
case 121: {
if (tmp == 'R')
gsm_state = 122;
else
gsm_state = 0;
}; break;
case 122: {
if (tmp == 'E')
gsm_state = 123;
else
gsm_state = 0;
}; break;
case 123: {
if (tmp == 'A')
gsm_state = 124;
else
gsm_state = 0;
}; break;
case 124: {
if (tmp == 'D'){
response_rcvd = 1; // We have "ERROR" response
response = GSM_UNREAD; // Set reception flag
responseID = response; // Set response ID
Unread_flag = 1;
}
gsm_state = 0;
}; break;
default: { // Unwanted character
gsm_state = 0; // Reset state machine
}
}
==================================
هسى اريد تبسيط اكثر لعمليه القراءه للعلم طريقه وحيده ناجحه هي بأستخدام الانتربت
طبعا هذا موضوع مهم لانك سوف تستخدمه بعده تطبيقات منها GSM , XBEE و كومبيوتر وكثير من تطبيقات
في مشاركه القادمه سوضح لكم طريقه وأستخدام الامر memcmp
ماذا يعني memcmp
تعني مقارنه بين مصفوفتين فأذا كانت متطابقه ترجع 0 واذا لم تكن متطابقه ترجع الفرق بين الاول حرف مختلف
هذا شكل داله
كود:
int memcmp(void *s1, void *s2, int n);
Function compares the first n characters of objects pointed to by s1 and s2 and returns zero if the objects are equal, or returns a difference between the first differing characters (in a left-to-right evaluation). Accordingly, the result is greater than zero if the object pointed to by s1 is greater than the object pointed to by s2 and vice versa.
طبعا يتم مقارنه n من حروف s1 مع s2
يعني مثلا
كود:
char txt[] = "mikroElektronika";
char txt_sub[] = "mikro;
res = memcmp(txt, txt_sub, 16); // returns 69, which is ASCII code of the first differing character - letter 'E'
تلاحظون بمثال تم مقارنه 16 حرف من txt مع txt_sub وبالطبع هنا غير متطابقه لذا رجعت لنا الفرق وهو حرف E
مثال اخر
كود:
memcmp(mess,"R1ON",4)
هنا تعني قارن اول 4 احرف من mess مع R1ON اذا كانت متطابقه ترجع 0 والا ترجع قيمه الفرق
مثال اخر
كود:
memcmp(mess+5,"R1OFF",5)
هنا تعني قارن 5 الاحرف من mess ابتداءا من عنصر 5 وقارنها مع R1OFF
===============================
x!
اذا كانت x=0 فالنتيجه x!=1
واذا كانت x لاتساوي 0 فالنتيجه دائما x!=0