Friday, August 28, 2009

Serial To Ethernet Gateway Part 1

In this article, I will talk about the design considerations for making serial to ethernet gateway.
One might think it is very easy to implement such a device. If you have both the Ethernet and Serial Input/Output (I/O) , you just have to connect the serial output to the ethernet input or vice versa to do the conversion. There you have it!! Finished! That is actually a very ideal case.

In real life, data doesn't just come in and out at a constant flow. Sometimes there are a lot of data to process, but sometimes there are none. This is similar to a highway. During the morning traffic, there are tons of people driving to work, but during the off hours, there are none.

So how do we design a system so that we can accommodate all the data coming in from serial and convert them into Ethernet data?
Well the answer is ring buffer.

A Ring buffer is type of data structure which returns to the starting point whenever the maximum is reached. In this case, the older data get written over by the new data. When using a ring buffer you need to have a write pointer and read pointer so that you can keep track of your data.

There are four cases to consider
(Notice that orange box indicates new data is written into the buffer)

1) Empty Buffer (Write pointer = Read pointer)


2) New data is written into buffer (Write pointer > Read pointer)


3) New data is written into buffer and the write pointer loops back to the beginning
(Read Pointer > Write Pointer)


4) Buffer is full (Write pointer = Read Pointer)
If your buffer is large enough, the likelihood of case 4 is very slim. However, you should still put case 4 into your consideration when designing the ring buffer.

After observing the behavior of these cases, it is important that you implement every possible cases to ensure that your system is stable. Next time, I will go through the source code for this project. Thanks for viewing

Enjoy!








Thursday, August 27, 2009

UDP Programming


User Datagram Protocol is an alternative way of sending/receive data through the internet without establishing a connection. Unlike TCP, UDP is unreliable because the receiver is not guaranteed to get the data. In addition, out of order packets may occur in the receiver's side.

As a result, UDP is mostly used in video and audio streaming because delayed packets effect the playback quality( i.e. lagging) while dropped packets do not effect the playback quality as much as delayed packets. Another unique UDP's characteristic is the ability to broadcast and multicast to different users.

The Other Day I wrote a program for testing our new W7100 in UDP mode. This program basically sends out a reply message whenever the W7100 receives a UDP message from a UDP client.

There are some considerations when writing this program:

1) How long does the Ethernet negotiation take place?
Currently, I have delayed the program to start after 7 seconds has elapsed
Without this delay, the program will go wild!

2) How much buffer is needed when receiving ?
The buffer string size is the same size as the RX buffer in the W7100 socket.

I hope you enjoyed my article. Enjoy Programming!

Below is the code which I have used to perform this test.

#define MAX_BUF_SIZE 2048 // define maximum buffer size

uint8 xdata pDestaddr[4]; //xdata using external ram

uint16 xdata pPort; // destination port

uint8 xdata rcvLen; // length of data received

uint16 xdata length; // the length of data received

uint8 xdata RXdata[MAX_BUF_SIZE]; // RX data received

uint8 xdata TXData[MAX_BUF_SIZE]; // TX data send

uint16 xdata sentLen; // length of data sent

uint16 xdata TXSize; // size of string sent

Init_iMCU(); // Initialize iMCUW7100

Init_Network(); // Initialize Network Configuration

//Delay for link

wait_1ms(7000);

// 1. create socket for udp

if(socket(0, Sn_MR_UDP,5000, 0) == 1) // create socket with UDP port 5000

{

PutString("\r\n opened socket");

// 2. receive data

while(getSn_RX_RSR(0) == 0) ; // check if there is data in socket

PutString("\r\n got data");

rcvLen = getSn_RX_RSR(0);

if(rcvLen > MAX_BUF_SIZE) // check if data received is bigger than size of buffer string

rcvLen = MAX_BUF_SIZE; // assign max buffer size to the received length

PutString("\r\n RX data size is "); PutITOA(rcvLen); PutString(" Bytes.");

length = recvfrom(0, RXdata, rcvLen, pDestaddr, &pPort); // receive data

PutString("\r\n Received Data");

RXdata[length] = 0; // indicate the end of string

PutString("\r\n RX data : ");

PutString(RXdata); // print received data

// 3. trasnmit data

//set Dest. IP address & Port

pDestaddr[0]= 192;

pDestaddr[1]= 168;

pDestaddr[2]= 1;

pDestaddr[3]= 3;

pPort = 5000;

// Send reply string 'HELLO'

PutString("\r\n TX data size is ");PutITOA(TXSize);

sentLen = sendto(0, TXData, TXSize, pDestaddr, pPort); // transfer data

if (sentLen == TXSize)

PutString("\r\n success to transmit");

else

PutString("\r\n Failed to transmit");

close(0); // close the socket

}

else // if socket is fail

PutString("\r\n Failed to open socket");

while(1);



Thursday, August 13, 2009

Casino in Singapore

Recently, I have been talking with some of our customers in Singapore. During the internet revolution, the Ethernet has pretty much conquered the PC market. Since Ethernet is such a popular and powerful interface, most of the embedded systems are following this trend as well.

One example application of using the Ethernet is in slot machines. In the Old days, when you walk into a Casino, you have to get change to play the slot machine. To avoid such inconvenience, a card is used in place of coins nowadays, so that you don’t have to insert coins every time you want to enjoy the slot machine. Once your card is inserted into the card reader, and you are ready to go. This card stores the number of credit you have left, so that you don’t have to carry loads of coins in your pocket. In fact, the slot machines are connected to each other by an Ethernet Network. Whenever you use your credits, they are automatically deducted from the central server’s data base.

No only this is a convenience from the customer’s point of view, but from the business point of view, you can also use this type of system to collect your customer’s data. Your customers are required to register their name, address and phone number in order to use this card. Since all of your customers’ playing records are recorded in your server, you can receive important marketing data such as how much they spend during their stay, Which day of the week they usually come?, What time of the day do they come most?, or How often do they come in a month? Once you have such important data, you can tailor-make your promotion activities to target specific customers. Perhaps, you can offer your customers a drink whenever they lose a certain amount of money! :)

Here is just one example of how our products can be applied in real life application. I hope you have enjoyed it! Please feel free to leave me your comments! Thanks! :)

FAE of WIZnet H.K.