Hi all,
I have barcode scanner of TTL OUT connection with arduino and w5100 ethernet shield..after some punches of barcode scanner ..server closes its connection itself..i am using SOCKETTEST Utility to listen ..on TCP PORT...arduino dont recognize if server closes connection..
the code is as follows:
#include <NeoSWSerial.h>
NeoSWSerial scanner1( 2, 3 );
#include <Ethernet.h>
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 10, 11);
IPAddress server(192,168,10,105);
IPAddress gateway( 192,168,10,3 );
IPAddress subnet( 255,255,255,0 );
IPAddress dns1( 59,144,127,16);
// 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, use port 10002):
EthernetClient client;
String in1,in2;
void setup()
{
Ethernet.begin(mac, ip,dns1,gateway,subnet);
Serial.begin( 9600 );
scanner1.begin( 9600 );
delay(1000);
Serial.println("connecting...");
delay(100);
// if you get a connection, report back via serial:
if (client.connect(server, 11000)) {
Serial.println("connected");
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
}
void loop()
{
if (client.connected()) {
if (scanner1.available()) { // actually Serial, but this is easier to read
char scan1 = scanner1.read(); // echo to Serial monitor window
in1+=scan1;
if (scan1 == '\n')
{
Serial.print("Arduino Received: ");
Serial.print(in1);
client.print(in1);
in1="";
}}}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
}
}
This sounds like a hardware problem (at least if it really correlates with read barcodes). Please post a wiring diagram that shows all connections including power supply.
Yes that was a LAN cable issue ..how can i reconnect or reset ethernet shield after being disconnected ..
I have tried some code...but it wont show disconnecting or softreset after i disconnect from server side..
plz have a look:
#include <NeoSWSerial.h>
NeoSWSerial scanner1( 2, 3 );
#include <AltSoftSerial.h>
AltSoftSerial scanner2; // pins 8 & 9
#include <Ethernet.h>
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 10, 11);
IPAddress server(192,168,10,105);
IPAddress gateway( 192,168,10,3 );
IPAddress subnet( 255,255,255,0 );
IPAddress dns1( 59,144,127,16);
// 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, use port 10002):
EthernetClient client;
boolean clientConnected = false;
String in1,in2;
void softReset(){
asm volatile (" jmp 0");
}
void setup()
{
Ethernet.begin(mac, ip,dns1,gateway,subnet);
Serial.begin( 9600 );
scanner2.begin( 9600 );
scanner1.begin( 9600 );
delay(1000);
Serial.println("connecting...");
delay(100);
// if you get a connection, report back via serial:
if (client.connect(server, 11000)) {
Serial.println("connected");
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
softReset();
}
}
void loop()
{
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
clientConnected = false;
softReset();
}
if (client.connected()) {
if (scanner1.available()) { // actually Serial, but this is easier to read
char scan1 = scanner1.read(); // echo to Serial monitor window
in1+=scan1;
if (scan1 == '\n')
{
Serial.print("Arduino Received: ");
Serial.print(in1);
client.print(in1);
in1="";
}}
if (scanner2.available()) { // actually Serial, but this is easier to read
char scan2 = scanner2.read(); // echo to Serial monitor window
in2+=scan2;
if (scan2 == '\n')
{
Serial.print("Arduino Received: ");
Serial.print(in2);
if (client.connected()) {
client.print(in2);
}
in2="";
}}}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
softReset();
}
}
following code is working good....but as a new comer i dont know about scalibility and stabilty..please help in giving proper flow thank you
#include <NeoSWSerial.h>
NeoSWSerial scanner1( 2, 3 );
#include <AltSoftSerial.h>
AltSoftSerial scanner2; // pins 8 & 9
#include <Ethernet.h>
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
IPAddress ip(192, 168, 10, 11);
IPAddress server(192,168,10,105);
IPAddress gateway( 192,168,10,3 );
IPAddress subnet( 255,255,255,0 );
IPAddress dns1( 59,144,127,16);
// 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, use port 10002):
EthernetClient client;
boolean clientConnected = false;
String in1,in2;
void softReset(){
asm volatile (" jmp 0");
}
void setup()
{
Ethernet.begin(mac, ip,dns1,gateway,subnet);
Serial.begin( 9600 );
scanner2.begin( 9600 );
scanner1.begin( 9600 );
delay(1000);
Serial.println("connecting...");
delay(100);
// if you get a connection, report back via serial:
if (client.connect(server, 11000)) {
Serial.println("connected");
} else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
softReset();
}
}
void loop()
{
if (scanner1.available()) { // actually Serial, but this is easier to read
char scan1 = scanner1.read(); // echo to Serial monitor window
in1+=scan1;
if (scan1 == '\n')
{
Serial.print("Arduino Received: ");
Serial.print(in1);
client.println(in1);
in1="";
}}
if (scanner2.available()) { // actually Serial, but this is easier to read
char scan2 = scanner2.read(); // echo to Serial monitor window
in2+=scan2;
if (scan2 == '\n')
{
Serial.print("Arduino Received: ");
Serial.print(in2);
client.println(in2);
in2="";
}}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
softReset();
}
}
Hii server closes connection after some time when i keep connection idle for some time..then show qr code ..it reads qr code and abruptly closes connection why is it so?
it is normal that server closes an unused tcp connection. I didn't find in Arduino networking API a keep-alive setting for the socket. I simple reconnect if the connection to server is not connected.
if (!client.connected()) {
client.stop();
client.connect(serverAdress, 502);
if (!client.connected()) {
client.stop();
return CONNECT_ERROR;
}
}
akshay123:
what is connect_error? can you send whole code ?
this code snippet is from my project and CONNECT_ERROR is my error code constant.
you can put a while(true); there and wait for watchdog restart or whatever