From 7d38f681799dbc903741ba4bf686ad1fe6293df4 Mon Sep 17 00:00:00 2001 From: follower Date: Tue, 11 Dec 2007 12:18:09 +0000 Subject: [PATCH] Add 'available()' method to connection to match 'Serial.available()'. Change 'read()' method to match behaviour of 'Serial.read()'. Document some of this. Print bytes received from the demo to the console. git-svn-id: svn+ssh://oldsvn/home/mlalondesvn/svn/cral@114 3ee9b42a-b53c-0410-a25e-f0b6218d5d5b --- .../wiz810mj/src/demo/WizDemo4/WizDemo4.pde | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/branches/follower/wiz810mj/src/demo/WizDemo4/WizDemo4.pde b/branches/follower/wiz810mj/src/demo/WizDemo4/WizDemo4.pde index 1796a11..842f7e5 100644 --- a/branches/follower/wiz810mj/src/demo/WizDemo4/WizDemo4.pde +++ b/branches/follower/wiz810mj/src/demo/WizDemo4/WizDemo4.pde @@ -226,6 +226,7 @@ class NetworkConnection { // Essentially a Socket wrapper int listen(); int isConnected(); + int available(); int read(); void NetworkConnection::close(); @@ -249,8 +250,39 @@ int NetworkConnection::listen() { // TODO: Make private or protected? return !!::listen(_socket); // TODO: Use C++ namespaces for the driver functions? } +int NetworkConnection::available() { + /* + + Functionality matches 'Serial.available()'. + + Note: If the socket is not connected then this will return 0, + but it is intended that 'isConnected()' would be checked first. + + Returns: + "The number of bytes available to read ... or 0 if none are available. + If any data has come in, [...].available() will be greater than 0." + + */ + // TODO: Do we want to check for 'isConnected' as well, or not? + // We have to, I guess, for valid behaviour with 'getSn_*'. + if (!isConnected()) { + return 0; + } + + return getSn_RX_RSR(_socket); +} + +#define NO_READ_DATA_AVAILABLE -1 + int NetworkConnection::read() { /* + + Functionality matches 'Serial.read()'. + + Returns: + "an int, the first byte of incoming serial data available + (or -1 if no data is available)." + Note: I thought 'recv' blocked until data was available, but it currently seems not to block after all. @@ -261,6 +293,11 @@ int NetworkConnection::read() { */ uint8_t theByte; + + if (!available()) { + return NO_READ_DATA_AVAILABLE; + } + recv(_socket, &theByte, 1); return theByte; } @@ -403,6 +440,14 @@ void setup () { Serial.println("Connected..."); + while (conn.isConnected()) { + if (conn.available()) { + Serial.print(conn.read(), BYTE); + } + } + + Serial.println(""); + conn.close(); Serial.println("End test and dummy loop");