Flow chart to understand the TCP-IP library

General discussion on Libstock website & codes posted on this website.
Author
Message
Prof.Wagner
Posts: 126
Joined: 25 Mar 2011 04:35

Flow chart to understand the TCP-IP library

#1 Post by Prof.Wagner » 19 Feb 2013 21:24

Hi people,

I am trying to use the library TCP-IP with mikrobasic, but I am not having sucess.

Some body can help me with a flowchart about the sequence of routines that I must use to do a device in client mode. I need to write and read a remote server.

Thank you

prof. Wagner Ideali

Prof.Wagner
Posts: 126
Joined: 25 Mar 2011 04:35

Re: Flow chart to understand the TCP-IP library

#2 Post by Prof.Wagner » 19 Feb 2013 21:26

Hi

I forgot to explain that I am using PIC 24F and mikrobasic. My device will be client and a PC will be a server.

I didnt get to understand de sequence of routines that I must to use, variable and how to read and write to the server.

Thank you

prof. Wagner Ideali

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

Re: Flow chart to understand the TCP-IP library

#3 Post by dejan.odabasic » 20 Feb 2013 15:15

Hello,

You should call as often as possible _doPacket() function, basically it processes next received packet if such exists.
ARP & ICMP requests are replied automatically, upon TCP/UDP request the _UserTCP/_UserUDP() function is called for further processing.

Since both client and server responses need to be implemented in _UserTCP() function, in order to enter the _UserTCP() function when you wish to develop client side,_startSendTCP() function need to be called.
When you call _startSendTCP() function, a dummy ACK is sent to server, when server receives it, it will respond with ACK and this will make client to enter _UserTCP() function.

Usually, when you call _startSendTCP() you can set some sort of flag variable, so when you enter _UserTCP() function checking this flag will tell you in which way you need to process the packages, as client or as server.

You can find client example project on following topic: http://www.mikroe.com/forum/viewtopic.p ... 15#p199115
In this example, one package of 'mmm...' is sent to server for every button press.

Best regards.

Prof.Wagner
Posts: 126
Joined: 25 Mar 2011 04:35

Re: Flow chart to understand the TCP-IP library

#4 Post by Prof.Wagner » 21 Feb 2013 15:42

Hi Dejan,

Please, could you check my code below, it is very simple, if I have some mistake.
I intend to start to connect and next step is to send and to recieve bytes. I didnt make the routine to do this.
I am using mikrobasic with 24F and my test board is a explorer from microchip.
I am not getting to connect. I test with many diferents programs that I have in my PC.
if I send the command ping 192.168.1.100 to this program will it return or not?

best regards

prof. Wagner

Code: Select all

SYMBOL RW =   PORTD.5
dim    myMacAddr   as byte[6]   ' my MAC address
dim    myIpAddr    as byte[4]   ' my IP address
dim    rmtipadd    as byte[4]   ' remote ip
dim    gwIpAddr    as byte[4]   ' gateway IP address
dim    ipMask      as byte[4]   ' network mask
dim    dnsIpaddr   as byte[6]   ' dns address
dim    BYTEIN      as byte
dim    i           as BYTE
dim sock as ^socket_28j60_dsc


dim Net_Ethernet_28j60_RST as sbit at LATF0_bit
dim Net_Ethernet_28j60_RST_Direction as sbit at TRISF0_bit
dim Net_Ethernet_28j60_CS as sbit at LATD14_bit
dim Net_Ethernet_28j60_CS_Direction as sbit at TRISD14_bit



' lcd
dim LCD_RS as sbit at LATB15_bit
dim LCD_EN as sbit at LATD4_bit
dim LCD_D4 as sbit at LATE4_bit
dim LCD_D5 as sbit at LATE5_bit
dim LCD_D6 as sbit at LATE6_bit
dim LCD_D7 as sbit at LATE7_bit

dim LCD_RS_Direction as sbit at TRISB15_bit
dim LCD_EN_Direction as sbit at TRISD4_bit
dim LCD_D4_Direction as sbit at TRISE4_bit
dim LCD_D5_Direction as sbit at TRISE5_bit
dim LCD_D6_Direction as sbit at TRISE6_bit
dim LCD_D7_Direction as sbit at TRISE7_bit

'*************************
' WAIT 1000ms
'*************************
sub procedure WAIT_1000ms()
    DIM I AS BYTE
    FOR I = 1 TO 10
      WAIT_100ms()
    NEXT I
end sub

'****************
' BEEP ROUTINE
'****************
sub procedure BIP()
   Sound_Play(1000,50)
end sub

sub function Net_Ethernet_28j60_UserUDP(dim udpDsc as ^UDP_28j60_Dsc) as word
RESULT = 0
end sub

sub procedure Net_Ethernet_28j60_UserTCP(dim socket as ^SOCKET_28j60_Dsc)
END SUB

'***************************
' INICIO DA ROTINA PRINCIPAL
'***************************

main:
'I/O DEFINITIONS
TRISD.5 = 0                 ' RW LCD

' set mac address
myMacAddr[0] = 0x00
myMacAddr[1] = 0x14
myMacAddr[2] = 0xA5
myMacAddr[3] = 0x76
myMacAddr[4] = 0x19
myMacAddr[5] = 0x3F

' set IP address
myIpAddr[0] = 192
myIpAddr[1] = 168
myIpAddr[2] = 1
myIpAddr[3] = 100

' set dns address
dnsIpAddr[0] = 192
dnsIpAddr[1] = 168
dnsIpAddr[2] = 1
dnsIpAddr[3] = 5

' set gateway address
gwIpAddr[0]  = 192
gwIpAddr[1]  = 168
gwIpAddr[2]  = 1
gwIpAddr[3]  = 5

' set server address
rmtipadd[0] = 192
rmtipadd[1] = 168
rmtipadd[2] = 1
rmtipadd[3] = 5

' set subnet mask
ipMask[0]    = 255
ipMask[1]    = 255
ipMask[2]    = 255
ipMask[3]    = 0

'Sound initialization
Sound_Init(PORTA, 0)        ' BEEP INICIALIZATION

WAIT_100ms()                ' Wait sound to finish

RW = 0                      ' LCD WRITE

Lcd_Init()                  ' LCD INITIALIZATION
WAIT_100ms()
Lcd_CMD(_LCD_CLEAR)
LCD_CMD(_LCD_UNDERLINE_ON)

LCD_OUT_CP("TEST TCP-IP")

BIP()
                        ' BEEP TO START
WAIT_1000MS()

Lcd_CMD(_LCD_CLEAR)

SPI2_Init_Advanced(_SPI_MASTER, _SPI_8_BIT, _SPI_PRESCALE_SEC_1, _SPI_PRESCALE_PRI_4,
                    _SPI_SS_DISABLE, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_IDLE_2_ACTIVE)
                    
Net_Ethernet_28j60_Init(@myMacAddr, @myIpAddr, Net_Ethernet_28j60_FULLDUPLEX)  ' init ethernet board

Net_Ethernet_28j60_confNetwork(@ipMask, @gwIpAddr, @dnsIpAddr)

Net_Ethernet_28j60_stackInitTCP()

Delay_ms(1000)

while(i <> 1)
    i = Net_Ethernet_28j60_connectTCP(@myIpAddr,1000,1000,@sock)
wend
LCD_OUT_CP("CONECTED!")

while(1)
  Net_Ethernet_28j60_doPacket()
wend
END.

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

Re: Flow chart to understand the TCP-IP library

#5 Post by dejan.odabasic » 21 Feb 2013 16:10

Hello,

Looking at your code, server address look suspicions to me.
I don't think it should be the same IP address as gateway.

If you made correct network setup, device should respond to PING.
Do you see "CONECTED" on LCD?
You need to call _doPacket() function in order to respond to PING.

Best regards.

Prof.Wagner
Posts: 126
Joined: 25 Mar 2011 04:35

Re: Flow chart to understand the TCP-IP library

#6 Post by Prof.Wagner » 21 Feb 2013 18:31

Hi,

I dont see conected. This is the problem.

I put the same gateway and server the same address because I dont use gateway. The conection will be local.

I dont understand when you say " You need to call _doPacket() function in order to respond to PING."

If you see my code, I am call _doPacket() function in the infinity loop in the end of program.

I will put 0.0.0.0 in the gateway.

Best regards

Prof.Wagner
Posts: 126
Joined: 25 Mar 2011 04:35

Re: Flow chart to understand the TCP-IP library

#7 Post by Prof.Wagner » 21 Feb 2013 19:05

Hi Dejan,
Could you put a small code in my program to read a byte e return it to the host?

I need to do a device in client mode only.

My need are:

1- I need first all that this code conect to the host;
2- I need that this device accept ping and return it;
3- I need to work in client mode only.

I study this tcp-ip library from LIbstock but I think that is necessary to have a flowchart, more detail and examples.

I developed with mikrobasic a long time and diferents programs to PIC24F and this is the first time that I am developing a device using tcp-ip with this library.

Could you help me?

Best regards

Prof. Wagner Ideali

Prof.Wagner
Posts: 126
Joined: 25 Mar 2011 04:35

Re: Flow chart to understand the TCP-IP library

#8 Post by Prof.Wagner » 22 Feb 2013 16:06

Hi,Dejan,

Now I get to conect and I get to send ping. I found a problem in my SPI configurantion.

I need only to do it works in Client mode insteand of server, but I dont know how to do this

Best regards

Prof. Wagner

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

Re: Flow chart to understand the TCP-IP library

#9 Post by dejan.odabasic » 22 Feb 2013 17:51

Hello,

If you have managed to get ping, than all you need to do is to implement response in _UsreTCP() function.
I suggest that you use client project which I suggested as a starting point: http://www.mikroe.com/forum/viewtopic.p ... 15#p199115

There you can see how to sent one packet of data to server.
Try testing the project with your network parameters, and try to connect to PC server application, like TCPIP Builder.

Best regards.

Prof.Wagner
Posts: 126
Joined: 25 Mar 2011 04:35

Re: Flow chart to understand the TCP-IP library

#10 Post by Prof.Wagner » 22 Feb 2013 19:29

I will try.
Thank you
Wagner

Prof.Wagner
Posts: 126
Joined: 25 Mar 2011 04:35

Re: Flow chart to understand the TCP-IP library

#11 Post by Prof.Wagner » 25 Feb 2013 17:03

Hi Dejan,

I made several tests and the program works very good (connected, send e receive bytes) but in server mode.

I put the "Net_Ethernet_28j60_startSendTCP(socket)" but the host (PC) recognizes it only in server mode.

Do you know how to put parameters in the TCP-IP builder program to PC connects only in server mode in my device?

I have several programs to TCP-IP test, but all program I get to connect it only my device to being server and PC client and I need to do opposite.

You can see below that I made like you told about the example: viewtopic.php?f=88&t=50719&p=199115#p199115

Could you help me?

Thank you

prof. Wagner

Code: Select all


program TCPIP
' Project name: teste tcpip
' IDTLA
' WAGNER IDEALI
' VERSION 1.0

SYMBOL RW =   PORTD.5
dim    myMacAddr   as byte[6]   ' my MAC address
dim    myIpAddr    as byte[4]   ' my IP address
dim    rmtipadd    as byte[4]   ' remote ip
dim    gwIpAddr    as byte[4]   ' gateway IP address
dim    ipMask      as byte[4]   ' network mask
dim    dnsIpaddr   as byte[6]   ' dns address
dim    rmtport     as byte[4]
dim    myport      as byte[4]
dim    BYTEIN      as byte
dim    SOCKET      as ^socket_28j60_dsc
dim    DADO        as byte
dim    i           as byte

dim Net_Ethernet_28j60_RST as sbit at LATD15_bit
dim Net_Ethernet_28j60_RST_Direction as sbit at TRISD15_bit
dim Net_Ethernet_28j60_CS as sbit at LATD14_bit
dim Net_Ethernet_28j60_CS_Direction as sbit at TRISD14_bit
sub procedure BIP()
   Sound_Play(1000,50)
end sub

sub function Net_Ethernet_28j60_UserUDP(dim udpDsc as ^UDP_28j60_Dsc) as word
RESULT = 0
end sub

sub procedure Net_Ethernet_28j60_UserTCP(dim SOCKET as ^SOCKET_28j60_Dsc)
IF (DADO <> "") THEN
      Net_Ethernet_28j60_putByteTCP(DADO, SOCKET)
      DADO = ""
END IF

BYTEIN = Net_Ethernet_28j60_getByte()

IF (BYTEIN <> "") THEN
   Lcd_Chr_Cp(bytein)
   Net_Ethernet_28j60_putByteTCP(BYTEIN, SOCKET)
   BYTEIN = ""
END IF
END SUB

'***************************
' MAIN ROUTINE
'***************************

main:
'I/O DEFINITIONS
trisd.5 = 0                 ' RW LCD

' set mac address
myMacAddr[0] = 0x00
myMacAddr[1] = 0x14
myMacAddr[2] = 0xA5
myMacAddr[3] = 0x76
myMacAddr[4] = 0x19
myMacAddr[5] = 0x3F

' set IP address
myIpAddr[0] = 192
myIpAddr[1] = 168
myIpAddr[2] = 1
myIpAddr[3] = 100

' set dns address
dnsIpAddr[0] = 0
dnsIpAddr[1] = 0
dnsIpAddr[2] = 0
dnsIpAddr[3] = 0

' set gateway address
gwIpAddr[0]  = 0
gwIpAddr[1]  = 0
gwIpAddr[2]  = 0
gwIpAddr[3]  = 0

' set server address
rmtipadd[0] = 192
rmtipadd[1] = 168
rmtipadd[2] = 1
rmtipadd[3] = 5

' set remote port
rmtport[0] = 65
rmtport[1] = 50

' local port
myport[0] = 65
myport[1] = 50

' set subnet mask
ipMask[0]    = 255
ipMask[1]    = 255
ipMask[2]    = 255
ipMask[3]    = 0

'Sound initialization
Sound_Init(PORTA, 0)        ' BEEP INICIALIZATION

DELAY_ms(100)              ' Wait sound to finish

RW = 0                         ' LCD WRITE

Lcd_Init()                     ' LCD INITIALIZATION

DELAY_ms(100)

Lcd_CMD(_LCD_CLEAR)

LCD_CMD(_LCD_UNDERLINE_ON)

LCD_OUT_CP("TEST TCP-IP")

BIP()
                        ' BEEP TO START
WAIT_100MS()

Lcd_CMD(_LCD_CLEAR)

SPI1_Init()  ' init spi module

Net_Ethernet_28j60_Init(@myMacAddr, @myIpAddr, Net_Ethernet_28j60_FULLDUPLEX)  ' init ethernet board

Net_Ethernet_28j60_confNetwork(@ipMask, @gwIpAddr, @dnsIpAddr)

DELAY_ms(100)

Net_Ethernet_28j60_stackInitTCP()

while(i<>1)
    i = Net_Ethernet_28j60_connectTCP(@rmtipadd, @rmtport, @myport, @socket)
wend

LCD_OUT_CP("CONECTED!")
DELAY_ms(1000)
Lcd_CMD(_LCD_CLEAR)

BYTEIN = ""
DADO   = ""
while(true)
    Net_Ethernet_28j60_doPacket()
    IF(PORTD.7 = 0) THEN             ' IF PRESS BUTTON SEND CHAR => B
      while PORTD.7 = 0                ' WAIT RELEASE THE BUTTON TO SEND
        DELAY_ms(1)
      wend
      DADO = "B"
      Net_Ethernet_28j60_startSendTCP(socket)
    END IF
wend
END.

Prof.Wagner
Posts: 126
Joined: 25 Mar 2011 04:35

Re: Flow chart to understand the TCP-IP library

#12 Post by Prof.Wagner » 26 Feb 2013 15:06

Hi Dejan,

Sorry to disturb you but I'm serious problem with this project. As I reported earlier, I can send and receive data and connect too, but I see that all happens in server mode. I need that my device works in client mode.

Could You help me with the information how I must to inform the Builder TCP program to work in client mode and could you check my code below in what is lacking to work as client mode?

I do not know what else to do.

I am very thankful and sorry again pesters you.

Best regards

Prof. Wagner Ideali

Code: Select all

program TCPIP
' Project name: teste tcpip
' IDTLA
' WAGNER IDEALI
' VERSION 1.0

SYMBOL RW =   PORTD.5
dim    myMacAddr   as byte[6]   ' my MAC address
dim    myIpAddr    as byte[4]   ' my IP address
dim    rmtipadd    as byte[4]   ' remote ip
dim    gwIpAddr    as byte[4]   ' gateway IP address
dim    ipMask      as byte[4]   ' network mask
dim    dnsIpaddr   as byte[6]   ' dns address
dim    rmtport     as byte[4]
dim    myport      as byte[4]
dim    BYTEIN      as byte
dim    SOCKET      as ^socket_28j60_dsc
dim    DADO        as byte
dim    i           as byte

dim Net_Ethernet_28j60_RST as sbit at LATD15_bit
dim Net_Ethernet_28j60_RST_Direction as sbit at TRISD15_bit
dim Net_Ethernet_28j60_CS as sbit at LATD14_bit
dim Net_Ethernet_28j60_CS_Direction as sbit at TRISD14_bit


' MODULE LCD
dim LCD_RS as sbit at LATB15_bit
dim LCD_EN as sbit at LATD4_bit
dim LCD_D4 as sbit at LATE4_bit
dim LCD_D5 as sbit at LATE5_bit
dim LCD_D6 as sbit at LATE6_bit
dim LCD_D7 as sbit at LATE7_bit

dim LCD_RS_Direction as sbit at TRISB15_bit
dim LCD_EN_Direction as sbit at TRISD4_bit
dim LCD_D4_Direction as sbit at TRISE4_bit
dim LCD_D5_Direction as sbit at TRISE5_bit
dim LCD_D6_Direction as sbit at TRISE6_bit
dim LCD_D7_Direction as sbit at TRISE7_bit

'*************************
' Wait 100ms
'*************************
sub procedure WAIT_100ms()
    DIM I AS BYTE
    FOR I = 1 TO 100
      DELAY_ms(1)
    NEXT I
end sub

'*************************
' WAIT 1000ms
'*************************
sub procedure WAIT_1000ms()
    DIM I AS BYTE
    FOR I = 1 TO 10
      WAIT_100ms()
    NEXT I
end sub

'****************
' BEEP ROUTINE
'****************
sub procedure BIP()
   Sound_Play(1000,50)
end sub

sub function Net_Ethernet_28j60_UserUDP(dim udpDsc as ^UDP_28j60_Dsc) as word
RESULT = 0
end sub

sub procedure Net_Ethernet_28j60_UserTCP(dim SOCKET as ^SOCKET_28j60_Dsc)
IF (DADO <> "") THEN
      Net_Ethernet_28j60_putByteTCP(DADO, SOCKET)
      DADO = ""
END IF

BYTEIN = Net_Ethernet_28j60_getByte()
IF (BYTEIN <> "") THEN
   Lcd_Chr_Cp(bytein)
   Net_Ethernet_28j60_putByteTCP(BYTEIN, SOCKET)
   BYTEIN = ""
END IF
END SUB

'***************************
' main routine
'***************************

main:
'I/O DEFINITIONS
trisd.5 = 0                 ' RW LCD

' set mac address
myMacAddr[0] = 0x00
myMacAddr[1] = 0x14
myMacAddr[2] = 0xA5
myMacAddr[3] = 0x76
myMacAddr[4] = 0x19
myMacAddr[5] = 0x3F

' set IP address
myIpAddr[0] = 192
myIpAddr[1] = 168
myIpAddr[2] = 1
myIpAddr[3] = 100

' set dns address
dnsIpAddr[0] = 0
dnsIpAddr[1] = 0
dnsIpAddr[2] = 0
dnsIpAddr[3] = 0

' set gateway address
gwIpAddr[0]  = 0
gwIpAddr[1]  = 0
gwIpAddr[2]  = 0
gwIpAddr[3]  = 0

' set server address
rmtipadd[0] = 192
rmtipadd[1] = 168
rmtipadd[2] = 1
rmtipadd[3] = 5

' set remote port
rmtport[0] = 65
rmtport[1] = 50

' local port
myport[0] = 65
myport[1] = 50

' set subnet mask
ipMask[0]    = 255
ipMask[1]    = 255
ipMask[2]    = 255
ipMask[3]    = 0

'Sound initialization
Sound_Init(PORTA, 0)        ' BEEP INICIALIZATION

WAIT_100ms()                ' Wait sound to finish

RW = 0                      ' LCD WRITE

Lcd_Init()                  ' LCD INITIALIZATION

WAIT_100ms()

Lcd_CMD(_LCD_CLEAR)

LCD_CMD(_LCD_UNDERLINE_ON)

LCD_OUT_CP("TEST TCP-IP")

BIP()
                        ' BEEP TO START
WAIT_100MS()

Lcd_CMD(_LCD_CLEAR)

SPI1_Init()  ' init spi module

Net_Ethernet_28j60_Init(@myMacAddr, @myIpAddr, Net_Ethernet_28j60_FULLDUPLEX)  ' init ethernet board

Net_Ethernet_28j60_confNetwork(@ipMask, @gwIpAddr, @dnsIpAddr)

WAIT_100ms()

Net_Ethernet_28j60_stackInitTCP()

while(i<>1)
    i = Net_Ethernet_28j60_connectTCP(@rmtipadd, @rmtport, @myport, @socket)
wend

LCD_OUT_CP("CONECTED!")               ' it is working very good!!!!!!
WAIT_1000MS()
Lcd_CMD(_LCD_CLEAR)

BYTEIN = ""
DADO   = ""
while(true)
    Net_Ethernet_28j60_doPacket()
    IF(PORTD.7 = 0) THEN             ' IF PRESS BUTTON SEND CHAR => B
      while PORTD.7 = 0              ' wait to release the button
        DELAY_ms(1)
      wend
      DADO = "B"                     ' send "b" to the server
      Net_Ethernet_28j60_startSendTCP(socket)
    END IF
wend
END.

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

Re: Flow chart to understand the TCP-IP library

#13 Post by dejan.odabasic » 26 Feb 2013 16:19

Hello,

Both server and client responses are handled and should be implemented in _UserTCP function.

If you are able to send letter B on button press, then you are sending data as client to server.
And when you are sending byte which you have received, then you are responding as server which received data from client.

Are you able to send letter B on button press?

When you start TCP/IP Builder, your PC IP address should be written in LocalIP text box and value of rmtport variable in text box next to LocalIP text box. Then you should click on Create Socket button and then on Listen button.

Now when you power your development system, you should connect to Server which is created using TCP/IP Builder.

TCP/IP Builder should detect when connection is established. Now you can exchange the data.
When you click on PORTD.B7, data should be displayed in Received section of TCP/IP Builder. (This would be client action from your device).

Best regards.

Prof.Wagner
Posts: 126
Joined: 25 Mar 2011 04:35

Re: Flow chart to understand the TCP-IP library

#14 Post by Prof.Wagner » 26 Feb 2013 19:55

Hi Dejan,


Now my device is working like client. My mistake was to define the local and remote port like byte[2] instead of WORD variable. It seems the same but if I define my ports like 2 bytes it doesnt work like client. I dont know why but if I define the ports like WORD it works very good.
I will improve the code and I will send to you and you can make avaliable to another user (in mikrobasic) a client code that is running very good.

Thank you a lot

Prof. Wagner Ideali

Prof.Wagner
Posts: 126
Joined: 25 Mar 2011 04:35

Re: Flow chart to understand the TCP-IP library

#15 Post by Prof.Wagner » 26 Feb 2013 20:03

Hi Dejan,

This code below works very good in client mode for mikrobasic.

I am usgin 24FJ128GA010 8Mhz.

Every data send by server will be show in the LCD and if you press the button connect to the Server and after if you press send the letter "B".

Maybe it can help another user.

Thank you

Prof. Wagner

Code: Select all

program TCPIP
' Project name: test tcpip
' VERSION 1.0

SYMBOL RW =   PORTD.5
symbol button = portd.7
dim    myMacAddr   as byte[6]   ' my MAC address
dim    myIpAddr    as byte[4]   ' my IP address
dim    rmtipadd    as byte[4]   ' remote ip
dim    gwIpAddr    as byte[4]   ' gateway IP address
dim    ipMask      as byte[4]   ' network mask
dim    dnsIpaddr   as byte[6]   ' dns address
dim    rmtport     as word
dim    myport      as word

dim    BYTEIN      as byte
dim    SOCKET      as ^socket_28j60_dsc
dim    value       as byte
dim    i           as byte
dim    buffer      as byte[64]
dim Net_Ethernet_28j60_RST as sbit at LATD15_bit
dim Net_Ethernet_28j60_RST_Direction as sbit at TRISD15_bit
dim Net_Ethernet_28j60_CS as sbit at LATD14_bit
dim Net_Ethernet_28j60_CS_Direction as sbit at TRISD14_bit


' MODULE LCD
dim LCD_RS as sbit at LATB15_bit
dim LCD_EN as sbit at LATD4_bit
dim LCD_D4 as sbit at LATE4_bit
dim LCD_D5 as sbit at LATE5_bit
dim LCD_D6 as sbit at LATE6_bit
dim LCD_D7 as sbit at LATE7_bit

dim LCD_RS_Direction as sbit at TRISB15_bit
dim LCD_EN_Direction as sbit at TRISD4_bit
dim LCD_D4_Direction as sbit at TRISE4_bit
dim LCD_D5_Direction as sbit at TRISE5_bit
dim LCD_D6_Direction as sbit at TRISE6_bit
dim LCD_D7_Direction as sbit at TRISE7_bit

'*************************
' Wait 100ms
'*************************
sub procedure WAIT_100ms()
    DIM I AS BYTE
    FOR I = 1 TO 100
      DELAY_ms(1)
    NEXT I
end sub

'*************************
' WAIT 1000ms
'*************************
sub procedure WAIT_1000ms()
    DIM I AS BYTE
    FOR I = 1 TO 10
      WAIT_100ms()
    NEXT I
end sub

'****************
' BEEP ROUTINE
'****************
sub procedure BIP()
   Sound_Play(1000,50)
end sub

sub function Net_Ethernet_28j60_UserUDP(dim udpDsc as ^UDP_28j60_Dsc) as word
RESULT = 0
end sub

sub procedure Net_Ethernet_28j60_UserTCP(dim SOCKET as ^SOCKET_28j60_Dsc)

IF (value <> "") THEN
      Net_Ethernet_28j60_putByteTCP(value, SOCKET)
      value = ""
END IF

i = 0
do
    BYTEIN = Net_Ethernet_28j60_getByte()
    BUFFER[I] = BYTEIN
    INC (I)
loop until BYTEIN = ""

i = 0
do
   Lcd_Chr_Cp(buffer[i])
   inc (i)
loop until buffer[i] = ""
END SUB

'***************************
' main routine
'***************************

main:
'I/O DEFINITIONS
trisd.5 = 0                 ' RW LCD
trisd.7 = 1                 ' button to send data or connect tcp

' set mac address
myMacAddr[0] = 0x00
myMacAddr[1] = 0x14
myMacAddr[2] = 0xA5
myMacAddr[3] = 0x76
myMacAddr[4] = 0x19
myMacAddr[5] = 0x3F

' set IP address
myIpAddr[0] = 192
myIpAddr[1] = 168
myIpAddr[2] = 1
myIpAddr[3] = 100

' set dns address
dnsIpAddr[0] = 192
dnsIpAddr[1] = 168
dnsIpAddr[2] = 1
dnsIpAddr[3] = 5

' set gateway address
gwIpAddr[0]  = 192
gwIpAddr[1]  = 168
gwIpAddr[2]  = 1
gwIpAddr[3]  = 1

' set server address
rmtipadd[0] = 192
rmtipadd[1] = 168
rmtipadd[2] = 1
rmtipadd[3] = 5

' set remote port
rmtport = 6550

' local port
myport = 6550

' set subnet mask
ipMask[0]    = 255
ipMask[1]    = 255
ipMask[2]    = 255
ipMask[3]    = 0

'Sound initialization
Sound_Init(PORTA, 0)        ' BEEP INICIALIZATION
WAIT_100ms()                ' Wait sound to finish
RW = 0                      ' LCD WRITE
Lcd_Init()                  ' LCD INITIALIZATION
WAIT_100ms()
Lcd_CMD(_LCD_CLEAR)
LCD_CMD(_LCD_UNDERLINE_ON)
LCD_OUT_CP("TEST TCP-IP")
Lcd_CMD(_LCD_SECOND_ROW)
LCD_OUT_CP("Wait...")
BIP()                        ' BEEP TO START
WAIT_100MS()

SPI1_Init()                  ' init spi module

Net_Ethernet_28j60_Init(@myMacAddr, @myIpAddr, Net_Ethernet_28j60_FULLDUPLEX)  ' init ethernet board

Net_Ethernet_28j60_confNetwork(@ipMask, @gwIpAddr, @dnsIpAddr)

WAIT_1000ms()

Net_Ethernet_28j60_stackInitTCP()

WAIT_1000ms()

Lcd_CMD(_LCD_CLEAR)
LCD_OUT_CP("Press key to")
Lcd_CMD(_LCD_SECOND_ROW)
LCD_OUT_CP("connect!")

while button = 1              ' wait to release the button
   DELAY_ms(1)
wend

while(i<>1)
    i = Net_Ethernet_28j60_connectTCP(@rmtipadd,rmtport,myport,@socket)
wend

Lcd_CMD(_LCD_CLEAR)

BYTEIN  = ""
value   = ""
while(true)
    Net_Ethernet_28j60_doPacket()
    IF(button = 0) THEN               ' IF PRESS BUTTON SEND CHAR => B
      while button = 0                ' wait to release the button
        DELAY_ms(1)
      wend
      value = "B"                     ' send "b" to the server
      Net_Ethernet_28j60_startSendTCP(socket)
    END IF
wend
END.

Post Reply

Return to “Libstock Discussion”