Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.

henryyao

Member Since 14 Dec 2012
Offline Last Active Feb 06 2013 07:21 PM
-----

Topics I've Started

how to stop the socket.connection

05 February 2013 - 04:24 AM

the socket.connection() really disturbs me thesedays. The program will stuck at socket.connection() forever if the remote port cannot be accessed.

 

So is there a way to solve that, say give it a 5 secs timeout?

 

Is there a way to do that despite the thread.abort()?

 

And  since my socket is a tcp/ip stream socket, and there is a shake hands procedure when the connection is being setup. normally it will try the syn/ack procedure for 21 secs before timeout but somehow it takes much more than that in netduino. 

That's my another confusion.

 

thanks for any comment


how to stop the socket.connect() function if it stucks?

31 January 2013 - 09:57 PM

I'm facing a problem of stop the socket.connect() function. Sometimes the physical link between my local machine and the remote machine might be good, but due to some reason, the remote endpoint cannot be accessed, maybe a firewall or the port on the remote machine is closed. In such cases, the socket.connection() function will be stuck there and waits for an infinite long time...Even the firewall is disabled later, the function will still stuck there forever.

So I tried to find a way to stop the socket.connect() when faces the above situations.

The thing is I'm using a .net micro framework in which I dont have timeout mechanism or task or socket.beginconnect();

I'm trying making the socket.connect() itself a thread and tried to abort() it after 2 seconds if (!thread.join(2000)). However, I dont quite understand the abort() function and i've heard its an unwise way to do so and it does not work afterall.

Also I'd like the socket keep trying until it succeed in connecting.

Now i dont know what to do about it? can anyone help? thx a lot. it's a netduino plus board

main function {

remoteEndPoint = new IPEndPoint(IPAddress.Parse(IP_add), 30000);

while (true){ m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//Set up socket m_socket.Connect(remoteEndPoint); myThread.Start(); if (!myThread.Join(2000)) { Debug.Print(myThread.ThreadState.ToString()); myThread.abort();

m_socket.close(); }

else

{break;} } } private static void socket_connect() { m_socket.Connect(remoteEndPoint);//Connect to remote device }

 

 

I'm also have the following code by creating a new thread each time.

 

 

while (true) { try { m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);//reset up socket myThread = new System.Threading.Thread(new System.Threading.ThreadStart(socket_connect)); myThread.Start(); if (!myThread.Join(2000)) { throw new SocketException(SocketError.AccessDenied); } } catch (Exception ex) {

m_socket.Close();

Debug.GC(true); } } } private static void socket_connect() { m_socket.Connect(remoteEndPoint);//Connect to remote device }

 

The above code will allow the GC to deal with the useless threads instead of using abort(). This code will run like 10 mins before it stops running anymore. And I've checked the memory, it still have more than 45M. Can anyone tell me why this happens?


how to do simple I2c reading

21 December 2012 - 11:18 PM

Hi,i'm having some problem in reading data from I2C bus. The slave is a temperature sensor with i2c address 0xD0.
Following is my code.I'm having an unhandled exception.
Please let me know what's is problem.

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;

namespace i2c
{
    public class Program
    {
        public static void Main()
        {
            // write your code here
            byte[] buffer = new byte[10] ;
            I2CDevice.Configuration config = new I2CDevice.Configuration(0xD0,100);
            I2CDevice dev = new I2CDevice(config);
           
            I2CDevice.I2CWriteTransaction write = I2CDevice.CreateWriteTransaction(new byte[1] { 0 });
            
            I2CDevice.I2CReadTransaction read = I2CDevice.CreateReadTransaction(buffer);
              
            I2CDevice.I2CTransaction[] actions = new I2CDevice.I2CTransaction[] { write, read };
            dev.Execute(actions, 1000);

            Debug.Print(buffer[0].ToString());

        }

    }
}

how to end a NativeEventHandler Thread

20 December 2012 - 09:58 PM

I'm having a problem with ending a NativeEventHandler(button). The problem is main thread stuck at the join() function. I dont understand why the childthread is not released after it is run.

public class Program
    {
        private static Thread mainThread;
        private static Thread childThread;     

        private static InterruptPort button = new InterruptPort(Pins.GPIO_PIN_D1, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeLevelHigh);
      
        private static OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);
        
        public static void Main()
        {            
            mainThread = Thread.CurrentThread;
            Thread.Sleep(1000);
            while (true)
            {
                Thread.Sleep(100);
                button.OnInterrupt += new NativeEventHandler(ButtonEvent);
                mainThread.Suspend();
                childThread.Join();//It stuck here.
                Thread.Sleep(100);
                button.EnableInterrupt();
                button.ClearInterrupt();  
            }
        }

        private static void ButtonEvent(uint port, uint state, DateTime time)
        {
            childThread = Thread.CurrentThread;
            button.DisableInterrupt();          
            mainThread.Resume();
           // Thread.CurrentThread.Abort(); this .Abort() seems doesn't terminate the thread either.
        }
    }

How to do Ethernet communication in netduino in c#

20 December 2012 - 01:31 AM

Hi everyone, Recently I got a project and netduino need to be a socket client to send out the control information to a socket server(let's say a video machine). The thing is I dont know if the video machine support TCP/ip or whatever, so I'd think of sending ethernet packets rather than using the IP layer. Is there a way to do such thing in VS C#? Since I did not find anything like "ProtocolType.Ethernet" in setting up a socket protocol.And things like "new IPEndPoint(IPAddress.Parse("10.11.0.122"), 4000);" are used in the IP socket, so another question is that what will be the corresponding thing in Ethernet. I'd really appreciated some examples if there is any. Thanks

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.