bosko lekovic
/
ETHtoCOM_10
nova verzija tcp+udp->serial com
SerialCom.cpp@26:c14d034d7459, 2020-07-10 (annotated)
- Committer:
- bosko001
- Date:
- Fri Jul 10 11:33:05 2020 +0200
- Revision:
- 26:c14d034d7459
- Parent:
- 9:893843262a1f
ispituje se na terenu
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bosko001 | 9:893843262a1f | 1 | #include "mbed.h" |
bosko001 | 9:893843262a1f | 2 | #include "clubbing.h" |
bosko001 | 9:893843262a1f | 3 | #include "doHTML.h" |
bosko001 | 9:893843262a1f | 4 | |
bosko001 | 9:893843262a1f | 5 | |
bosko001 | 9:893843262a1f | 6 | |
bosko001 | 9:893843262a1f | 7 | #define SerialTxPin PTC17 |
bosko001 | 9:893843262a1f | 8 | #define SerialRxPin PTC16 |
bosko001 | 9:893843262a1f | 9 | char scomBaud[7] = "9600", scomBaudFlash[7] = "200000"; |
bosko001 | 9:893843262a1f | 10 | |
bosko001 | 9:893843262a1f | 11 | |
bosko001 | 9:893843262a1f | 12 | /* globalni pointeri*/ |
bosko001 | 9:893843262a1f | 13 | |
bosko001 | 9:893843262a1f | 14 | |
bosko001 | 9:893843262a1f | 15 | Thread *gp_scomRxThread=NULL; |
bosko001 | 9:893843262a1f | 16 | UARTSerial *gp_scom=NULL; |
bosko001 | 9:893843262a1f | 17 | extern C_HTMLparse *gpc_html; |
bosko001 | 9:893843262a1f | 18 | |
bosko001 | 9:893843262a1f | 19 | |
bosko001 | 9:893843262a1f | 20 | int get_baud( void ) |
bosko001 | 9:893843262a1f | 21 | { |
bosko001 | 9:893843262a1f | 22 | int a = atoi(scomBaudFlash), b = atoi(scomBaud), ret; |
bosko001 | 9:893843262a1f | 23 | if( (a > 90) && (a < 100000) ) ret = a; |
bosko001 | 9:893843262a1f | 24 | else if( (b > 90) && (b < 100000) ) ret = b; |
bosko001 | 9:893843262a1f | 25 | else ret = 96/*00*/; |
bosko001 | 9:893843262a1f | 26 | printf(" baud rates = %d %d final %d\n\r", a, b, ret ); |
bosko001 | 9:893843262a1f | 27 | return ret; |
bosko001 | 9:893843262a1f | 28 | } |
bosko001 | 9:893843262a1f | 29 | // char * C_HTMLparse::get_BaudRate( void ); |
bosko001 | 9:893843262a1f | 30 | // char * C_HTMLparse::get_Parity( void ); |
bosko001 | 9:893843262a1f | 31 | // char * C_HTMLparse::get_StopBits( void ); |
bosko001 | 9:893843262a1f | 32 | // char * C_HTMLparse::getBaudRate( void ); |
bosko001 | 9:893843262a1f | 33 | |
bosko001 | 9:893843262a1f | 34 | int get_baudRates( void ) |
bosko001 | 9:893843262a1f | 35 | { |
bosko001 | 9:893843262a1f | 36 | int br = atoi( gpc_html->get_BaudRate( ) ); |
bosko001 | 9:893843262a1f | 37 | if( IN_RANGE( 1000, br, 1000000) ) return br; |
bosko001 | 9:893843262a1f | 38 | return 9600; |
bosko001 | 9:893843262a1f | 39 | } |
bosko001 | 9:893843262a1f | 40 | int get_stopBits( void ) |
bosko001 | 9:893843262a1f | 41 | { |
bosko001 | 9:893843262a1f | 42 | const char *c = gpc_html->get_StopBits( ); |
bosko001 | 9:893843262a1f | 43 | if( !strcmp( c, "2" ) ) return 2; |
bosko001 | 9:893843262a1f | 44 | else return 1; |
bosko001 | 9:893843262a1f | 45 | } |
bosko001 | 9:893843262a1f | 46 | SerialBase::Parity get_parity ( void ) |
bosko001 | 9:893843262a1f | 47 | { |
bosko001 | 9:893843262a1f | 48 | const char *c = gpc_html->get_Parity( ); |
bosko001 | 9:893843262a1f | 49 | SerialBase::Parity p = SerialBase::None; |
bosko001 | 9:893843262a1f | 50 | if( !strcmp( c, "Odd") ) return SerialBase::Odd; |
bosko001 | 9:893843262a1f | 51 | if( !strcmp( c, "Even") ) return SerialBase::None; |
bosko001 | 9:893843262a1f | 52 | return p; |
bosko001 | 9:893843262a1f | 53 | } |
bosko001 | 9:893843262a1f | 54 | int get_databits( void ) |
bosko001 | 9:893843262a1f | 55 | { |
bosko001 | 9:893843262a1f | 56 | int b = atoi( gpc_html->get_DataBits( )); |
bosko001 | 9:893843262a1f | 57 | if( IN_RANGE( 5, b, 8 ) ) return b; |
bosko001 | 9:893843262a1f | 58 | return 8; |
bosko001 | 9:893843262a1f | 59 | } |
bosko001 | 9:893843262a1f | 60 | /**************** Predaja na Serial Com ********************************/ |
bosko001 | 9:893843262a1f | 61 | |
bosko001 | 9:893843262a1f | 62 | |
bosko001 | 9:893843262a1f | 63 | void sendScom( struct UARTSerial *p_scom, char *buffer, int val) |
bosko001 | 9:893843262a1f | 64 | { |
bosko001 | 9:893843262a1f | 65 | if( p_scom ) |
bosko001 | 9:893843262a1f | 66 | { |
bosko001 | 9:893843262a1f | 67 | int num; |
bosko001 | 9:893843262a1f | 68 | if(val>0) num = p_scom->write( (const uint8_t*) buffer, val ); |
bosko001 | 9:893843262a1f | 69 | printf(" Poslan serial com paket od %d bajtova", num); |
bosko001 | 9:893843262a1f | 70 | } |
bosko001 | 9:893843262a1f | 71 | else printf("Serial com neinicijalizovan\n\r"); |
bosko001 | 9:893843262a1f | 72 | } |
bosko001 | 9:893843262a1f | 73 | |
bosko001 | 9:893843262a1f | 74 | void sendToScom( char *buffer, int val ) |
bosko001 | 9:893843262a1f | 75 | { |
bosko001 | 9:893843262a1f | 76 | sendScom( gp_scom, buffer, val); |
bosko001 | 9:893843262a1f | 77 | } |
bosko001 | 9:893843262a1f | 78 | |
bosko001 | 9:893843262a1f | 79 | /************** Prijem na serial com i slanje na UDP *********************/ |
bosko001 | 9:893843262a1f | 80 | |
bosko001 | 9:893843262a1f | 81 | void scomrx_fun( void ) |
bosko001 | 9:893843262a1f | 82 | { |
bosko001 | 9:893843262a1f | 83 | |
bosko001 | 9:893843262a1f | 84 | char readbuff[1500]; |
bosko001 | 9:893843262a1f | 85 | int totno=0; |
bosko001 | 9:893843262a1f | 86 | int noreaded=0; |
bosko001 | 9:893843262a1f | 87 | // if(gp_scom) delete gp_scom; |
bosko001 | 9:893843262a1f | 88 | gp_scom = new UARTSerial(SerialTxPin,SerialRxPin,/*get_baud()*/ get_baudRates( ) ); //38400 za KSS |
bosko001 | 9:893843262a1f | 89 | gp_scom->set_format( get_databits( ), get_parity(), get_stopBits( ) ); |
bosko001 | 9:893843262a1f | 90 | printf("Rx serial com initialised\n\r"); |
bosko001 | 9:893843262a1f | 91 | |
bosko001 | 9:893843262a1f | 92 | while( !(ThisThread::flags_get( ) & 1) ) |
bosko001 | 9:893843262a1f | 93 | { |
bosko001 | 9:893843262a1f | 94 | if( gp_scom->readable() ) |
bosko001 | 9:893843262a1f | 95 | { |
bosko001 | 9:893843262a1f | 96 | noreaded = gp_scom->read( (uint8_t *)(readbuff+totno), 1500); |
bosko001 | 9:893843262a1f | 97 | totno += noreaded; |
bosko001 | 9:893843262a1f | 98 | wait(0.01); |
bosko001 | 9:893843262a1f | 99 | } |
bosko001 | 9:893843262a1f | 100 | else if(totno) |
bosko001 | 9:893843262a1f | 101 | { |
bosko001 | 9:893843262a1f | 102 | printf("PRIJEM na rs232 com totno = %d \n\r", totno); |
bosko001 | 9:893843262a1f | 103 | // if(readbuff[0] == 'q') {extern Thread udpBroadcast_thread; udpBroadcast_thread.terminate();} |
bosko001 | 9:893843262a1f | 104 | |
bosko001 | 9:893843262a1f | 105 | // if(readbuff[0] == 'c'){ extern void udpBroadcast_fun( void );Thread *th = new Thread; th->start( udpBroadcast_fun);} |
bosko001 | 9:893843262a1f | 106 | // if(readbuff[0] == 'r'){ extern void inic_1(void); extern PFV pfv; pfv = inic_1; } |
bosko001 | 9:893843262a1f | 107 | // if(readbuff[0] == 'q'){ if(gp_udpSocket)gp_udpSocket->close();gp_udpSocket=NULL; } |
bosko001 | 9:893843262a1f | 108 | // sendUdp( ps_ear, (char *)readbuff, totno); |
bosko001 | 9:893843262a1f | 109 | extern void sendToUdp( char *buffer, int val ); sendToUdp( readbuff, totno ); |
bosko001 | 9:893843262a1f | 110 | totno = 0; |
bosko001 | 9:893843262a1f | 111 | } |
bosko001 | 9:893843262a1f | 112 | } |
bosko001 | 9:893843262a1f | 113 | if(gp_scom) {delete gp_scom; gp_scom = NULL;} |
bosko001 | 9:893843262a1f | 114 | printf(" Ugasen serial com Thread\n\r"); |
bosko001 | 9:893843262a1f | 115 | } |
bosko001 | 9:893843262a1f | 116 | |
bosko001 | 9:893843262a1f | 117 | |
bosko001 | 9:893843262a1f | 118 | void scomRxThreadCancel( void ) |
bosko001 | 9:893843262a1f | 119 | { |
bosko001 | 9:893843262a1f | 120 | /* gasenje serial com thread-a */ |
bosko001 | 9:893843262a1f | 121 | int st = gp_scomRxThread->get_state(); |
bosko001 | 9:893843262a1f | 122 | if( st && st<16 )gp_scomRxThread->flags_set(1); |
bosko001 | 9:893843262a1f | 123 | gp_scomRxThread->join(); |
bosko001 | 9:893843262a1f | 124 | printf("ScomRx Thread upravo zavrsio\n\r"); |
bosko001 | 9:893843262a1f | 125 | if(gp_scomRxThread) { delete gp_scomRxThread; gp_scomRxThread = NULL;} |
bosko001 | 9:893843262a1f | 126 | } |
bosko001 | 9:893843262a1f | 127 | |
bosko001 | 9:893843262a1f | 128 | void scomRxThreadRiseUp( void ) |
bosko001 | 9:893843262a1f | 129 | { |
bosko001 | 9:893843262a1f | 130 | /* paljenje serial com thread-a */ |
bosko001 | 9:893843262a1f | 131 | gp_scomRxThread = new Thread(scomrx_fun); |
bosko001 | 9:893843262a1f | 132 | } |
bosko001 | 9:893843262a1f | 133 | |
bosko001 | 9:893843262a1f | 134 | void scomRxThreadRestart( void ) |
bosko001 | 9:893843262a1f | 135 | { |
bosko001 | 9:893843262a1f | 136 | scomRxThreadCancel( ); |
bosko001 | 9:893843262a1f | 137 | scomRxThreadRiseUp( ); |
bosko001 | 9:893843262a1f | 138 | } |