p1

Files at this revision

API Documentation at this revision

Comitter:
bosko001
Date:
Thu Jun 18 15:23:39 2020 +0000
Parent:
6:19e7658575fe
Commit message:
p1

Changed in this revision

PGSdisplay.cpp Show annotated file Show diff for this revision Revisions of this file
PGSdisplay.h Show annotated file Show diff for this revision Revisions of this file
ParkareClient.cpp Show annotated file Show diff for this revision Revisions of this file
ParkareClient.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/PGSdisplay.cpp	Mon Feb 24 17:14:44 2020 +0000
+++ b/PGSdisplay.cpp	Thu Jun 18 15:23:39 2020 +0000
@@ -9,49 +9,82 @@
 #define DISP_ADR   40
 
 DigitalOut tast(PTB19,0);
+DigitalOut proba(PTB18,0);
 ////RawSerial rs485(PTC17,PTC16);
-RawSerial rs485(PTD3,PTD2);
+//Serial rs485(PTD3,PTD2);
+UARTSerial rs485(PTD3,PTD2);
+DigitalOut led(LED2,1);
+
+
+void printT(char *s, char *tel, int duzina);
+
+char urxbuf[100];
+int urb=0;
 
 
-void ev(int)
+void rec( void )
 {
-    tast=0;
+   int i = 0, j = 0;
+    while(true)
+    {
+proba = 1;
+            int k;
+     //     if( rs485.readable() ) 
+          {
+              urb = rs485.read((uint8_t*)&urxbuf[0],100/*,NULL,NULL*/);
+ 
+//             printf("k=%x\n\r",k);
+                printT("prijem", urxbuf, urb);
+            }
+proba = 0;
+        thread_sleep_for(1);
+    }
 }
 
-uint8_t rs[100];
-void init_disp( void)
+
+int dispGetResponce( void )
 {
-    rs485.format(8,RawSerial::Even,1);  
-    while(rs485.readable())
-    {
-        rs485.read(rs,1,NULL);
-    }
-    putOnDisp("elcom", 3);
-    int i =0;
-    //while(rs485.readable()==0);
-    wait(0.5);
-    if(rs485.readable())
-    {
-        
-        rs485.read(rs+i,1,NULL);
-        i++;
-    }
-    printf("ODGOVOR: %X - %d\n\r",rs[0],i);
-    //int i=0;
-//    
-//    printf("%d\n\r",rs485.readable());
-//    if(rs485.readable())
-//    {
-//        printf("\n\rRECEIVE: ");   
-//        while(rs485.readable())
-//        {
-//            rs[i++] = rs485.getc();
-//        }
-//        printf("%X:  RECEIVED %d: \n\r",rs[0], i);   
-//    }
+   urb = 0;
+   thread_sleep_for(100);
+   if(urb && (urxbuf[0] == 0xe5))      return 1;
+   return 0;
 }
 
 unsigned char buf_tel[100] = {0x71,0x00,0x0D,0x71,0x43,DISP_ADR,0x1B,0x4A,0x01,0x01,  /*0x1b,0x45,0x1,*/  0x1B,0x43,1,0x33,0xD5,0x16};
+
+void init_disp( void)
+{
+    rs485.set_format(8,SerialBase::Even,1);
+//   rs485.attach( tast_off, Serial::TxIrq );  
+//   rs485.attach( rc, Serial::RxIrq );  
+
+    Thread *Ttast = new Thread;
+    extern void tast_fun(void);
+    Ttast->start( (tast_fun ));
+
+
+    Thread *Trec = new Thread;
+    Trec->start( rec );
+
+    
+    while( true )
+    {      
+     extern void putOnDispNo( int i, char boja);
+     static int i=1;
+       buf_tel[5] = i;
+        putOnDispNo(i, 1);
+
+        char *r;
+        if( dispGetResponce( ) ) { printf( "adresa je %d\n\r", i); break;}
+        i++;
+        if(i==0) i++;
+        else if(i == 254) {i = 1; break;}
+    }
+
+}
+
+
+
 #define NO_BEFORE_DATA 13
 #define NO_WITH_DATA (NO_BEFORE_DATA + strlen(s))
 
@@ -71,10 +104,15 @@
     buf_tel[1]=ubb>>8;
     buf_tel[2]=ubb;
 
+    
+//      Thread *t = new Thread;
+//    extern void tast_fun(Thread* t);
+//    t->start( callback(tast_fun, t ));
+//    thread_sleep_for(1);  
 
-    tast=1;
-    rs485.write(buf_tel, NO_WITH_DATA+2+2,ev);
-    
+    tast = 1;
+    rs485.write((const void*)buf_tel, (size_t)(NO_WITH_DATA+2+2));
+//     for(int i=0; i<  NO_WITH_DATA+2+2; i++ ) rs485.putc(buf_tel[i]);
 }
 
 void putOnDispNo(int broj, char boja)
@@ -101,6 +139,39 @@
 }
 
 
+
+int rs485tastovanje( int tast )
+{ 
+static int start = 0;
+int ret = -1;
+      if( tast == 0 ) { start = 0;  }
+      else
+      { 
+        if(  start == 0)  { start = 1; ret = 0;  thread_sleep_for(1);} 
+        else 
+        {
+          volatile char c = UART2->S1;
+           if ( c & UART_S1_TC_MASK)  {ret = 1; start = 0; }
+        }        
+      }
+  return ret;
+}
+
+
+void tast_fun( /*Thread *t*/void )
+{
+    while( true )
+   {
+     extern int rs485tastovanje( int );
+     int ret;
+      if( (ret = rs485tastovanje( tast )) == 1 ) { tast = 0; }
+    }   
+//   kos = t;
+//  printf("___istastovanje %d  tast = %d   thr=%d \n\r", brb, (int)tast, t);
+//  fflush(stdout);
+}
+
+
 char * extract_string( char delimiter, char *ulazni_str, int n_str )
 {
     int br_str=0;
@@ -122,3 +193,14 @@
     return NULL;
 }
 
+
+void printT(char *s, char *tel, int duzina){
+    int i=0;
+    printf("%s duzina = %d: ",s,duzina);
+    while(i<duzina){   
+        printf("%02X ", tel[i]);
+        i++;
+    }
+    printf("\n\r");
+    fflush(stdout);
+}
--- a/PGSdisplay.h	Mon Feb 24 17:14:44 2020 +0000
+++ b/PGSdisplay.h	Thu Jun 18 15:23:39 2020 +0000
@@ -5,6 +5,7 @@
 void init_disp( void);
 void putOnDisp( char *s, char boja);
 void putOnDispNo(int broj, char boja);
+int dispGetResponce( void );
 char * extract_string( char delimiter, char *ulazni_str, int n_str );
 
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ParkareClient.cpp	Thu Jun 18 15:23:39 2020 +0000
@@ -0,0 +1,77 @@
+
+#include "ParkareClient.h"
+#include "PGSdisplay.h"
+
+
+
+//unsigned char msg_request_message[] = "{5|1|1;2}";
+//unsigned char msg_catalogue_request_message[] = "{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);
+}
+
+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);
+    if (atoi(strno) < 0)
+        putOnDisp("0", boja);
+    else
+        putOnDisp((char*)strno, boja);
+}
+
+
+
+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,strlen(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 );
+                    s->response = dispGetResponce(  );
+
+                }
+            }
+        }
+
+        printf("requestMessage - zatvaranje konekcije\n\r");
+        tcpSocket.close();
+    }
+
+}
+
+
+
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ParkareClient.h	Thu Jun 18 15:23:39 2020 +0000
@@ -0,0 +1,24 @@
+#ifndef PARKARECLIENT_H
+#define PARKARECLIENT_H
+
+#include "mbed.h"
+#include "EthernetInterface.h"
+
+struct s_rm {
+    EthernetInterface *e;
+    SocketAddress *sa;
+    char *tx_msg;
+    void (*fun_rec)(char*);
+    int response;
+};
+
+
+void requestMessageThread(struct s_rm *s);
+
+void crm_fun( char *rx);
+void srm_fun( char *rx );
+
+
+
+
+#endif
\ No newline at end of file
--- a/main.cpp	Mon Feb 24 17:14:44 2020 +0000
+++ b/main.cpp	Thu Jun 18 15:23:39 2020 +0000
@@ -13,105 +13,89 @@
 #include "TCPSocket.h"
 
 #include "PGSdisplay.h"
+#include "ParkareClient.h"
+
+
 
 
-//
-//void putOnDispNo(int broj, char boja);
-//void putOnDisp(char *s, char boja);
-void requestMessageThread(struct s_rm *s);
-//void slanjefun();
+char MyIPaddress[]= "192.168.2.11";
+char MyNETmask  []= "255.255.248.0";
+char MyGWaddress[]= "192.168.1.1";
 
-#define IP_ADDR   "192.168.2.11"
-#define NET_MASK  "255.255.248.0"
-#define GATW_ADDR "192.168.1.1"
-#define IP_ADDR_R "192.168.1.9"
+char conf_server_ip[] = "192.168.2.254";
+int conf_server_port = 12197;
 
 
+Thread *threadGarColl = NULL;
+
 
 EthernetInterface eth;
 nsapi_error_t err;
+volatile int ethinic_flag = 0;
+
+void theth_fun( void )
+{
+ 
+//   while( true )
+   {
+//        if(ethinic_flag==0)
+        {
+            printf(" setovanje ethernet mreze \n\r");
+            if( (err = eth.set_network(MyIPaddress,MyNETmask,MyGWaddress))== NSAPI_ERROR_OK ) 
+            {
+                printf(" uspostavljanje konekcije \n\r");
+                if( (err = eth.connect())  == NSAPI_ERROR_OK )  
+                {
+                
+                    printf( "MAC adresa: %s\n\r", eth.get_mac_address() );
+                    printf( "IP adresa:  %s\n\r", eth.get_ip_address() );
+                    printf( "net mask:   %s\n\r", eth.get_netmask() );
+                    printf( "GW adresa:  %s\n\r", eth.get_gateway() );
+        ethinic_flag = 1;
+                }
+            }
+        }
+        fflush( stdout);
+//        thread_sleep_for( 1000 );
+    }
+}
+
+
 
 char * extract_string( char delimiter, char *ulazni_str, int n_str );
 void putOnDisp( char *s, char boja);
 
-
-
 void tcpThread_fun( EthernetInterface *e);
 void requestMessage( EthernetInterface *e);
-//
-//DigitalOut tast(PTB19,0);
-//////RawSerial rs485(PTC17,PTC16);
-//RawSerial rs485(PTD3,PTD2);
 
-struct s_rm {
-    EthernetInterface *e;
-    SocketAddress *sa;
-    char *tx_msg;
-    void (*fun_rec)(char*);
-};
-
-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);
-}
-
-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);
-    if (atoi(strno) < 0)
-        putOnDisp("0", boja);
-    else
-        putOnDisp((char*)strno, boja);
-}
-
-
-
+DigitalOut ledc(LED3,1);
 
 int main(void)
 {
+printf("\n\r----------------Pocetak------------ \n\r"); fflush(stdout);
 
+Thread ThEth;
+    ThEth.start( theth_fun );
 
     init_disp( );
+    putOnDisp("elcom",3);
+
+    ThEth.join( );
+    printf(" Thread ethernet = %d \n\r", (int)&ThEth );
+    
+    SocketAddress sa(conf_server_ip, conf_server_port);
+    printf("socket inicijalizovan \n\r"); fflush(stdout);
+
+    putOnDisp("flash",2);
+
+extern char crm_req[];
+extern char srm_req[];
+    struct s_rm s_crm = { &eth, &sa, crm_req, crm_fun, 0 };
+    struct s_rm s_srm = { &eth, &sa, srm_req, srm_fun, 0 };
 
 
-    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("Parkare server zahtev za katalog \n\r"); fflush(stdout);
 
-    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 };
-
-
-    putOnDisp("flash", 2);
 
     Thread CRMThread;
     CRMThread.start( callback( requestMessageThread, &s_crm ));
@@ -119,65 +103,37 @@
     int i=0;
 //        thread_sleep_for(15000);
 
+printf("ispred while-a\n\r"); 
+fflush(stdout);
+
     while(true) {
+//        ledc = !ledc;
+if( threadGarColl != NULL ) {delete threadGarColl; threadGarColl = NULL; }
+
         thread_sleep_for(5000);
 
+        printf("Parkare server zahtev za broj \n\r"); fflush(stdout);
         Thread SRMThread;
+        s_srm.response = 0;
         SRMThread.start( callback( requestMessageThread, &s_srm ));
         SRMThread.join();
-        printf("main %d\n\r",i++);
+        printf("PGSdisplay status response %d\n\r",s_srm.response);
     }
 //   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 sb[1000]="HTTP/1.1 200 OK\r\nConnection: close\r\nContent-Type: text/html\r\n\r\n";
-
-
-
-unsigned char txbuf[100]="test konekcije";
-unsigned char rxbuf[100];
-
-
-char conf_server_ip[] = "192.168.1.26";
-int conf_server_port = 12197;
-unsigned char msg_request_message[] = "{5|1|1;2}";
-unsigned char msg_catalogue_request_message[] = "{1|1}";
-
+//
+//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";
+//
+//
+//
+//unsigned char txbuf[100]="test konekcije";
+//unsigned char rxbuf[100];
 
 
-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,strlen(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();
-    }
-
-}
 
 //
 //enum nsapi_error {