Hi,
Does someone here knows how to convert Serial to TCP packet?
I'm having a problem to convert Serial data into TCP packet & send it from Arduino to my PC. I managed to convert UDP to Serial and Serial back to UDP packet, also able to convert TCP to Serial, but have no luck converting Serial to TCP packet.
If anyone knows about this matter, appreciated if could lend me some help. I'm using Duemilanove and Ethernet Shield.
I posted my source code for easier reference.
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFA, 0xDE }; //Arduino Rx MAC Address
byte ip[] = { 192, 168, 1, 175 }; //Arduino Rx IP Address
byte gw[] = { 192, 168, 1, 252 }; //default gateway
byte subnet[] = { 255, 255, 255, 0 };
byte targetIp[] = { 192, 168, 1, 65 }; //PC's IP Address
#define MAX_SIZE 100
Client client(targetIp,8000);
int incomingByte = 0; // for incoming serial data
void setup()
{
Ethernet.begin(mac, ip, gw, subnet);
Serial.begin(38400); // opens serial port, sets data rate to 38400 bps
client.connect(); //connect to specified IP and port
}
void loop()
{
if(Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
//client.connect();
//delay(10);
if(client.connected()) {
client.write((uint8_t *)&incomingByte,MAX_SIZE); //send TCP packet to targetIp
}
}
if (client.available()) {
char c = client.read();
}
if (!client.connected()) {
client.stop();
for(;;)
;
}
}