葱拌豆腐 发表于 2015-6-2 14:54:39

求教:如何用W5100实现上下位通讯?

最近做一个项目需要通过以太网通讯,想问一下下位在接收是需不需要进行超时判断?如果哪位能提供一个简单的示例,感激不尽。

贴一下刚完成的接收函数(未测试)
//W5100 variable define
#define SF_WAIT_FRAME_HEAD                0X01
#define SF_WAIT_FRAME_LEN                        0X02
#define SF_WAIT_FRAME_CMD                        0X03
#define SF_WAIT_FRAME_DATA                0X04
#define SF_WAIT_FRAME_CHK                        0X05

boolean DataReceivedOK = false;
boolean alreadyConnected = false;
boolean EnthernetIsOK = false;
byte PacketBuffer;
unsigned int LocalPort = 8888;

byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192, 168, 1, 177);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress Center(192, 168, 1, 113);
// Enter the IP address of the server you're connecting to:
IPAddress server(192, 168, 1, 113);
EthernetServer AlarmServer;
// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 23 is default for telnet;
// if you're using Processing's ChatServer, useport 10002):
EthernetClient AlarmClient;
byte ReceiveBuf;
byte SendBuf[]={0xaa,0x03,0x05,0x28,0x11,0xbb};
String str="";
void setup()
{
        // start the Ethernet connection:
        Ethernet.begin(mac, ip);
        // Open serial communications and wait for port to open:
        Serial.begin(9600);
        // if you get a connection, report back via serial:
        if (AlarmClient.connect(server, 9050))
        {
                Serial.println("connected");
        }
        else
        {
                // if you didn't get a connection to the server:
                Serial.println("connection failed");
        }
}

void loop()
{
        // if there are incoming bytes available
        // from the server, read them and print them:
        if (client.available())
        {
               
                /*static char sn=0;
                static char len=0;*/
                char c = client.read();
                Serial.println(c);
                if (c == 0xaa)
                {
                        Serial.println("Get Frame HEAD 0xAA");
                }
                else if (c == 0xbb)
                {
                        Serial.println("Get Frame Length 0xBB");
                }
                else
                {
                        Serial.print("Get other Byte value:");
                        Serial.println(c);
                }

        }

        // as long as there are bytes in the serial queue,
        // read them and send them out the socket if it's open:
        while (Serial.available() > 0)
        {
                char inChar = Serial.read();
                if (client.connected())
                {
                        client.print(inChar);
                }
        }

        for(int i=0;i<6;i++)
        {
                client.write(SendBuf);
        }
        delay(1000);
        // if the server's disconnected, stop the client:
        if (!client.connected())
        {
                Serial.println();
                Serial.println("disconnecting.");
                client.stop();
                // do nothing:
                while (true);
        }
}

void EthernetInit()
{
        Ethernet.begin(mac, ip, gateway, subnet);
        AlarmServer.begin();
}

void AlarmServerStart()
{
        EthernetClient client = AlarmServer.available();

        if (client)
        {
                if (!alreadyConnected)
                {
                        client.flush();
                        client.println("Hello,client!");
                        alreadyConnected = true;
                }

                if (client.available()>0)
                {
                        char thisChar = client.read();
                        AlarmServer.write(thisChar);
                }
        }
}
//连接server
void AlarmClientStart()
{
        if (AlarmClient.connected())
        {
                EnthernetIsOK = true;
                Serial.println("Connected");
        }
        else
        {
                EnthernetIsOK = false;
                AlarmClient.connect(Center, LocalPort);
        }
}
//读取一个字节
byte AlarmClientRead()
{
        byte c;
        if (AlarmClient.available())
        {
                c = AlarmClient.read();
                FrameStateMachine(c);
                //Serial.print(c);
        }
        return c;
}
//接收报文状态机
/*
报文格式:0xAA   len    cmd    datacheck
0xaa : 报文头
len:data区数据长度,不超过20
cmd:命令字,一个byte
data:len个byte
check:校验字,等于len^cmd^data
*/
void FrameStateMachine(byte c)
{
        static byte FSState = SF_WAIT_FRAME_HEAD;
        static byte len = 0;
        static byte CheckSum = 0;
        //static byte TimeOutCounts = 0;//超时计数

        switch (FSState)
        {
        case SF_WAIT_FRAME_HEAD:
                CheckSum = 0;
                len = 0;
                if (c == 0xAA)
                {
                        PacketBuffer = c;
                        FSState = SF_WAIT_FRAME_LEN;
                }
                break;
        case SF_WAIT_FRAME_LEN:
                if (c <= 0x14 && c >= 0x00)                                                                                                //数据长度不能大于20个字节
                {
                        PacketBuffer = c;
                        CheckSum = CheckSum^PacketBuffer;
                        FSState = SF_WAIT_FRAME_CMD;
                }
                else
                {
                        FSState = SF_WAIT_FRAME_HEAD;
                }
                break;
        case SF_WAIT_FRAME_CMD:
                if (c>0x01 && c<0x40)                                                                //command word is between 0x01 and 0x40
                {
                        if (PacketBuffer>0)
                        {
                                PacketBuffer = c;
                                CheckSum = CheckSum^PacketBuffer;
                                FSState = SF_WAIT_FRAME_DATA;
                        }
                        else
                        {
                                PacketBuffer = c;
                                CheckSum = CheckSum^PacketBuffer;
                                FSState = SF_WAIT_FRAME_CHK;                                        //如果数据长度为0,则进入校验值接收
                        }
                }
                else
                {
                        FSState = SF_WAIT_FRAME_HEAD;
                }
                break;
        case SF_WAIT_FRAME_DATA:
                PacketBuffer = c;
                CheckSum = CheckSum^ c;
                len++;
                if (len>PacketBuffer)
                {
                        FSState = SF_WAIT_FRAME_CHK;
                }
                break;
        case SF_WAIT_FRAME_CHK:
                if (c == CheckSum)
                {
                        FSState = SF_WAIT_FRAME_HEAD;
                        DataReceivedOK = true;
                }
                else
                {
                        FSState = SF_WAIT_FRAME_HEAD;
                }
                break;
        default:
                FSState = SF_WAIT_FRAME_HEAD;
                break;
        }
}
页: [1]
查看完整版本: 求教:如何用W5100实现上下位通讯?