TCP Communications between a PC and an Arduino Using Lazarus / Free Pascal

This is the next installment in a series of posts discussing various ways to communicate between a PC and an Arduino. This post is only going to discuss the differences between using UDP vs TCP. If you are interested in this, you should first review these posts as they have detailed descriptions of the hardware and the function of the software:

UDP Communications between a PC and an Arduino Using Lazarus / Free Pascal

Serial Communications between a PC and an Arduino Using Lazarus / Free Pascal

After getting the UDP version of the software running, I wanted to then use TCP. The hardware stays the same, I just have to change the software.

Before the arduino can send/receive data, a TCP session must be established. I allow for this at the top of the infinite while loop in the loop function:

// establish TCP connection
while (!client.connected()) {
    client.stop();                                    // not connected, so terminate any prior client
    client = server.available();
    if (client)
        Serial.println("TCP Connection Established");
    else
        continue;
    }

This is backwardy code. To insert it into the infinite while loop of my loop function, a connection may already be made, so before establishing a connection, I make sure one hasn’t been already THEN I establish it with server.available().

As with the conversion from serial to UDP, the code only need to be modified in the two procedures where I read/write from the hardware.

boolean a2p_i_recvPacket(
    char*                                 packet,
    int                                    packetMax
    ){
    char                                c;
    int                                    i;
    int                                    packetSize;

    i = 0;
    while (client.connected()) {
        if (client.available()) {
            c = client.read();
            switch (c) {
                case '\r':
                    break;
                case '\n':
                    if (i > packetMax)
                        return false;
                    packet[i] = '';
                    Serial.print("@a2p_i_recvPacket: received ");
                    Serial.println(packet);
                    return true;
                default:
                    if (i > packetMax)
                        return false;
                    packet[i] = c;
                    i = i + 1;
                    break;
                }
            } // clientavail
        } // while

The receive procedure goes back to looking much like the serial port procedure did: I must read the input character by character, building the packet while I look for an <LF> character to end the packet.

UDP provides a single packet of data. You get it all at once. TCP provides a stream of characters like the serial port did. It can (and will) be separated across multiple packets.

Further, I am checking that the connection isn’t lost during the read of each character. If it is, then false is returned.

The transmit packet procedure is much like the UDP version. Short and sweet:

void a2p_i_xmitPacket(
    char*                                 packet
    ) {

    client.write(packet);
    client.write("\r\n");

    } // a2p_i_xmitPacket

Pascal Code Modifications

Changing the Pascal Code from UDP to TCP was trivial. I literally just changed ‘udp’ to ‘tcp’ to use the Synapse TCP library rather than UDP library. The only addition change needed was to put back transmission of <CR><LF> like I had to do when using the serial library:

procedure p2a_i_xmitPacket(
        packet                            : string
    );

begin

if debug then
    writeln('@p2a_i_xmitPacket: packet=', packet);

try
    tcp.SendString(packet + #13 + #10);
except
    writeln('@p2a_i_xmitPacket: SendString Failed. LastError=', tcp.lastError);
    halt;
    end;

end; // p2a_i_xmitPacket

The code for the TCP version of Arduino and PC are here:

http://www.xyfyx.com/files/arduino2PCviaTCP.zip

This entry was posted in c-arduino, c-lazarus and tagged , . Bookmark the permalink.

2 Responses to TCP Communications between a PC and an Arduino Using Lazarus / Free Pascal

  1. Pingback: TCP Communications between a Raspberry Pi and an Arduino Using Lazarus / Free Pascal | Big Dan the Blogging Man

  2. Pingback: TCP Communications between a PC and an Arduino Nano Using Lazarus / Free Pascal | Big Dan the Blogging Man

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s