On 1 Aug 2013, at 12:03, Vanni Parronchi <vanniparron...@gmail.com> wrote:

> hi everyone,
> 
> I'm writing a mac osx app that needs to talk to a serial device.
> 
> There's a well known protocol to adhere to (implemented in the device
> firmware) and is a kind of a half-duplex synchronous protocol where if i
> send a message to the device from my app i have to wait till i get a
> response from the device, inspect it and then i'm allowed to send another
> message. This has to go for several messages i need to send to the device.
> 
> Given this synchronous nature, i'm trying to keep the UI free so i'm doing
> all the messaging stuff on background threads. My choice is to use
> NSOperation and NSOperationQueue, where the queue is configured with
> maxConcurrentOperations set to 1 in order to have a strict serial behavior
> (this is due to the constraints given by the implemented protocol). How can
> i make an NSOperation subclass waiting for a signal given by the serial
> port delegate to make it fire isFinished KVO to the queue and make that
> queue consequently execute the next NSOperation?
> 
> Does this approach make sense to you?

My suggestion would instead be to make an NSOperation subclass for sending a 
message to the device.

That subclass should require you to specify to which device you are talking.  
Each device object should carry around a dispatch_semaphore.  When a message is 
sent, the semaphore should be checked and set to stop any other operations 
proceeding until the semaphore is unset.  When the device responds, the 
semaphore should be unset.

You can then make your operation queue a concurrent queue, rather than a serial 
one, so that you can send messages to more than one device at the same time, 
while still maintaining your one-thing-at-a-time per device rule.

Tom Davie

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to