nova verzija

Files at this revision

API Documentation at this revision

Comitter:
bosko001
Date:
Fri Feb 21 11:44:48 2020 +0000
Parent:
1:10e2a0bef1b4
Commit message:
nova verzija

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Sat Feb 15 15:38:25 2020 +0000
+++ b/main.cpp	Fri Feb 21 11:44:48 2020 +0000
@@ -35,45 +35,51 @@
 
 int strcount( char *ps)
 {
- int i=0;
-  for(i=0; ps[i] != 0; i++);
-  return i;   
+    int i=0;
+    for(i=0; ps[i] != 0; i++);
+    return i;
 }
 
 void tcpThread_fun( EthernetInterface *e);
 void requestMessage( EthernetInterface *e);
 
- DigitalOut tast(PTB19,0);               
-//RawSerial rs485(PTC17,PTC16);           
+DigitalOut tast(PTB19,0);
+//RawSerial rs485(PTC17,PTC16);
 RawSerial rs485(PTD3,PTD2);
 
-struct s_rm
-{
+struct s_rm {
     EthernetInterface *e;
     SocketAddress *sa;
     char *tx_msg;
     void (*fun_rec)(char*);
 };
 
-char crm_req[]={'{','1','|','1','}'};//"{1|1}";
+char crm_req[]= {'{','1','|','1','}'}; //"{1|1}";
 char crm_resp[100];
 void crm_fun( char *rx)
 {
-  printf("crm_resp: %s\n\r",rx);
-  strcmp(crm_resp, rx);
+    printf("crm_resp: %s\n\r",rx);
+    strcmp(crm_resp, rx);
 }
 
-char srm_req[]="{5|1|1}";
+char srm_req[]="{5|1|0}";
 void srm_fun( char *rx )
 {
-   printf("srm_resp: %s\n\r",rx);
-   char *strno= extract_string(';',rx, 2);
-   char boja = 0;
-   if(atoi(strno) == 0) { boja = 1; }
-   else if(atoi(strno) >0 && atoi(strno)<5) { boja = 4; }
-   else { boja = 2; }
-   printf("broj: %d - boja: %d\n\r", atoi(strno),boja);
-   putOnDisp((char*)strno, boja);
+    printf("srm_resp: %s\n\r",rx);
+    char *strno= extract_string(';',rx, 2);
+    char boja = 0;
+    if(atoi(strno) <= 0) {
+        boja = 1;
+    } else if(atoi(strno) >0 && atoi(strno)<5) {
+        boja = 4;
+    } else {
+        boja = 2;
+    }
+    printf("broj: %d - boja: %d\n\r", atoi(strno),boja);
+    if (atoi(strno) < 0)
+        putOnDisp("0", boja);
+    else
+        putOnDisp((char*)strno, boja);
 }
 
 
@@ -81,57 +87,62 @@
 
 int main(void)
 {
-   
+
+
+
+    rs485.format(8, mbed::RawSerial::Even, 1);
 
- 
- rs485.format(8, mbed::RawSerial::Even, 1);
- 
     Thread t;
- //   t.start( slanjefun );
+//   t.start( slanjefun );
+
+    putOnDisp("elcom", 3);
 
- 
- 
-     if( (err = eth.set_network(IP_ADDR,NET_MASK,GATW_ADDR))!= NSAPI_ERROR_OK )  { printf(" greska setovanja mreze %d \n\r", err); return 1; }    
-     if( (err = eth.connect())  != NSAPI_ERROR_OK )  { printf(" greska konekcije %d \n\r", err); return 1; }
-    
+    if( (err = eth.set_network(IP_ADDR,NET_MASK,GATW_ADDR))!= NSAPI_ERROR_OK )  {
+        printf(" greska setovanja mreze %d \n\r", err);
+        return 1;
+    }
+    if( (err = eth.connect())  != NSAPI_ERROR_OK )  {
+        printf(" greska konekcije %d \n\r", err);
+        return 1;
+    }
+
     const char *ip = eth.get_ip_address() ;
-    
-    
-   printf( "MAC adresa: %s\n\r", eth.get_mac_address() );
+
+
+    printf( "MAC adresa: %s\n\r", eth.get_mac_address() );
     printf( "IP adresa:  %s\n\r", ip);
     printf( "net mask:   %s\n\r", eth.get_netmask() );
     printf( "GW adresa:  %s\n\r", eth.get_gateway() );
- 
+
     SocketAddress sa("192.168.2.254", 12197);
- 
- struct s_rm s_crm ={ &eth, &sa, crm_req, crm_fun }; 
- struct s_rm s_srm ={ &eth, &sa, srm_req, srm_fun }; 
-    
-//    Thread TCPThread;
-//    TCPThread.start( callback( tcpThread_fun, &eth ));
-    
-    
-      Thread CRMThread;
-      CRMThread.start( callback( requestMessageThread, &s_crm ));
-      CRMThread.join();
+
+    struct s_rm s_crm = { &eth, &sa, crm_req, crm_fun };
+    struct s_rm s_srm = { &eth, &sa, srm_req, srm_fun };
+
+
+    putOnDisp("flash", 2);
+
+    Thread CRMThread;
+    CRMThread.start( callback( requestMessageThread, &s_crm ));
+    CRMThread.join();
     int i=0;
-  
-    while(true)
-    {
-      thread_sleep_for(10000);
-      Thread SRMThread;
-      printf("Thread id= %d\n\r",(int)&SRMThread);
-      SRMThread.start( callback( requestMessageThread, &s_srm ));
-      SRMThread.join();
-      printf("main %d\n\r",i++);
+//        thread_sleep_for(15000);
+
+    while(true) {
+        thread_sleep_for(5000);
+
+        Thread SRMThread;
+        SRMThread.start( callback( requestMessageThread, &s_srm ));
+        SRMThread.join();
+        printf("main %d\n\r",i++);
     }
- //   printf("\n\rmain: Kraj, error=%d\n\r",err);    fflush(stdout);
+//   printf("\n\rmain: Kraj, error=%d\n\r",err);    fflush(stdout);
 }
 
 
 
 
-    unsigned char sendBuffer[]= "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Type: text/html\r\n\r\nMarko je ovde";                  // HTTP Message Body, length = 11
+unsigned char sendBuffer[]= "HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Type: text/html\r\n\r\nMarko je ovde";                  // HTTP Message Body, length = 11
 unsigned char sb[1000]="HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Type: text/html\r\n\r\n";
 
 
@@ -139,59 +150,6 @@
 unsigned char txbuf[100]="test konekcije";
 unsigned char rxbuf[100];
 
-void tcpThread_fun( EthernetInterface *e)
-{
-
-    TCPSocket tcpSocket;  
-    nsapi_error_t err=NULL;      
-    SocketAddress sa("192.168.1.26", 12197);
-    printf("ip= %s, ver= %d, port= %d\n\r", sa.get_ip_address(), sa.get_ip_version(), sa.get_port() ); 
-    int i=1;
-    
-    while(true)
-    {
-     if((err=tcpSocket.open( e )) ==0 )        
-     {
-        if((err = tcpSocket.connect(sa)) == 0)
-        {
-            while( true)
-            {
-                printf("iteracija %d   \n\r", i);
-                    if(strcount((char*)txbuf) )
-                    {
-                        printf(" za slanje %d bajtova: %s\n\r", strcount((char*)txbuf), txbuf);
-                        if( (err = tcpSocket.send(txbuf,strcount((char*)txbuf))<0 ) )   {    printf("send err %d\n\r", err); break; }
-                        else(" poslano %d bajtova od: %s\n\r", err, txbuf);
-                        memset(txbuf, 0, sizeof(txbuf) );
-                    }
-                    else
-                    {
-                        if( (err = tcpSocket.recv(rxbuf,sizeof(rxbuf)) <0 )) {    printf("recv err %d\n\r", err); break;  }
-                        else                                                 
-                        {
-                          
-                                printf(" prim tel %d: %s\n\r", err, rxbuf) ; 
-                                {
-                                  extern  void putOnDisp(char *, char );
-                                     putOnDisp((char*)rxbuf, 1);
-                                }
-                                memcpy(txbuf, rxbuf, sizeof(txbuf) );
-                                memset(rxbuf, 0, sizeof(rxbuf)); 
-                        } 
-                    }
-                 printf("kraj iteracije %d   \n\r", i++);
-                 fflush(stdout);
-            }
-        }
-        else printf("greska otvaranja konekcije %d\n\r", err);
-      }
-      else printf("greska otvaranja socketa %d\n\r", err);
-      
-      printf("zatvaranje konekcije\n\r");
-      tcpSocket.close();
-      thread_sleep_for(2000);
-     }
-}
 
 char conf_server_ip[] = "192.168.1.26";
 int conf_server_port = 12197;
@@ -200,121 +158,35 @@
 
 
 
-
-void requestMessage(EthernetInterface *e)
+void requestMessageThread(struct s_rm *s)
 {
-    
-    while(true)
-    {
-        
-        TCPSocket tcpSocket;  
-        nsapi_error_t err=NULL;      
-        SocketAddress sa(conf_server_ip, conf_server_port);
-        char rxbuf[1024];
-        memset(rxbuf, 0, sizeof(rxbuf)); 
-        if((err=tcpSocket.open( e )) ==0 )        
-        {
-            if((err = tcpSocket.connect(sa)) == 0)
-            {
-                if( (err = tcpSocket.send(msg_request_message,strcount((char*)msg_request_message))<0 ) )   
-                {    
-                    printf("requestMessage - send err: %d\n\r", err);
-                }
-                else
-                {
-                    printf("requestMessage - poslano: %d bajtova od: %s\n\r", err, msg_request_message);
-                    if( (err = tcpSocket.recv(rxbuf,sizeof(rxbuf)) <0 )) {    printf("requestMessage - recv err %d\n\r", err); }
-                    else                                                 
-                    {
-                      
-                            printf("requestMessage - prim tel %d: %s\n\r", err, rxbuf) ; 
-                            
-//                            char* parts[100];
-//                            int partcount = 0;
-//                            
-//                            parts[partcount++] = rxbuf;
-//                            
-//                            char* ptr = rxbuf;
-//                            while(*ptr) { //check if the string is over
-//                                if(*ptr == ';') {
-//                                    *ptr = 0;
-//                                    parts[partcount++] = ptr + 1;
-//                                }
-//                                ptr++;
-//                            }
-                            
-                            extern char * extract_string( char delimiter, char *ulazni_str, int n_str );
 
-                            char *strno= extract_string(';',rxbuf, 2);
-                            printf("%s\n\r",strno);
-                            int color = 0;
-                            int brojMesta = atoi(strno);
-                            printf("Broj mesta: %d\n\r",brojMesta);
-                            if (brojMesta==0) { color=1; }
-                            else if(brojMesta>0&&brojMesta<10){ color=4; }
-                            else { color=2; }
-                            printf("Color: %d\n\r",color);
-                            {
-                              extern  void putOnDisp(char *, char );
-                                 putOnDisp((char*)strno, color);
-                            } 
-                    } 
+    TCPSocket tcpSocket;
+    nsapi_error_t err=NULL;
+    char rxbuf[1024];
+    memset(rxbuf, 0, sizeof(rxbuf));
+    if((err=tcpSocket.open( s->e )) ==0 ) {
+        if((err = tcpSocket.connect(*(s->sa))) == 0) {
+            char *ss = s->tx_msg;
+            if( (err = tcpSocket.send(s->tx_msg,strcount(ss))<0 ) ) {
+                printf("requestMessage - send err: %d\n\r", err);
+            } else {
+                printf("requestMessage - poslano: %d bajtova od: %s\n\r", err, s->tx_msg);
+                if( (err = tcpSocket.recv(rxbuf,sizeof(rxbuf)) <0 )) {
+                    printf("requestMessage - recv err %d\n\r", err);
+                } else {
+
+                    printf("requestMessage - prim tel %d: %s\n\r", err, rxbuf) ;
+                    s->fun_rec( rxbuf );
                 }
             }
-            
-            printf("requestMessage - zatvaranje konekcije\n\r");
-            tcpSocket.close();
         }
-        
-      thread_sleep_for(30000);
-    }
-    
-}   
-
-//
-//struct s_rm
-//{
-//    EthernetInterface *e;
-//    SocketAddress *sa;
-//    char *tx_msg;
-//    void (*fun_rec)(char*);
-//};
-
 
-void requestMessageThread(struct s_rm *s)
-{
-        
-        TCPSocket tcpSocket;  
-        nsapi_error_t err=NULL;      
-        char rxbuf[1024];
-        memset(rxbuf, 0, sizeof(rxbuf)); 
-        if((err=tcpSocket.open( s->e )) ==0 )        
-        {
-            if((err = tcpSocket.connect(*(s->sa))) == 0)
-            {
-                char *ss = s->tx_msg;
-                if( (err = tcpSocket.send(s->tx_msg,strcount(ss))<0 ) )   
-                {    
-                    printf("requestMessage - send err: %d\n\r", err);
-                }
-                else
-                {
-                    printf("requestMessage - poslano: %d bajtova od: %s\n\r", err, s->tx_msg);
-                    if( (err = tcpSocket.recv(rxbuf,sizeof(rxbuf)) <0 )) {    printf("requestMessage - recv err %d\n\r", err); }
-                    else                                                 
-                    {
-                      
-                            printf("requestMessage - prim tel %d: %s\n\r", err, rxbuf) ; 
-                            s->fun_rec( rxbuf );                            
-                    } 
-                }
-            }
-            
-            printf("requestMessage - zatvaranje konekcije\n\r");
-            tcpSocket.close();
-        }
-    
-}   
+        printf("requestMessage - zatvaranje konekcije\n\r");
+        tcpSocket.close();
+    }
+
+}
 
 //
 //enum nsapi_error {
@@ -342,49 +214,54 @@
 
 
 
-void ev(int){tast=0;}
-
-unsigned char buf_tel[100] = {0x71,0x00,0x0D,0x71,0x43,0x01,0x1B,0x4A,0x01,0x01,0x1B,0x43,1,0x33,0xD5,0x16};
-void putOnDisp( char *s, char boja)
+void ev(int)
 {
-    int ubb = 13+strlen(s)+1+1-4-2; // ubb = ukupan broj bajtova
-    unsigned char lh, ll;
-    ll=ubb;
-    lh=ubb>>8;
-    printf("Boja: %d\n\r",boja);
-    buf_tel[12]=boja;
-    memcpy(buf_tel+13, s, strlen(s)+2);
-    
-    int suma=0;
-    for(int i = 4; i < 13+strlen(s); i++) {
-        suma +=buf_tel[i]; 
-    }
-    
-    buf_tel[13+strlen(s)] = suma;
-    buf_tel[13+strlen(s)+1] = 22;
-    buf_tel[1]=lh;
-    buf_tel[2]=ll;
-   
-    fflush(stdout);
-    
-    tast=1;
-    rs485.write(buf_tel, 21,ev);
-}
-void putOnDispNo(int broj, char boja)
-{
-     char s[10];
-    sprintf( s, "%d", broj);  
-    putOnDisp( s, boja); 
+    tast=0;
 }
 
 
-void slanjefun(){
-    
+
+unsigned char buf_tel[100] = {0x71,0x00,0x0D,0x71,0x43,0x01,0x1B,0x4A,0x01,0x01,  /*0x1b,0x45,0x1,*/  0x1B,0x43,1,0x33,0xD5,0x16};
+#define NO_BEFORE_DATA 13
+#define NO_WITH_DATA (NO_BEFORE_DATA + strlen(s))
+
+void putOnDisp( char *s, char boja)
+{
+    int ubb = NO_BEFORE_DATA+strlen(s)+1+1-4-2; // ubb = ukupan broj bajtova
+    buf_tel[NO_BEFORE_DATA-1]=boja;
+    memcpy(buf_tel+NO_BEFORE_DATA, s, strlen(s)+2);
+
+    char suma=0;
+    for(int i = 4; i < NO_WITH_DATA; i++) {
+        suma +=buf_tel[i];
+    }
+
+    buf_tel[NO_WITH_DATA] = suma;
+    buf_tel[NO_WITH_DATA+1] = 22;
+    buf_tel[1]=ubb>>8;
+    buf_tel[2]=ubb;
+
+
+    tast=1;
+    rs485.write(buf_tel, NO_WITH_DATA+2+2,ev);
+}
+
+void putOnDispNo(int broj, char boja)
+{
+    char s[10];
+    sprintf( s, "%d", broj);
+    putOnDisp( s, boja);
+}
+
+
+void slanjefun()
+{
+
     int j=0;
-    
+
 
     while (true) {
- //       printf("disp thred %d\n\r",j);
+//       printf("disp thred %d\n\r",j);
         if(j >10 && j<15 ) putOnDisp("des",1);
         else putOnDispNo(j,1);
         j++;
@@ -395,21 +272,18 @@
 
 char * extract_string( char delimiter, char *ulazni_str, int n_str )
 {
-  int br_str=0;
-  char *ret_str=ulazni_str;
-  char* ptr = ulazni_str;
-                            
-    while(*ptr) 
-    { 
-        if(*ptr == delimiter) 
-        {
-           *ptr = 0;
-           if( br_str == n_str )   return ret_str;
-           else
-           {
-              br_str++;
-              ret_str = ptr + 1;
-           }
+    int br_str=0;
+    char *ret_str=ulazni_str;
+    char* ptr = ulazni_str;
+
+    while(*ptr) {
+        if(*ptr == delimiter) {
+            *ptr = 0;
+            if( br_str == n_str )   return ret_str;
+            else {
+                br_str++;
+                ret_str = ptr + 1;
+            }
         }
         ptr++;
     }