Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 5 of 5

Thread: Teensy 4.1 Ethernet

  1. #1
    Junior Member
    Join Date
    Oct 2017
    Posts
    9

    Teensy 4.1 Ethernet

    I have Ethernet hardware functioning on Teensy 4.1, using NativeEthernet.h.

    Webserver example yields the following output:

    Code:
    Ethernet WebServer Example
    using HW_OCOTP_MAC* - see https://forum.pjrc.com/threads/57595-Serial-amp-MAC-Address-Teensy-4-0
    MAC: 04:e9:e5:0b:c2:21
    IP Address 10.22.1.164
    10.22.1.164
    server is at 10.22.1.164
    My hope is to communicate to the Teensy through my LAN using a .NET application.

    Code:
               TcpClient client = new TcpClient("10.22.1.164", 20);
                NetworkStream stream = client.GetStream();
                Byte[] data = new Byte[1024];
                String responseData = String.Empty;
                Int32 bytes;
                while (true)
                {
                    bytes = stream.Read(data, 0, data.Length);
                    if (bytes > 0)
                    {
                        responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
                        Console.WriteLine("Received: {0}", responseData);
                    }
                }
    Although I have the Teensy now connected to LAN, I can neither ping the Teensy server nor find it with the .Net code.

    Any Pointers would be appreciated.
    thanks

  2. #2
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,553
    ping should work, you may need to configure netmask and broadcast and router IP. also your .Net example uses port 20, but WebServer will be on port 80

  3. #3
    Junior Member
    Join Date
    Oct 2017
    Posts
    9
    Changed from WebServer to Web Client, DHCP assigned by Router, and works well.

    Code:
    Initialize Ethernet with DHCP:
    using HW_OCOTP_MAC* - see https://forum.pjrc.com/threads/57595-Serial-amp-MAC-Address-Teensy-4-0
    MAC: 04:e9:e5:0b:c2:21
      DHCP assigned IP 192.168.0.3
    connecting to www.google.com...
    connected to 172.217.14.196
    
    disconnecting.
    Received 280677 bytes in 6.1474, rate = 45.66 kbytes/second

    next task is to broadcast sensor data from Teensy over LAN to Listening devices.

  4. #4
    Junior Member
    Join Date
    Oct 2017
    Posts
    9
    Cant seem to crack this one:

    .Net indicates we are connected to the Teensy yet we do not receive acknowledgement from Teensy that there is a connection.

    Code:
    void loop() {
      // listen for incoming clients
      EthernetClient client = server.available();
    
      if (client == true) {
        Serial.println("new client");
        // an http request ends with a blank line
        boolean currentLineIsBlank = true;
        while (client.connected()) {
          if (client.available()) {
            char c = client.read();
            Serial.write(c);
            // if you've gotten to the end of the line (received a newline
            // character) and the line is blank, the http request has ended,
            // so you can send a reply
            if (c == '\n' && currentLineIsBlank) {
              // send a standard http response header
              client.println("HTTP/1.1 200 OK");
    ...
    variable "client" never goes true.

    c# code
    Code:
            private void Form1_Load(object sender, EventArgs e)
            {
                TcpClient client = new TcpClient("192.168.0.164", 80);
                bool con = client.Connected;
                string message = "GET / HTTP/1.1";
                Byte[] data = System.Text.Encoding.ASCII.GetBytes(message);
                NetworkStream stream = client.GetStream();
    
                stream.Write(data, 0, data.Length);
                //Byte[] data = new Byte[1024];
                String responseData = String.Empty;
                int bytes;
                while (true)
                {
                    bytes = stream.Read(data, 0, data.Length);
                    if (bytes > 0)
                    {
                        responseData = System.Text.Encoding.ASCII.GetString(data, 0, bytes);
                        Console.WriteLine("Received: {0}", responseData);
                    }
                }
                stream.Close();
                client.Close();
    
            }
    to completely confuse me Arduino states the server.available() will always evaluate false if no client has data available for reading. https://www.arduino.cc/en/Reference/ServerAvailable

    Assume the client is expected to send a request string. Thats all I can think of.

    thanks

  5. #5
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,553
    server.available() returns a pointer or NULL, not a boolean, so your test should be if (client) { ....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •