-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
79 changed files
with
10,958 additions
and
0 deletions.
There are no files selected for viewing
236 changes: 236 additions & 0 deletions
236
hardware/pic32/libraries/DEIPcK/examples/TCPEchoClient/TCPEchoClient.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,236 @@ | ||
/************************************************************************/ | ||
/* */ | ||
/* TCPEchoClient */ | ||
/* */ | ||
/* A chipKIT DEIPcK TCP Client application to */ | ||
/* demonstrate how to use the TcpClient Class. */ | ||
/* This can be used in conjuction with TCPEchoServer */ | ||
/* */ | ||
/************************************************************************/ | ||
/* Author: Keith Vogel */ | ||
/* Copyright 2014, Digilent Inc. */ | ||
/************************************************************************/ | ||
/* | ||
* | ||
* Copyright (c) 2013-2014, Digilent <www.digilentinc.com> | ||
* Contact Digilent for the latest version. | ||
* | ||
* This program is free software; distributed under the terms of | ||
* BSD 3-clause license ("Revised BSD License", "New BSD License", or "Modified BSD License") | ||
* | ||
* Redistribution and use in source and binary forms, with or without modification, | ||
* are permitted provided that the following conditions are met: | ||
* | ||
* 1. Redistributions of source code must retain the above copyright notice, this | ||
* list of conditions and the following disclaimer. | ||
* 2. Redistributions in binary form must reproduce the above copyright notice, | ||
* this list of conditions and the following disclaimer in the documentation | ||
* and/or other materials provided with the distribution. | ||
* 3. Neither the name(s) of the above-listed copyright holder(s) nor the names | ||
* of its contributors may be used to endorse or promote products derived | ||
* from this software without specific prior written permission. | ||
* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | ||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | ||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | ||
* IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | ||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | ||
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | ||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE | ||
* OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | ||
* OF THE POSSIBILITY OF SUCH DAMAGE. | ||
*/ | ||
/************************************************************************/ | ||
/* */ | ||
/* */ | ||
/************************************************************************/ | ||
/* Revision History: */ | ||
/* */ | ||
/* 5/14/2014 (KeithV): Created */ | ||
/* */ | ||
/************************************************************************/ | ||
|
||
/************************************************************************/ | ||
/* Supported hardware: */ | ||
/* */ | ||
/* MX7cK */ | ||
/* */ | ||
/************************************************************************/ | ||
|
||
//****************************************************************************************** | ||
//****************************************************************************************** | ||
//***************************** SET YOUR CONFIGURATION ************************************* | ||
//****************************************************************************************** | ||
//****************************************************************************************** | ||
|
||
/************************************************************************/ | ||
/* */ | ||
/* Include ONLY 1 hardware library that matches */ | ||
/* the network hardware you are using */ | ||
/* */ | ||
/* Refer to the hardware library header file */ | ||
/* for supported boards and hardware configurations */ | ||
/* */ | ||
/************************************************************************/ | ||
#include <IM8720PHY.h> // This is for the the Internal MAC and SMSC 8720 PHY | ||
|
||
/************************************************************************/ | ||
/* Required library, Do NOT comment out */ | ||
/************************************************************************/ | ||
#include <DEIPcK.h> | ||
|
||
/************************************************************************/ | ||
/* */ | ||
/* SET THESE VALUES FOR YOUR NETWORK */ | ||
/* */ | ||
/************************************************************************/ | ||
|
||
const char * szIPServer = "192.168.1.180"; // server to connect to | ||
unsigned short portServer = 44300; | ||
|
||
//****************************************************************************************** | ||
//****************************************************************************************** | ||
//***************************** END OF CONFIGURATION *************************************** | ||
//****************************************************************************************** | ||
//****************************************************************************************** | ||
|
||
typedef enum | ||
{ | ||
NONE = 0, | ||
CONNECT, | ||
WRITE, | ||
READ, | ||
CLOSE, | ||
DONE, | ||
} STATE; | ||
|
||
STATE state = CONNECT; | ||
IPSTATUS status; | ||
|
||
unsigned tStart = 0; | ||
unsigned tWait = 5000; | ||
|
||
TCPSocket tcpClient; | ||
byte rgbRead[1024]; | ||
int cbRead = 0; | ||
|
||
// this is for Print.write to print | ||
byte rgbWrite[] = {'*','W','r','o','t','e',' ','f','r','o','m',' ','p','r','i','n','t','.','w','r','i','t','e','*','\n'}; | ||
int cbWrite = sizeof(rgbWrite); | ||
|
||
// this is for tcpClient.writeStream to print | ||
byte rgbWriteStream[] = {'*','W','r','o','t','e',' ','f','r','o','m',' ','t','c','p','C','l','i','e','n','t','.','w','r','i','t','e','S','t','r','e','a','m','*','\n'}; | ||
int cbWriteStream = sizeof(rgbWriteStream); | ||
|
||
/*** void setup() | ||
* | ||
* Parameters: | ||
* None | ||
* | ||
* Return Values: | ||
* None | ||
* | ||
* Description: | ||
* | ||
* Arduino setup function. | ||
* | ||
* Initialize the Serial Monitor, and initializes the | ||
* connection to the TCPEchoServer | ||
* Use DHCP to get the IP, mask, and gateway | ||
* by default we connect to port 11000 | ||
* | ||
* ------------------------------------------------------------ */ | ||
void setup() { | ||
|
||
Serial.begin(9600); | ||
Serial.println("TCPEchoClient 3.0"); | ||
Serial.println("Digilent, Copyright 2014"); | ||
Serial.println(""); | ||
|
||
// use DHCP to get our IP and network addresses | ||
deIPcK.begin(); | ||
} | ||
|
||
/*** void loop() | ||
* | ||
* Parameters: | ||
* None | ||
* | ||
* Return Values: | ||
* None | ||
* | ||
* Description: | ||
* | ||
* Arduino loop function. | ||
* | ||
* We are using the default timeout values for the DNETck and TcpClient class | ||
* which usually is enough time for the Tcp functions to complete on their first call. | ||
* | ||
* This code will write some stings to the server and have the server echo it back | ||
* | ||
* ------------------------------------------------------------ */ | ||
void loop() { | ||
int cbRead = 0; | ||
|
||
switch(state) | ||
{ | ||
case CONNECT: | ||
if(deIPcK.tcpConnect(szIPServer, portServer, tcpClient, &status)) | ||
{ | ||
state = WRITE; | ||
} | ||
break; | ||
|
||
// write out the strings | ||
case WRITE: | ||
if(tcpClient.isEstablished()) | ||
{ | ||
Serial.println("Got Connection"); | ||
|
||
tcpClient.writeStream(rgbWriteStream, cbWriteStream); | ||
|
||
Serial.println("Bytes Read Back:"); | ||
state = READ; | ||
tStart = (unsigned) millis(); | ||
} | ||
break; | ||
|
||
// look for the echo back | ||
case READ: | ||
|
||
// see if we got anything to read | ||
if((cbRead = tcpClient.available()) > 0) | ||
{ | ||
cbRead = cbRead < sizeof(rgbRead) ? cbRead : sizeof(rgbRead); | ||
cbRead = tcpClient.readStream(rgbRead, cbRead); | ||
|
||
for(int i=0; i < cbRead; i++) | ||
{ | ||
Serial.print((char) rgbRead[i]); | ||
} | ||
} | ||
|
||
// give us some time to get everything echo'ed back | ||
else if( (((unsigned) millis()) - tStart) > tWait ) | ||
{ | ||
Serial.println(""); | ||
state = CLOSE; | ||
} | ||
break; | ||
|
||
// done, so close up the tcpClient | ||
case CLOSE: | ||
tcpClient.close(); | ||
Serial.println("Closing TcpClient, Done with sketch."); | ||
state = DONE; | ||
break; | ||
|
||
case DONE: | ||
default: | ||
break; | ||
} | ||
|
||
// keep the stack alive each pass through the loop() | ||
DEIPcK::periodicTasks(); | ||
} |
Oops, something went wrong.