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's Content

There have been 19 items by henryyao (Search limited from 19-November 18)


By content type

See this member's

Sort by                Order  

#45027 how to stop the socket.connection

Posted by henryyao on 06 February 2013 - 07:04 PM in Netduino Plus 2 (and Netduino Plus 1)

Ya, I'm using 4.2. Now i'm looking for a better solution rather than multi- thread. I've been searching for a week but cannot find one..

i am not sure about 4.3, but in 4.2 its blocking - means nothing happens till a connection is etablished.

 

you may want to put it in a thread so your programm keeps running, same for receiving data




#44941 how to stop the socket.connection

Posted by henryyao on 05 February 2013 - 04:24 AM in Netduino Plus 2 (and Netduino Plus 1)

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




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

Posted by henryyao on 01 February 2013 - 06:36 PM in Netduino Plus 2 (and Netduino Plus 1)

Maybe you could use an event wait handle to do the same thing as Thread.Join(), something like this:

    public class Connector	{		private IPEndPoint _remoteEndPoint;		private AutoResetEvent _waitForConnection;		Socket _socket;		public Socket ConnectTo(byte[] ipAddress, int port)		{			_remoteEndPoint = new IPEndPoint(new IPAddress(ipAddress), port);			_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);			while (true)			{				_waitForConnection = new AutoResetEvent(false);				var connectorThread = new Thread(WaitForConnection);				connectorThread.Start();				if (!_waitForConnection.WaitOne(2000, false))				{					connectorThread.Abort();					_socket = null;				}				return _socket;			}		}		public void WaitForConnection()		{			_socket.Connect(_remoteEndPoint);			_waitForConnection.Set();		}	}

 

What makes Thread.Abort() dangerous is that you don't get the chance to clean up anything you've done inside the Thread.  Threads are usually used for long-running operations and so normally you need to clean up any resources you've allocated.  But that's not the case here - you're using a thread simply as a timeout mechanism.  If you make sure to keep the thread code as simple as possible, and you never need to clean up anything, then you should be fine. 

 

Worst case scenario here is that a connection is established between _waitForConnection.WaitOne() and _waitForConnection.Set(), so we have a connected socket that we think is unconnected.  But the socket will be disposed of anyway, so I don't think that will cause problems.

 

I see what you mean. Thanks for the explanation on abort().

And is there a difference between join() and autoresetevent in this situation?




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

Posted by henryyao on 31 January 2013 - 09:57 PM in Netduino Plus 2 (and Netduino Plus 1)

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?




#42737 how to do simple I2c reading

Posted by henryyao on 04 January 2013 - 08:48 PM in Netduino Plus 2 (and Netduino Plus 1)

Thanks everyone, I have ordered the new TPA sensor and it turns out the problem was with the old sensor. :(

Waste a lot of time on this...




#42455 how to do simple I2c reading

Posted by henryyao on 30 December 2012 - 07:11 PM in Netduino Plus 2 (and Netduino Plus 1)

What does "it does not work either" mean exactly? You should not get the argument exception with 0x68 slave address.


Well, unless you have the source code, you will not be able to step into .NET MF classes - just use 'step over'.

Also, although the documentation does not contain detailed description of the communication protocol, the linked tutorial mentions that repeated start condition is needed when reading from or writing to the slave device; you'd need special I2C methods to achieve that.

Hi CW2,

 

I did get some result from 0x68 address, however the results are strange.

 

I did not use the special I2C method you provided, instead, i changed my code to the following, so I think a startup sequence is triggered at each dev.Execute.

However, the results are still the same strange ones. Now the thing is, do I really have to use the special I2C method or its the problem with my sensor.

            I2CDevice.I2CTransaction[] action1 = new I2CDevice.I2CTransaction[] { write };            I2CDevice.I2CTransaction[] action2 = new I2CDevice.I2CTransaction[] { read };                     dev.Execute(action1, 1000);            dev.Execute(action2, 1000); 



#42453 how to do simple I2c reading

Posted by henryyao on 30 December 2012 - 06:08 PM in Netduino Plus 2 (and Netduino Plus 1)

Hi Henry, Are you sure that the address is 0xD0 and not 0x50? I2C addresses are generally 7-bit (with the top bit reserved for the write/read indicator). Chris

 

Thanks for that. The address is 0x68 in fact. It's a half of 0xD0. I'm corrected this and I keep getting strange data. It is not what its supposed to be. I guess the problem may be my TPA sensor. 




#42448 tpa81 i2c sensor read error with net cable connected

Posted by henryyao on 30 December 2012 - 05:29 PM in Netduino Plus 2 (and Netduino Plus 1)

Hi dexmac

I'm having a similar issue as your case, still I'm stuck there.

I keep getting strange value from tap81 like 7,39,46,14,3,0,0,0,0,0

I'm in a normal temperature environment so I guess the correct result should be like 7,25,23,24,23,23,21,23,21,21, stuff like that.

 

So did you figure out the reason for that?

 

thanks.

 

 

Henry

 




#42110 how to do simple I2c reading

Posted by henryyao on 25 December 2012 - 03:07 AM in Netduino Plus 2 (and Netduino Plus 1)

Is there anyone here can help me with this?



#42059 how to end a NativeEventHandler Thread

Posted by henryyao on 23 December 2012 - 04:12 PM in Netduino Plus 2 (and Netduino Plus 1)

The nice things about PIRs is that they have an internal mechanism which will ensure that they will not overload the user of their output. So in most cases this will only change every few seconds when there is a lot of movement. So no need to stop the interrupt. One word of caution, be aware that the activity done in the event handler should not take too much time. So for starters you could add a Debug.Print in the event handler which shows you that movement has detected. There should not be any more code that the Debug.Print.

The main while should be just that for now, an empty while: "while (true);".


Thanks mackelen. Could you explain more about why "be aware that the activity done in the event handler should not take too much time.", I'm putting lots of functions in my new event handler, so I'd like to know if this will cause some trouble.



#42039 how to do simple I2c reading

Posted by henryyao on 22 December 2012 - 04:31 PM in Netduino Plus 2 (and Netduino Plus 1)

Are you using external pullup resistors on the SDA and SCL lines? I think with that sensor you can use two 1.8k resistors.

Also the address on the Netduino should be set for 0x68 instead of 0xD0. Netduino needs only the 7 most significant bits for the address.


Yes, I'm using two pull up resister 2.2k. I have tested the 0x68, it does not work either.
I step into the I2CDevice dev = new I2CDevice(config); and it gives me a "No source aviable" page.
:
Locating source for 'C:\Documents\Secret Labs\Projects\Production\SecretLabs.NETMF.Hardware.NetduinoPlus\NetduinoPlusHardwareProvider.cs'. Checksum: MD5 {a5 f7 23 70 92 46 16 e3 2a 4d b1 44 79 3a ba a}
The file 'C:\Documents\Secret Labs\Projects\Production\SecretLabs.NETMF.Hardware.NetduinoPlus\NetduinoPlusHardwareProvider.cs' does not exist.
Looking in script documents for 'C:\Documents\Secret Labs\Projects\Production\SecretLabs.NETMF.Hardware.NetduinoPlus\NetduinoPlusHardwareProvider.cs'...
Looking in the projects for 'C:\Documents\Secret Labs\Projects\Production\SecretLabs.NETMF.Hardware.NetduinoPlus\NetduinoPlusHardwareProvider.cs'.
The file was not found in a project.
Looking in directory 'F:\Program Files\C# Express 2010\Common7\IDE\vc7\atlmfc'...
Looking in directory 'F:\Program Files\C# Express 2010\Common7\IDE\vc7\crt'...
The debug source files settings for the active solution indicate that the debugger will not ask the user to find the file: C:\Documents\Secret Labs\Projects\Production\SecretLabs.NETMF.Hardware.NetduinoPlus\NetduinoPlusHardwareProvider.cs.
The debugger could not locate the source file 'C:\Documents\Secret Labs\Projects\Production\SecretLabs.NETMF.Hardware.NetduinoPlus\NetduinoPlusHardwareProvider.cs'.



#42037 how to end a NativeEventHandler Thread

Posted by henryyao on 22 December 2012 - 04:17 PM in Netduino Plus 2 (and Netduino Plus 1)

Hi Henry,

There are quite some things wrong in your program. First of all, what are you trying to accomplish ? Do you want to receive a trigger when a button is pressed ?

1) In your program you are suspending/resuming/joining the main thread, even in the while loop which is running on that particular thread.
2) The subscription on the OnInterrupt event should be made before entering the while loop, the same goes with 'button.EnableInterrupt'.
3) There is no need for suspend/resume/join in this program, how did you come up with this ?
4) Why are you calling 'button.DisableInterrupt' in the event handler ?
5) Are you trying to quit the main while loop when a button is pressed ? If so use either a ManualResetEvent or an AutoResetEvent at which the while loop keeps running till it is signaled. (Use WaitOne with a timeout).


1)I thought the ButtonEvent is a new thread.I think that's what they meant in the third example(P.27) in tutorial http://forums.netdui...attach_id=1168. But I now get that the childthread and the mainthread are the same thing.
2)you are absolute right about that.
3)again from that tutorial.
4)Because I dont want to trigger the handler again when the ButtonEvent function is running(its infact not a button, its a motion detector, so I dont want to multi-trig the Button Eventin a very short time). I guess the triggering event may be accumulated, waiting in a queue to be handled, it's like scanf() in C.
5)It's a loop for forever detection if the motion detector is triggered or not.

Please corret me if I was wrong,thanks.



#42024 how to do simple I2c reading

Posted by henryyao on 22 December 2012 - 03:32 AM in Netduino Plus 2 (and Netduino Plus 1)

What Netduino Version are you using and what firmware is installed? Also what type of sensor is this? Is it a breakout board?


Hi Dave, I'm using a Netduino plus and firmware 4.2.
It is a Tpa81 sensor, here is the link to this sensor: http://www.robot-ele...m/tpa81tech.htm
And it is not a breakout board, I guess.



#42017 how to do simple I2c reading

Posted by henryyao on 21 December 2012 - 11:18 PM in Netduino Plus 2 (and Netduino Plus 1)

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());

        }

    }
}



#41991 how to end a NativeEventHandler Thread

Posted by henryyao on 21 December 2012 - 05:02 AM in Netduino Plus 2 (and Netduino Plus 1)

Have you looked at the advanced tutorials example on
http://www.netduino.com/projects/

I think it is a working example of your objective. I apologize for not answering why your code doesn't work.


I have looked at the tutorial.

I have posted the same quesiton on stackoverflow and someone answered it. And now i think the problem is that I should not be adding the event handler over and over in the loop.

And for other people new to C#, I would recommend AutoResetEvent instead of resume/suspend. The AutoResetEvent is introduced in the 4th example in the following tutorial.
http://forums.netdui...tronic-project/



#41978 how to end a NativeEventHandler Thread

Posted by henryyao on 20 December 2012 - 09:58 PM in Netduino Plus 2 (and Netduino Plus 1)

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.
        }
    }



#41970 How to do Ethernet communication in netduino in c#

Posted by henryyao on 20 December 2012 - 07:58 PM in Netduino Plus 2 (and Netduino Plus 1)

I don't think that you can do what you wan't without changing the firmware - or use another ethernet adapter, which uses an other driver, and then, if that driver allows it, you can do what you want.


So I have to go with the TCP or UDP stuff, right?
The thing is the remote server may not support TCP/IP, so now I'm stuck



#41949 How to do Ethernet communication in netduino in c#

Posted by henryyao on 20 December 2012 - 03:25 PM in Netduino Plus 2 (and Netduino Plus 1)

iirc "ethernet" is just the connection, you need a protocol, like tcp, udp ...


O, ok. But here is what in my mind: In a LAN, there is no need for the transmitter to determine the IP address when sending out a packet, so what if it just add a Ethernet head(which contains two MAC address) to the raw packet and then send it out? In this case, there wont be any TCPIP or UDP issue.
Please correct me if I was wrong.



#41911 How to do Ethernet communication in netduino in c#

Posted by henryyao on 20 December 2012 - 01:31 AM in Netduino Plus 2 (and Netduino Plus 1)

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.