need help in rs232 to udp and rs232

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
elmofty
Posts: 20
Joined: 19 Feb 2012 00:09

need help in rs232 to udp and rs232

#1 Post by elmofty » 12 Jan 2013 16:07

hello to all
i need help in this project
i try to send data using rs232 and recive the data from rs232 and send the darta to another macine using upd ethernet
i can got the data which sending from re232 can recive from rs232 but i cannot get the same data on udp on another machine
any one can help me
thanks
this is my coode and the data i try to send is 9 bytes i can recive the same data i send using rs232 but cannot recive the same data on upd need help
thanks

Code: Select all

#include <built_in.h>

/**************************************************************************************************
*
**************************************************************************************************/

// duplex config flags
#define Spi_Ethernet_HALFDUPLEX     0x00  // half duplex
#define Spi_Ethernet_FULLDUPLEX     0x01  // full duplex

// mE ehternet NIC pinout
sfr sbit SPI_Ethernet_Rst at LATE1_bit;
sfr sbit SPI_Ethernet_CS  at LATE0_bit;
sfr sbit SPI_Ethernet_Rst_Direction at TRISE1_bit;
sfr sbit SPI_Ethernet_CS_Direction  at TRISE0_bit;

// TCP flags
typedef struct {
  unsigned canCloseTCP: 1;  // flag which closes TCP socket (not relevant to UDP)
  unsigned isBroadcast: 1;  // flag which denotes that the IP package has been received via subnet broadcast address (not used for PIC16 family)
} TEthPktFlags;

// RAM variables
/***********************************
 * RAM variables
 */
char myMacAddr[6] = {0x00, 0x14, 0xA5, 0x76, 0x19, 0x3d} ;   // my MAC address
char myIpAddr[4]  = {0, 0, 0,0};                         // my IP address
char gwIpAddr[4] ;          // gateway (router) IP address
char ipMask[4] ;            // network mask (for example : 255.255.255.0)
char dnsIpAddr[4] ;         // DNS server IP address
//unsigned char dnsIpAddr[4] ;         // DNS server IP address
char server_Addr[6] = {192, 168, 1, 9};  // Remote machine IP address

 char uart_rd;
char uart_requst;
char uart_answer;

/*******************************************
 * functions
 */

/*
 * put the constant string pointed to by s to the ENC transmit buffer.
 */
/*unsigned int    putConstString(const char *s)
        {
        unsigned int ctr = 0 ;

        while(*s)
                {
                Spi_Ethernet_putByte(*s++) ;
                ctr++ ;
                }
        return(ctr) ;
        }*/
/*
 * it will be much faster to use library Spi_Ethernet_putConstString routine
 * instead of putConstString routine above. However, the code will be a little
 * bit bigger. User should choose between size and speed and pick the implementation that
 * suites him best. If you choose to go with the putConstString definition above
 * the #define line below should be commented out.
 *
 */
#define putConstString  SPI_Ethernet_putConstString

/*
 * put the string pointed to by s to the ENC transmit buffer
 */
/*unsigned int    putString(char *s)
        {
        unsigned int ctr = 0 ;

        while(*s)
                {
                Spi_Ethernet_putByte(*s++) ;

                ctr++ ;
                }
        return(ctr) ;
        }*/
/*
 * it will be much faster to use library Spi_Ethernet_putString routine
 * instead of putString routine above. However, the code will be a little
 * bit bigger. User should choose between size and speed and pick the implementation that
 * suites him best. If you choose to go with the putString definition above
 * the #define line below should be commented out.
 *
 */
#define putString  SPI_Ethernet_putString

/*
 * this function is called by the library
 * the user accesses to the HTTP request by successive calls to Spi_Ethernet_getByte()
 * the user puts data in the transmit buffer by successive calls to Spi_Ethernet_putByte()
 * the function must return the length in bytes of the HTTP reply, or 0 if nothing to transmit
 *
 * if you don't need to reply to HTTP requests,
 * just define this function with a return(0) as single statement
 *
 */
unsigned int SPI_Ethernet_UserTCP(unsigned char *remoteHost, unsigned int remotePort,
                                  unsigned int localPort, unsigned int reqLength, TEthPktFlags *flags) {
  return 0;                                 // return to the library with the number of bytes to transmit
}



/*
 * this function is called by the library
 * the user accesses to the UDP request by successive calls to Spi_Ethernet_getByte()
 * the user puts data in the transmit buffer by successive calls to Spi_Ethernet_putByte()
 * the function must return the length in bytes of the UDP reply, or 0 if nothing to transmit
 *
 * if you don't need to reply to UDP requests,
 * just define this function with a return(0) as single statement
 *
 */
unsigned int  SPI_Ethernet_UserUDP(unsigned char *remoteHost, unsigned int remotePort,
                                   unsigned int destPort, unsigned int reqLength, TEthPktFlags *flags) {
   return 0;              // back to the library with the length of the UDP reply
}

/**************************************************************************************************
* MAIN PROGRAM
**************************************************************************************************/
void main()
{
unsigned char response;
  ANSELB = 0;                        // Configure PORTB pins as digital.
  ANSELC = 0;                        // Configure PORTC pins as digital
  ANSELE = 0;                        // Configure PORTE pins as digital
  SLRCON = 0;                        // Configure all PORTS at the standard Slew

  PORTB = 0;
  TRISB = 0xff;                      // set PORTB as input for buttons
  PORTD = 0;
  TRISD = 0;                         // set PORTD as output

  /*
   * starts ENC28J60 with :
   * reset bit on RE1
   * CS bit on RE0
   * my MAC & IP address
   * full duplex
   */
//  SPI1_Init();
//  SPI_Ethernet_Init(macAddr, ipAddr, Spi_Ethernet_FULLDUPLEX) ;

/*
         * init Ethernet
         */
         SPI1_Init();
         SPI_Ethernet_Init(myMacAddr, myIpAddr,1);
         Delay_ms(2000);
         while(response != 1)
                             {
                                 response =SPI_Ethernet_initDHCP(10);
                             }
                          memcpy(myIpAddr, Spi_Ethernet_getIpAddress(),4) ;
                          memcpy(ipMask,    SPI_Ethernet_getIpMask(),       4) ; // get assigned IP mask
                          memcpy(gwIpAddr,  SPI_Ethernet_getGwIpAddress(),  4) ; // get assigned gateway IP address
                          memcpy(dnsIpAddr, SPI_Ethernet_getDnsIpAddress(), 4) ; // get assigned dns IP address
                          
                          
       UART1_Init(115200);               // Initialize UART module at 9600 bps
  Delay_ms(100);                  // Wait for UART module to stabilize

//  UART1_Write_Text("Start");
//  UART1_Write(13);
//  UART1_Write(10);

  while (1) {                     // Endless loop
    if (UART1_Data_Ready()) {     // If data is received,
      uart_requst = UART1_Read();     // read the received data,
      SPI_Ethernet_sendUDP(server_Addr, 1002, 1002, uart_requst, 1); // send uart_requst message to the above IP address, from UDP port 1002 to UDP port 1002



      UART1_Write(uart_requst);       // and send data via UART
    }
    }

}

elmofty
Posts: 20
Joined: 19 Feb 2012 00:09

Re: need help in rs232 to udp and rs232

#2 Post by elmofty » 13 Jan 2013 02:13

in this code i try to send 9 bytes but i cannot recive else first only 3 bytes on the rs232 and in the upd i got 3 bytes but not the same 3 bytes which sent

elmofty
Posts: 20
Joined: 19 Feb 2012 00:09

Re: need help in rs232 to udp and rs232

#3 Post by elmofty » 15 Jan 2013 01:31

36 view and no one can help me
where is the support

User avatar
dejan.odabasic
mikroElektronika team
Posts: 2649
Joined: 30 Apr 2012 14:20

Re: need help in rs232 to udp and rs232

#4 Post by dejan.odabasic » 17 Jan 2013 10:29

Hello,

You are using high UART baud rate(115200) and polling mechanism for reading data via UART.
This is causing lost of your data. You have collected only three bytes which are saved in buffer.

I suggest using UART interrupt for collecting data in bigger RAM buffer and then send them via UDP.
Since you are not using UserUDP function you don't need to call SPI_Ethernet_doPacket() function.
This will save you some processing time.

Receiving device could have some similar code problems - delay casing lost of data.
Receiving device should call SPI_Ethernet_doPacket() as as often as possible.

Best regards.

elmofty
Posts: 20
Joined: 19 Feb 2012 00:09

Re: need help in rs232 to udp and rs232

#5 Post by elmofty » 17 Jan 2013 20:24

hello to all
thaks my dear for your replay
i am not clear to use interrupt function can you please help me in this code
i will be thanks or give me sample example for it
thanks for all

User avatar
dejan.odabasic
mikroElektronika team
Posts: 2649
Joined: 30 Apr 2012 14:20

Re: need help in rs232 to udp and rs232

#6 Post by dejan.odabasic » 18 Jan 2013 08:44

Hello,

You haven't mentioned which PIC MCU are you using, but interrupt examples written for PIC18F45k22 can be found on LibStock:
http://www.libstock.com/projects/view/3 ... c-18f45k22

Best regards.

elmofty
Posts: 20
Joined: 19 Feb 2012 00:09

Re: need help in rs232 to udp and rs232

#7 Post by elmofty » 23 Jan 2013 03:16

i try this but i cannot work good
can you please help me with code for this read data uard and send using udp
or send me uart buffer example
thanks for all

User avatar
dejan.odabasic
mikroElektronika team
Posts: 2649
Joined: 30 Apr 2012 14:20

Re: need help in rs232 to udp and rs232

#8 Post by dejan.odabasic » 23 Jan 2013 17:55

Hello,

Please understand that writing custom user code is out of the scope of our technical support.
Maybe some of our more experienced users could help you.

Thank you for understanding.

Best regards.

Aquadrox
Posts: 168
Joined: 20 Mar 2012 18:11
Location: France / Gien

Re: need help in rs232 to udp and rs232

#9 Post by Aquadrox » 24 Jan 2013 09:24

Hi,

If you want UART exemple look at here:

Code: Select all

http://www.mikroe.com/forum/viewtopic.php?f=88&t=52255#p204143
This is the ISR UART handler.

Post Reply

Return to “mikroC PRO for PIC General”