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
}
}
}