serial to ethernet

Hello!

I need a sketch solution for serial to ethernet and ethernet to serial converter. I used the sample from the 1.0.5 IDE called chatServer, and the part ethernet=>serial port works fine. I wrote a part which is responsible for communication from serial port to ethernet, but i cannot get even a single char without error (baudrate is the same in terminal as in program). Sorry for my poor english, but it is not my native language.

#include <SPI.h>
#include <Ethernet.h>

byte mac = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1, 20);
IPAddress gateway(192,168,1, 1);
IPAddress subnet(255, 255, 0, 0);
EthernetServer server(23);
boolean alreadyConnected = false; // whether or not the client was connected previously

void setup() {
// initialize the ethernet device
Ethernet.begin(mac, ip);
// start listening for clients
server.begin();
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print(“Chat server address:”);
Serial.println(Ethernet.localIP());
}

void loop() {
// wait for a new client:
EthernetClient client = server.available();

// when the client sends the first byte, say hello:
if (client) {
if (!alreadyConnected) {
// clead out the input buffer:
client.flush();
Serial.println(“We have a new client”);
client.println(“Hello, client!”);
alreadyConnected = true;
}

if (client.available() > 0) {
// read the bytes incoming from the client:
char thisChar = client.read();
// echo the bytes back to the client:
server.write(thisChar);
// echo the bytes to the server as well:
Serial.write(thisChar);
}
//--------------------------------------added part-----------------------
if (Serial.available())
{
delay(10);
char inByte;
inByte = Serial.read();
Serial.print(inByte);
}

}
}

That code does something. You haven't explained what.

You want it to do something. What that is isn't clear.

What exactly do you mean by "serial to ethernet"? Your added code does nothing but read from the serial port and write back to the same port.

In this example, the code accepts a new client connection each time loop() is called. In that case you need to completely handle the connection and either close it yourself, or wait for the client to close it, before loop() returns.

To do that I'd expect to see the code looping as long as the client remains connected or unread data from the client was available, and in the loop you would test whether there was a byte available on the serial port (and handle it if so) and test whether there was a byte available from the client (and handle it if so).

If I remember correctly, there are example server sketches for the Ethernet library which demonstrate how to handle the incoming client connection in this way. You'd just need to add in the logic to deal with bytes from the two streams.

PaulS:
That code does something. You haven't explained what.

You want it to do something. What that is isn't clear.

What exactly do you mean by "serial to ethernet"? Your added code does nothing but read from the serial port and write back to the same port.

I forgot add "server.write(inByte)" in this message.
I would like to send a single char or a string from serial port to ethernet, but when i use command "Serial.read()" in ChatServer sketch I have problems with proper reading char from terminal and to ethernet client is sent random char like "%"^"~`" t\etc.

write() sends binary data. print() sends ASCII data. Hopefully, this is enough of a clue.

Even when I change "write" to 'print" i still have a mess with chars in ethernet terminal window... :frowning:

if (Serial.available())
 {
 delay(10);

Why? Why are you twiddling your thumbs? You know that there is serial data to read.

i still have a mess with chars in ethernet terminal window.

I have no idea what this means. What is an "ethernet terminal window"?

Ok, it looks like a hardware problem. On Arduino Mega2560 sent chars and strings from ethernet => serial port work well. From serial port => ethernet there were random chars. I changed mega board to arduino Uno, uploaded the sketch and transmission eth<>serial works great! Maybe there is something wrong with RX (pin0) on the arduino mega board.

Anyway Thanks!