[OT?] Reading hardware from thread

Hello,

This might be a slightly Off Topic question but I don't know where else to 
put it.

I have a digital I/O PCI Card that I have to read at least once every 35 ms 
(We are using the I/O card for communicating with an PLC that has about 35 
ms cycle time.

I have tried using OnTimer() but it will not work with these small times. 
So I was thinking that I maybe could start a thread that reads the I/O card 
continuously in a loop and creates Events Messages when something changes.

Since I haven't done anything like this before I just want to check with 
you guys and girls if it's a stupid idea or not!?

Any suggestion on how to solve the problem would be appreciated!

// Anders
-- 
English is not my first, or second, language
so anything strange, or insulting, is due to
the translation.
Please correct me so I may improve my English!
0
andis59 (75)
4/18/2007 4:06:12 PM
vc.mfc 33608 articles. 0 followers. Follow

5 Replies
511 Views

Similar Articles

[PageSpeed] 29

On Wed, 18 Apr 2007 18:06:12 +0200, Anders Eriksson
<andis59@gmail.com> wrote:

>Hello,
>
>This might be a slightly Off Topic question but I don't know where else to 
>put it.
>
>I have a digital I/O PCI Card that I have to read at least once every 35 ms 
>(We are using the I/O card for communicating with an PLC that has about 35 
>ms cycle time.
>
>I have tried using OnTimer() but it will not work with these small times. 
>So I was thinking that I maybe could start a thread that reads the I/O card 
>continuously in a loop and creates Events Messages when something changes.
>
>Since I haven't done anything like this before I just want to check with 
>you guys and girls if it's a stupid idea or not!?
>
>Any suggestion on how to solve the problem would be appreciated!
>

I have no trouble running a timer on a 50 msec cycle, doing things 20
times a second.  The actual smallest time slice you can get depends
strongly on the operating system though.  I believe it may also depend
on environmental factors, for example, whether a notebook computer is
running on AC power or on the battery.  You cannot count on a specific
time because you don't have a true real-time operating system.  So if
it absolutely essential that you MUST read the data every 35 msec,
come what may, no matter what the operating system is doing at the
time, then you will either have to write your own kernel level driver
or else get an "outboard" small embedded CPU system with its own
operating system that reads the data and stores it for transfer to
your Windows system.  



0
r
4/18/2007 4:42:38 PM
My suggestion is to use a seperate thread to perform the I/O reading. Inside 
the thread:

hTimer = CreateWaitableTimer(..);
SetWaitableTimer(hTimer, ...);

do
{
    ReadIo(...);
    Do something with the reading result (for example, send a usre defined 
message to your maim thread for the reading) ....

    SetWaitableTimer(hTimer, ...);
    WaitForSingleObject(hTimer, ...);

} while (exit_condition);

This way, your main app. can go on to do its own thing utill it receives the 
result from the I/O reading; or if it has nothing to do, just wait on the 
I/O reading thread using WaitForSingleObject.

Eric



"Anders Eriksson" <andis59@gmail.com> wrote in message 
news:y5ot62o2p1f5.dlg@ostling.com...
> Hello,
>
> This might be a slightly Off Topic question but I don't know where else to
> put it.
>
> I have a digital I/O PCI Card that I have to read at least once every 35 
> ms
> (We are using the I/O card for communicating with an PLC that has about 35
> ms cycle time.
>
> I have tried using OnTimer() but it will not work with these small times.
> So I was thinking that I maybe could start a thread that reads the I/O 
> card
> continuously in a loop and creates Events Messages when something changes.
>
> Since I haven't done anything like this before I just want to check with
> you guys and girls if it's a stupid idea or not!?
>
> Any suggestion on how to solve the problem would be appreciated!
>
> // Anders
> -- 
> English is not my first, or second, language
> so anything strange, or insulting, is due to
> the translation.
> Please correct me so I may improve my English! 


0
yren (4)
4/18/2007 6:26:07 PM
If you use this approach (which seems rational), then inside the loop, you
probably should adjust the waiting time dynamically, based on the actual
interval since the last loop.  In other words, since even the waitable timer
is not 100% accurate, it might be wrong to always wait for a fixed value of
35 msecs.  Instead, read the current system time, and then wait for a
dynamically-adjusted period of time that so that on average, there is a call
to ReadIo every 35 mesecs, even though each individual call is not exactly
35 mesecs from the last one.  This way, over long periods of time, you at
least maintain a 35 msecs interval, on average, without ever losing data for
failure to read it in time.

I thought this was called "isochronous", but that terminology might be
wrong.

Mike

"Yi Ren" <yren@lasercraftinc.com> wrote in message
news:uOwYqbegHHA.392@TK2MSFTNGP06.phx.gbl...
> My suggestion is to use a seperate thread to perform the I/O reading.
Inside
> the thread:
>
> hTimer = CreateWaitableTimer(..);
> SetWaitableTimer(hTimer, ...);
>
> do
> {
>     ReadIo(...);
>     Do something with the reading result (for example, send a usre defined
> message to your maim thread for the reading) ....
>
>     SetWaitableTimer(hTimer, ...);
>     WaitForSingleObject(hTimer, ...);
>
> } while (exit_condition);
>
> This way, your main app. can go on to do its own thing utill it receives
the
> result from the I/O reading; or if it has nothing to do, just wait on the
> I/O reading thread using WaitForSingleObject.
>
> Eric
>
>
>
> "Anders Eriksson" <andis59@gmail.com> wrote in message
> news:y5ot62o2p1f5.dlg@ostling.com...
> > Hello,
> >
> > This might be a slightly Off Topic question but I don't know where else
to
> > put it.
> >
> > I have a digital I/O PCI Card that I have to read at least once every 35
> > ms
> > (We are using the I/O card for communicating with an PLC that has about
35
> > ms cycle time.
> >
> > I have tried using OnTimer() but it will not work with these small
times.
> > So I was thinking that I maybe could start a thread that reads the I/O
> > card
> > continuously in a loop and creates Events Messages when something
changes.
> >
> > Since I haven't done anything like this before I just want to check with
> > you guys and girls if it's a stupid idea or not!?
> >
> > Any suggestion on how to solve the problem would be appreciated!
> >
> > // Anders
> > -- 
> > English is not my first, or second, language
> > so anything strange, or insulting, is due to
> > the translation.
> > Please correct me so I may improve my English!
>
>


0
4/18/2007 11:12:04 PM
It is never, ever, going to happen in this lifetime.  You cannot read a card from user
space, and it will never be possible to do so.  Not in any real operating system.  (I do
not consider MS-DOS to be a real operating system, so what is possible to do in MS-DOS is
irrelevant in the real world).

This is why you will need to write a device driver.  If your card already HAS a device
driver, then it should not require polling to accomplish what you are doing.

You are never going to get a precise 35ms time in the kernel; it is the responsibility of
the card to interrupt you every 35 ms so you can do this.  Otherwise, the card is a
defective design.  But even if it actually does interrupt, there are no guarantees when
your user-level code will see the results of this interrupt; it could be several hundred
milliseconds before you get control in user space.

Once you have a device driver, it is possible that you could use the multimedia timers to
get your 35ms notification, in user space, but what that really means is that
sort-of--every-35-ms you will get a notifrication; by the time you get the request down to
the device, some arbitrary, unbounded, and indeterminate amount of time will have elapsed.
By the time you get the reply back, some other arbitrary, unbounded, and indeterminate
amount of time will elapse. So if you need to feed it something every 35 ms or it will
fail, you are in trouble.  This is another reason you will need a device driver; it can
respond within a few hundred nanoseconds most of the time, within a few microseconds in
the worst case, if you can do everything in the ISR (if you can't and need a DPC routine,
you can figure a few hundred microseconds for the worst case scenario).

Timing precision in user space is problematic, and can be assumed (with the exception of
the multimedia times) to be at best a very chancy thing, since general purpose operating
systems have a very loose grasp of the concept of time.  35ms means somewhere between 35ms
and two weeks, with the average being somewhere between 40ms and 100ms on most days.  And
in the case of interfacing to a device, once you send an I/O request to the device, all
bets about timing go out the window.  There are NO guarantees.
					joe

On Wed, 18 Apr 2007 18:06:12 +0200, Anders Eriksson <andis59@gmail.com> wrote:

>Hello,
>
>This might be a slightly Off Topic question but I don't know where else to 
>put it.
>
>I have a digital I/O PCI Card that I have to read at least once every 35 ms 
>(We are using the I/O card for communicating with an PLC that has about 35 
>ms cycle time.
>
>I have tried using OnTimer() but it will not work with these small times. 
>So I was thinking that I maybe could start a thread that reads the I/O card 
>continuously in a loop and creates Events Messages when something changes.
>
>Since I haven't done anything like this before I just want to check with 
>you guys and girls if it's a stupid idea or not!?
>
>Any suggestion on how to solve the problem would be appreciated!
>
>// Anders
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15972)
4/22/2007 6:44:42 PM
Since the resolution of the system timer is 10ms or 15ms, and 35ms is not a multiple of
either of these, a waitable timer will not give you the expected result.  And in this
case, a waitable timer is just a complicated way of doing a Sleep().  You would have to
use multimedia timers, which do callbacks in a separate thread, but even so the timing is
problematic.  It might work most of the time, but it could fail massively under some
conditions, and there is nothing that can be done to prevent this.
					joe

On Wed, 18 Apr 2007 14:26:07 -0400, "Yi Ren" <yren@lasercraftinc.com> wrote:

>My suggestion is to use a seperate thread to perform the I/O reading. Inside 
>the thread:
>
>hTimer = CreateWaitableTimer(..);
>SetWaitableTimer(hTimer, ...);
>
>do
>{
>    ReadIo(...);
>    Do something with the reading result (for example, send a usre defined 
>message to your maim thread for the reading) ....
>
>    SetWaitableTimer(hTimer, ...);
>    WaitForSingleObject(hTimer, ...);
>
>} while (exit_condition);
>
>This way, your main app. can go on to do its own thing utill it receives the 
>result from the I/O reading; or if it has nothing to do, just wait on the 
>I/O reading thread using WaitForSingleObject.
>
>Eric
>
>
>
>"Anders Eriksson" <andis59@gmail.com> wrote in message 
>news:y5ot62o2p1f5.dlg@ostling.com...
>> Hello,
>>
>> This might be a slightly Off Topic question but I don't know where else to
>> put it.
>>
>> I have a digital I/O PCI Card that I have to read at least once every 35 
>> ms
>> (We are using the I/O card for communicating with an PLC that has about 35
>> ms cycle time.
>>
>> I have tried using OnTimer() but it will not work with these small times.
>> So I was thinking that I maybe could start a thread that reads the I/O 
>> card
>> continuously in a loop and creates Events Messages when something changes.
>>
>> Since I haven't done anything like this before I just want to check with
>> you guys and girls if it's a stupid idea or not!?
>>
>> Any suggestion on how to solve the problem would be appreciated!
>>
>> // Anders
>> -- 
>> English is not my first, or second, language
>> so anything strange, or insulting, is due to
>> the translation.
>> Please correct me so I may improve my English! 
>
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15972)
4/22/2007 6:47:27 PM
Reply:

Similar Artilces:

OT...check this out...OT
http://www.escapeyesterworld.com/ try all the controls -- John Inzer Thanks John...pretty nifty. -- Brian Kvalheim Microsoft Publisher MVP http://www.publishermvps.com This posting is provided "AS IS" with no warranties, and confers no rights. "John Inzer" <oobie@doobie.xyz> wrote in message news:uuhImG%23mFHA.3988@TK2MSFTNGP10.phx.gbl... > http://www.escapeyesterworld.com/ > try all the controls > > -- > > John Inzer > > ...

Read receipts not returning
Hello, We have a user who uses the read receipt option in Outlook 2007. Recently he has been complaining that he does not get as many read reciepts back anymore. He said they have dramtically stopped about a difference of 95%. Nothing has been changed on the Exchange side so what else could be causing the problem of him not recieving all of his read reciepts back? Thanks!! Sending a read receipt is controlled by the person receiving the e-mail. If they elect to prevent the sending of them your user isn't going to get them. DC wrote: > Hello, > > We ...

multiple read receipts
Hello all, I'm having a strange problem in Exchange 2000. A user A deleted a mail lang ago. Now others users (B, C, ...) are getting read receipts from that deleted mail. These read receipts comes unregularely. The time stamp in the read receipt says that user A read the mail some days till some weeks ago... Sometimes the time stamp is during night hours (the user ist not logged on...). In message tracking center I can see that the read receipt is originated from User A mailbox server. I don't have an idea where to look for this. Can someone help? Thanks! Gero For those ...

how to kill thread
I want to kill a thread from outside the thread. Currently, this is how I make the thread: Handle handle = CreateThread(NULL,NULL,(LPTHREAD_START_ROUTINE)StartLogging, this, NULL, NULL); Later on in the program, I want to kill the thread using its handle. So something like this would be useful: KillThread(handle); But KillThread is not a real method. Any ideas how I can accomplish this? Thanks. Jess Jessica Weiner wrote: > I want to kill a thread from outside the thread. Currently, this is how I > make the thread: > > Handle handle = CreateThread(NULL,NULL,(LPTHREAD_STA...

All Unread Messages are loaded into Inbox As Read
I am using Windoes Mail on Vista-x64. My mail account is with AOL and Windows Mail is configured to use it as an IMAP server. There are no problems sending or receiving e-mails except that all e-mail coming into the Inbox are never in BOLD to indicate they are unread. They are all loaded as if they were read. I can manually set an e-mail to Unread and in that case it will hold the message in BOLD indicating it is unread and it stays that way even if I close and reopen Windows mail. This started happening recently and I'm not sure what triggered it. Any help would be ap...

Mark All as Read does'nt recurse sub folders
Can Outlook be configured? thanks, B No. You need to do the folders separately. -- Diane Poremsky [MVP - Outlook] Outlook Tips: http://www.outlook-tips.net/ Outlook & Exchange Solutions Center: http://www.slipstick.com Outlook Tips by email: dailytips-subscribe-request@lists.outlooktips.net EMO - a weekly newsletter about Outlook and Exchange: EMO-NEWSLETTER-SUBSCRIBE-REQUEST@PEACH.EASE.LSOFT.COM You can access this newsgroup by visiting http://www.microsoft.com/office/community/en-us/default.mspx or point your newsreader to msnews.microsoft.com. "Brian Stoop" <...

mail read
just loged into my mail account. looks like some of my email was read but i don't remember reading it. how can i tell who last accessed my account? Look for an event 1016 in the event log. This will indicate a mailbox being accessed by another account. Nue "jack" <chasd1@cox.net> wrote in message news:bweGf.248$6G.133@dukeread03... > just loged into my mail account. looks like some of my email was read but > i don't remember reading it. how can i tell who last accessed my account? > On Tue, 7 Feb 2006 22:33:03 -0600, "jack" <chasd1@cox....

how to read XML with entities using C#
Hi there, I have two xml files, one is a master file and the other is just a fragment of xml. Master xml file uses 'DOCTYPE' to define the other file as an entity. Then, the master uses entity references that are supposed to be expanded into real content at parsing time. Examples are provided below. When I open master xml file in InternetExplorer , IE shows correct content. All the entities are transformed into right xml. So far I have been unsuccessfull in getting similar result when I try to do the same programmatically using C#. I was trying to use XmlDocument and XmlVal...

Passing Outlook objects between threads C++
Im using C++ VS2005 and in the main thread I establish a reference to an Outlook Contact Item. I call the Display method and it correctly creates an inspector and displays the Contact. While the Contact is open I pass a IDISPATCH pointer to the Contact to another thread. In the new thread I AddRef the Contact, the pointer seems good because I can get various properties, FileAs etc. However when I try to call the Close method I get an invalid access exception (the method does not return with an hr result). Watching the Inspector events, OnClose and OnCloseComplete do get called before ...

VC and Cobra and OTS
Hello, Can i use VC to build a project using cobra and OTS to communicate with other UNIX system? Do you mean CORBA? WTH "So Sick" <Sug@sug.com.hk> wrote in message news:OJ6KUh%232DHA.1264@TK2MSFTNGP11.phx.gbl... > Hello, > > Can i use VC to build a project using cobra and OTS to communicate with > other UNIX system? > > > > yes ..... sorry. "WTH" <spamsucks@Ih8it.com> wrote in message news:u09iMT$2DHA.2460@TK2MSFTNGP10.phx.gbl... > Do you mean CORBA? > > WTH > > "So Sick" <Sug@sug.com.hk>...

Multi-threaded (/MT)
i would like to more about visual studio runtime library ,what is the main difference between Multi-threaded (/MT),Multi-threaded Debug (/ MTd),Multi-threaded Debug DLL (/MDd) ..etc.. thanks.. keralafood@gmail.com wrote: > i would like to more about visual studio runtime library ,what is the > main difference between Multi-threaded (/MT),Multi-threaded Debug (/ > MTd),Multi-threaded Debug DLL (/MDd) ..etc.. > thanks.. keralafood: The ones named debug should be used in debug builds. With MT (static linking), the C Runtime Library (CRT) is a static library (.lib) which is com...

Threads, objects and classes
Hi I was to make multi-threading application (dialog based), but on the beggining i was stuck because of this reason: I got one main class (dialolg) in which i wanted to create two therads which were declareted in classes CThreadOne and CThreadTwo (CWinThread). I still dont know how to gain access from main dailog class to threads classes - how it should look like ? Should I include header files from this therad classes in main class and then create objects and use afxbeginthread method ? Or its not the right way ? Thx in advance for help Everything should work well. You should not require...

Excel 2007: Toggling Read Only Causes File to Lock
Okay, here's the situation... but first a little background: Affected O/S: Windows XP Pro SP2 and Vista Enterprise SP1 Office Version: 2007 Pro Plus SP1 After opening an Excel workbook that is stored on a network drive, I click the "Toggle Read Only" button. The top border in Excel now has "[Read-Only]" after the file name. Great! Now, if I click again to Toggle Read Only I receive this error: http://www.filedropper.com/excelroerror For those who can't see that, the error reads: Window Title - "File in Use" "Filename.xls is locked for editin...

Why am I unable to read some text in outlook 2003?
Unable to read some text messages in Outlook 2003 unless I right click and highlight them. I have 2 users on the pc and both user security settings are exactly set to the same settings. It reads fine on one person and not the other. Please advise. Thank You, Seniorsit On 28 Dec 2005, =?Utf-8?B?U2VuaW9yIFNpdA==?= wrote in news:DCE92378-F4F3-48EE-BFA1-A791CFCE47CB@microsoft.com > Unable to read some text messages in Outlook 2003 unless I right > click and highlight them. I have 2 users on the pc and both > user security settings are exactly set to the same settings. It > read...

Can you sort on the number of messages in thread?
Hi all, When you have your mail grouped on a field, is it then possible to sort on the number of messages in a thread. So threads with only 1 message will be on top en threads with more messages will appear lower. Hope this is possible..... All the best (this might be a solution to my previous question in another yet unanswered thread about filtering messages that are replied to) extra info: the version xp (but 2003 options are ok too) Best, Bert As far as I know, that's not an option "plurker" <nomail@nomail.com> wrote in message news:11ffe8fgqvmbc88@corp.su...

read / write clipboard VBA
I am writing a macro in which I refer to the clipboard. So I asked: how to save a range of cells to the clipboard to later in the loop repeatedly retrieve the data from the clipboard. This is one technique that may help: Option Explicit Sub testme() Dim FromCell As Range Dim DestCell As Range Dim iCtr As Long With ActiveSheet Set FromCell = .Range("A1") Set DestCell = .Range("B1") End With For iCtr = 1 To 10 FromCell.Copy _ Destination:=DestCell Set DestCell = DestCell.Offset(1, 0) Next...

OT
OT - OFF - TOPIC Hola amig@s; El gobierno Colombiano en su continuo inter�s de ofrecer oportunidades acad�micas y profesionales a todos sus habitantes y a todos los interesados en capacitaci�n ha preparado una de las mejores plataformas de educaci�n virtual, con cursos virtuales "certificados" con cero costo. Los invito a visitar la WEB del Servicio nacional de Aprendizaje, nacionales o extranjeros pueden acceder a una de las plataformas educativas gratuitas mejor estructuradas y pagadas por todos los Colombianos. Hay cursos de inform�tica, office, outlook , cursos de Excel...

Read binary File problem
Hi All Here is my problem I am using a Unicode project and I tried to read the File like sPath =3D LPCTSTR; FILE* oFp =3D _tfopen(sPath,L"r"); while(!feof(oFp)) { if (fgets(sReadData, 3072, oFp) !=3D NULL) { sStringBuffer +=3DsReadData; } } fclose(oFp); But when I tried to Read a *.BMP file I get BM6 in the Buffer sStringBuffer. after that It encounters end of file and closes down. Can you please let me know where I am making a mistake to read files. Thanks Anup >FILE* oFp = _tfopen(sPath,L"r"); >But when I tried to Read a *....

Read Only Attachments #2
Hi! i have a problem, we are using exchange 2000 and at clients we are using windows xp with outlook 2003, when ever one user sends a mail with word attachment to other users,outlook makes it Read Only. however the original file is not Read Only and other users are unable to make changes to the file. we want that attachments should not be Read Only, the user be able to make changes to the file and be able to resend it to the original sender along with the changes. Thank You in Advance Naveed Shakoor ...

How to add a read receipt button to the toolbar
I have recently migrated from Eudora to Outlook 2003, and really miss the ability to toggle the read receipt button right on the toolbar in a new message window. Is there a way to replicate this feature in Outlook 2003 (so I don't have to click the options button, check the read receipt box, adn click close)? Thanks for any and all guidance. if it's not one of the options in view, toolbars, customize you'd need to write a VBA macro to do it. -- Diane Poremsky [MVP - Outlook] Author, Teach Yourself Outlook 2003 in 24 Hours Need Help with Common Tasks? http://www.outlook-tips....

Deleting Read-Only Data
Hi! Turning to you guys for advice. I have a newly installed CRM db on which have been producing a bunch of test records in order to familiarize myself with the system. It is now time to set up proper data in order to approach production. However, some of the data I was toying with is read-only, for good reasons I am sure. But in this instance I just want to get rid of it. Can I safely remove the data using Enterprise Manager in order to avoid having to reinstall the whole thing? (It is installed on a production server) Or will that short-cut come back and bite me later? Gratefully !Robert...

Question about Hardware IO mapping
I am in need of some direction with I/O mapping. We recently moved a client from a 2000 server up to a new 2008 server. In the old server was PCI-DIO24 parallel interface card that was linked to an LCRAM card reader which was used by an office app to import and export data. The old IO address of the card (in the 2000 server) was 380h, and on that 2000 server everything seemed to work fine. When I moved the apps and hardware from the old server to the new one the application that drives the LC cards is still wanting to read based on the old hardware address, 380h, even though ...

C# registry read converts string
i'm trying to read a registry key in both local and remote machines. for example the HKLM\SYSTEM\CURRENTCONTROLSET\SERVICES\LANMANWORKSTATION there is a key called "imagepath" this is a "reg_expand_sz" and it will look like: "%systemroot%\system32\svchost.exe -k netsvcs". when i run my code on either local or remote machine it will convert "%systemroot%" to the local machine system drive, in my cast "e:\system32\svchost.exe -k netsvcs". so now my output is incorrect. here is a sample of my code: if (t == Syst...

Reading email from another country
I have someone that emails me from Russia and I've had to copy and paste it in a translator. Is there some addon I can install that will convert the language to English for me? What version of Outlook? Do you have Office installed or just Outlook? -- Diane Poremsky [MVP - Outlook] Outlook Tips: http://www.outlook-tips.net/ Outlook & Exchange Solutions Center: http://www.slipstick.com/ Outlook Tips by email: mailto:dailytips-subscribe-request@lists.outlooktips.net EMO - a weekly newsletter about Outlook and Exchange: mailto:EMO-NEWSLETTER-SUBSCRIBE-REQUEST@PEACH.EA...

OT...Ping...Brian Kvalheim...OT
Hey...Brian... Check your e-mail. If you didn't receive anything from me...give me a current address. -- John Inzer Hmmmm you didn't send me anything John Inzer wrote: || Hey...Brian... || || Check your e-mail. || || If you didn't receive anything from || me...give me a current address. || || -- || || John Inzer Did not receive anything. I am at bkvalheim@hotmail.com -- Brian Kvalheim Microsoft Publisher MVP http://www.publishermvps.com This posting is provided "AS IS" with no warranties, and confers no rights. "��MS-Publisher��" wrote: >...