bosko lekovic
/
EthToCom_11
nova proba
Serial/SerialCom.cpp@27:0772451fb880, 2021-01-17 (annotated)
- Committer:
- Bosko Lekovic
- Date:
- Sun Jan 17 23:03:43 2021 +0100
- Revision:
- 27:0772451fb880
initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Bosko Lekovic |
27:0772451fb880 | 1 | #include "mbed.h" |
Bosko Lekovic |
27:0772451fb880 | 2 | #include "clubbing.h" |
Bosko Lekovic |
27:0772451fb880 | 3 | #include "doHTML.h" |
Bosko Lekovic |
27:0772451fb880 | 4 | |
Bosko Lekovic |
27:0772451fb880 | 5 | |
Bosko Lekovic |
27:0772451fb880 | 6 | |
Bosko Lekovic |
27:0772451fb880 | 7 | #define SerialTxPin PTC17 |
Bosko Lekovic |
27:0772451fb880 | 8 | #define SerialRxPin PTC16 |
Bosko Lekovic |
27:0772451fb880 | 9 | |
Bosko Lekovic |
27:0772451fb880 | 10 | |
Bosko Lekovic |
27:0772451fb880 | 11 | /* globalni pointeri*/ |
Bosko Lekovic |
27:0772451fb880 | 12 | extern C_HTMLparse *gpc_html; |
Bosko Lekovic |
27:0772451fb880 | 13 | extern volatile int tcpServerBlock; |
Bosko Lekovic |
27:0772451fb880 | 14 | |
Bosko Lekovic |
27:0772451fb880 | 15 | UARTSerial * gp_scom;// = new UARTSerial(SerialTxPin,SerialRxPin/* , get_baudRates( )*/ ); //38400 za KSS |
Bosko Lekovic |
27:0772451fb880 | 16 | |
Bosko Lekovic |
27:0772451fb880 | 17 | static Thread *gp_scomRxThread=NULL; |
Bosko Lekovic |
27:0772451fb880 | 18 | int serial_status = 0; |
Bosko Lekovic |
27:0772451fb880 | 19 | int totno=0; |
Bosko Lekovic |
27:0772451fb880 | 20 | int noreaded=0; |
Bosko Lekovic |
27:0772451fb880 | 21 | |
Bosko Lekovic |
27:0772451fb880 | 22 | void print_serial( void ) |
Bosko Lekovic |
27:0772451fb880 | 23 | { |
Bosko Lekovic |
27:0772451fb880 | 24 | printf( " serial: status=%d totno=%d noreaded=%d \n\r", serial_status, totno, noreaded); |
Bosko Lekovic |
27:0772451fb880 | 25 | } |
Bosko Lekovic |
27:0772451fb880 | 26 | |
Bosko Lekovic |
27:0772451fb880 | 27 | |
Bosko Lekovic |
27:0772451fb880 | 28 | // char * C_HTMLparse::get_BaudRate( void ); |
Bosko Lekovic |
27:0772451fb880 | 29 | // char * C_HTMLparse::get_Parity( void ); |
Bosko Lekovic |
27:0772451fb880 | 30 | // char * C_HTMLparse::get_StopBits( void ); |
Bosko Lekovic |
27:0772451fb880 | 31 | // char * C_HTMLparse::getBaudRate( void ); |
Bosko Lekovic |
27:0772451fb880 | 32 | |
Bosko Lekovic |
27:0772451fb880 | 33 | int get_baudRates( void ) |
Bosko Lekovic |
27:0772451fb880 | 34 | { |
Bosko Lekovic |
27:0772451fb880 | 35 | int br = atoi( gpc_html->get_BaudRate( ) ); |
Bosko Lekovic |
27:0772451fb880 | 36 | if( IN_RANGE( 1000, br, 1000000) ) return br; |
Bosko Lekovic |
27:0772451fb880 | 37 | return 9600; |
Bosko Lekovic |
27:0772451fb880 | 38 | } |
Bosko Lekovic |
27:0772451fb880 | 39 | int get_stopBits( void ) |
Bosko Lekovic |
27:0772451fb880 | 40 | { |
Bosko Lekovic |
27:0772451fb880 | 41 | const char *c = gpc_html->get_StopBits( ); |
Bosko Lekovic |
27:0772451fb880 | 42 | if( !strcmp( c, "2" ) ) return 2; |
Bosko Lekovic |
27:0772451fb880 | 43 | else return 1; |
Bosko Lekovic |
27:0772451fb880 | 44 | } |
Bosko Lekovic |
27:0772451fb880 | 45 | SerialBase::Parity get_parity ( void ) |
Bosko Lekovic |
27:0772451fb880 | 46 | { |
Bosko Lekovic |
27:0772451fb880 | 47 | const char *c = gpc_html->get_Parity( ); |
Bosko Lekovic |
27:0772451fb880 | 48 | SerialBase::Parity p = SerialBase::None; |
Bosko Lekovic |
27:0772451fb880 | 49 | if( !strcmp( c, "Odd") ) return SerialBase::Odd; |
Bosko Lekovic |
27:0772451fb880 | 50 | if( !strcmp( c, "Even") ) return SerialBase::None; |
Bosko Lekovic |
27:0772451fb880 | 51 | return p; |
Bosko Lekovic |
27:0772451fb880 | 52 | } |
Bosko Lekovic |
27:0772451fb880 | 53 | int get_databits( void ) |
Bosko Lekovic |
27:0772451fb880 | 54 | { |
Bosko Lekovic |
27:0772451fb880 | 55 | int b = atoi( gpc_html->get_DataBits( )); |
Bosko Lekovic |
27:0772451fb880 | 56 | if( IN_RANGE( 5, b, 8 ) ) return b; |
Bosko Lekovic |
27:0772451fb880 | 57 | return 8; |
Bosko Lekovic |
27:0772451fb880 | 58 | } |
Bosko Lekovic |
27:0772451fb880 | 59 | |
Bosko Lekovic |
27:0772451fb880 | 60 | /******************** INICIJALIZACIJA I START SERIJALA - start se moze pozvati samo jednom ***********************/ |
Bosko Lekovic |
27:0772451fb880 | 61 | |
Bosko Lekovic |
27:0772451fb880 | 62 | |
Bosko Lekovic |
27:0772451fb880 | 63 | void init_SerialCom( void ) |
Bosko Lekovic |
27:0772451fb880 | 64 | { |
Bosko Lekovic |
27:0772451fb880 | 65 | gp_scom->set_format( get_databits( ), get_parity(), get_stopBits( ) ); |
Bosko Lekovic |
27:0772451fb880 | 66 | gp_scom->set_baud( get_baudRates()); |
Bosko Lekovic |
27:0772451fb880 | 67 | } |
Bosko Lekovic |
27:0772451fb880 | 68 | |
Bosko Lekovic |
27:0772451fb880 | 69 | void scomrx_fun( void ); |
Bosko Lekovic |
27:0772451fb880 | 70 | void start_SerialCom( void ) |
Bosko Lekovic |
27:0772451fb880 | 71 | { |
Bosko Lekovic |
27:0772451fb880 | 72 | |
Bosko Lekovic |
27:0772451fb880 | 73 | if(gp_scomRxThread==NULL) |
Bosko Lekovic |
27:0772451fb880 | 74 | { |
Bosko Lekovic |
27:0772451fb880 | 75 | // printf("inicijalizacija serijal thread-a\n\r");fflush(stdout); |
Bosko Lekovic |
27:0772451fb880 | 76 | gp_scomRxThread = new Thread; |
Bosko Lekovic |
27:0772451fb880 | 77 | gp_scomRxThread->start(scomrx_fun); |
Bosko Lekovic |
27:0772451fb880 | 78 | // printf(" Paljenje serial com RxThread = %d \n\r", gp_scomRxThread); |
Bosko Lekovic |
27:0772451fb880 | 79 | |
Bosko Lekovic |
27:0772451fb880 | 80 | } |
Bosko Lekovic |
27:0772451fb880 | 81 | else printf("@@@@@@@@@ ScomThread vec postoji, ne moze se setovati novi\n\r"); |
Bosko Lekovic |
27:0772451fb880 | 82 | |
Bosko Lekovic |
27:0772451fb880 | 83 | } |
Bosko Lekovic |
27:0772451fb880 | 84 | |
Bosko Lekovic |
27:0772451fb880 | 85 | /**************** Predaja na Serial Com ********************************/ |
Bosko Lekovic |
27:0772451fb880 | 86 | |
Bosko Lekovic |
27:0772451fb880 | 87 | |
Bosko Lekovic |
27:0772451fb880 | 88 | |
Bosko Lekovic |
27:0772451fb880 | 89 | |
Bosko Lekovic |
27:0772451fb880 | 90 | |
Bosko Lekovic |
27:0772451fb880 | 91 | void sendScom( struct UARTSerial *p_scom, char *buffer, int val) |
Bosko Lekovic |
27:0772451fb880 | 92 | { |
Bosko Lekovic |
27:0772451fb880 | 93 | if( !tcpServerBlock) |
Bosko Lekovic |
27:0772451fb880 | 94 | { |
Bosko Lekovic |
27:0772451fb880 | 95 | if( p_scom ) |
Bosko Lekovic |
27:0772451fb880 | 96 | { |
Bosko Lekovic |
27:0772451fb880 | 97 | int num; |
Bosko Lekovic |
27:0772451fb880 | 98 | if(val>0) num = p_scom->write( (const uint8_t*) buffer, val ); |
Bosko Lekovic |
27:0772451fb880 | 99 | printf("SComTx: Poslan serial com paket od %d bajtova\n\r", num); |
Bosko Lekovic |
27:0772451fb880 | 100 | } |
Bosko Lekovic |
27:0772451fb880 | 101 | else printf("SComTx: Serial com neinicijalizovan\n\r"); |
Bosko Lekovic |
27:0772451fb880 | 102 | } |
Bosko Lekovic |
27:0772451fb880 | 103 | } |
Bosko Lekovic |
27:0772451fb880 | 104 | |
Bosko Lekovic |
27:0772451fb880 | 105 | void sendToScom( char *buffer, int val ) |
Bosko Lekovic |
27:0772451fb880 | 106 | { |
Bosko Lekovic |
27:0772451fb880 | 107 | sendScom( gp_scom, buffer, val); |
Bosko Lekovic |
27:0772451fb880 | 108 | } |
Bosko Lekovic |
27:0772451fb880 | 109 | |
Bosko Lekovic |
27:0772451fb880 | 110 | /************** Prijem na serial com i slanje na UDP *********************/ |
Bosko Lekovic |
27:0772451fb880 | 111 | static volatile int prc =0; |
Bosko Lekovic |
27:0772451fb880 | 112 | void scomrx_fun( void ) |
Bosko Lekovic |
27:0772451fb880 | 113 | { |
Bosko Lekovic |
27:0772451fb880 | 114 | |
Bosko Lekovic |
27:0772451fb880 | 115 | char readbuff[1500]; |
Bosko Lekovic |
27:0772451fb880 | 116 | // int totno=0; |
Bosko Lekovic |
27:0772451fb880 | 117 | // int noreaded=0; |
Bosko Lekovic |
27:0772451fb880 | 118 | gp_scom = new UARTSerial(SerialTxPin,SerialRxPin,/*get_baud()*/ get_baudRates( ) ); //38400 za KSS |
Bosko Lekovic |
27:0772451fb880 | 119 | if( gp_scom ) |
Bosko Lekovic |
27:0772451fb880 | 120 | { |
Bosko Lekovic |
27:0772451fb880 | 121 | printf("inicijalizacija serijal comm-a\n\r");fflush(stdout); |
Bosko Lekovic |
27:0772451fb880 | 122 | init_SerialCom( ); |
Bosko Lekovic |
27:0772451fb880 | 123 | // printf("SComRxThread: Rx serial com initialised gp_scom=%d\n\r", gp_scom); |
Bosko Lekovic |
27:0772451fb880 | 124 | |
Bosko Lekovic |
27:0772451fb880 | 125 | int k =0; |
Bosko Lekovic |
27:0772451fb880 | 126 | while( true ) |
Bosko Lekovic |
27:0772451fb880 | 127 | { |
Bosko Lekovic |
27:0772451fb880 | 128 | |
Bosko Lekovic |
27:0772451fb880 | 129 | if( /* !tcpServerBlock &&*/ gp_scom->readable() /* && ((noreaded = gp_scom->read( (uint8_t *)(readbuff+totno), 1500-totno))>0)*/ ) |
Bosko Lekovic |
27:0772451fb880 | 130 | { |
Bosko Lekovic |
27:0772451fb880 | 131 | |
Bosko Lekovic |
27:0772451fb880 | 132 | noreaded = gp_scom->read( (uint8_t *)(readbuff+totno), 1500-totno); |
Bosko Lekovic |
27:0772451fb880 | 133 | totno += noreaded; |
Bosko Lekovic |
27:0772451fb880 | 134 | k++; |
Bosko Lekovic |
27:0772451fb880 | 135 | ThisThread::yield();//wait(0.001);*/ thread_sleep_for(1); |
Bosko Lekovic |
27:0772451fb880 | 136 | } |
Bosko Lekovic |
27:0772451fb880 | 137 | else if(totno) |
Bosko Lekovic |
27:0772451fb880 | 138 | { |
Bosko Lekovic |
27:0772451fb880 | 139 | // printf(" k=%d \n\r", k); |
Bosko Lekovic |
27:0772451fb880 | 140 | extern void sendToUdp( char *buffer, int val ); sendToUdp( readbuff, totno ); |
Bosko Lekovic |
27:0772451fb880 | 141 | extern void sendToTcp( char *buffer, int val ); sendToTcp( readbuff, totno ); |
Bosko Lekovic |
27:0772451fb880 | 142 | extern void sentOnTcpServers( char *buffer, int val ); sentOnTcpServers(readbuff, totno ); |
Bosko Lekovic |
27:0772451fb880 | 143 | k = 0; |
Bosko Lekovic |
27:0772451fb880 | 144 | totno = 0; |
Bosko Lekovic |
27:0772451fb880 | 145 | } |
Bosko Lekovic |
27:0772451fb880 | 146 | ThisThread::yield();//wait(0.001); |
Bosko Lekovic |
27:0772451fb880 | 147 | }// while 1 |
Bosko Lekovic |
27:0772451fb880 | 148 | } |
Bosko Lekovic |
27:0772451fb880 | 149 | else printf("problem inicijalizacije socketa\n\r"); |
Bosko Lekovic |
27:0772451fb880 | 150 | terminate(); |
Bosko Lekovic |
27:0772451fb880 | 151 | } |
Bosko Lekovic |
27:0772451fb880 | 152 |