Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Program comm port programmatically?

Status
Not open for further replies.

bassl1ne

Technical User
Sep 14, 2004
2
GB
I am writing a VB application to run as a service utilizing the following code:
My problem is my application requires the use of the serial port, however, as there is no loaded form for the service, events from the comm control do not fire. My guess is that I need to use the serial port programmatically? Can anyone point me in the right direction?

Thanks
 
If you are running as a service then why the need to program the communications? This has surely got to be a long-winded way of doing things...

Fire up your Comm control from a hidden form? e.g. Form1 Loaded but not shown, vbminimized true, ShowinTaskbar False... Just an idea, but surely easier.. If you need refinements, i'll happily ask but you should be able to go on your own...

Hope it helps.

JagWeb.gif

[tt]'Very funny, Scotty... Now Beam down my clothes.'[/tt]
 
Bearing in mind that the Comm control is a wrapper round the API calls, thea API will offer a solution.

API calls to look at include
CreateFile, WriteFile, ReadFile
Get|SetCommState, Get|SetCommTimeouts, Get|SetCommMask, WaitCommEvent

or of course, you could use a hidden form ad already suggested

Take Care

Matt
If at first you don't succeed, skydiving is not for you.
 
I have tried the suggestion of running a hidden form, the problem with this solution is that i am unable to read fired events on the form code.

For example, if i use the following code with a Timer Control on the hidden form, the timer never gets called.

Private sub tmrMyTimer_Timer()
'Timer interval & enabled set elsewhere in code
' Code here never calls called
'
End sub

Is there another way to read fired events from a control?

Thanks
 
I think there is only 1 event for the MSComm control which fires when the state of the control has changed. You then have to check the CommEvent property to see whats up. Instead of relying on the OnComm event, you might be able to check the CommEvent property in a loop, taking different actions based on the value...

Do While True

Select Case MSComm1.CommEvent

Case comEvSend
'Do something

Case comEvReceive
'Do something

Case etc, etc
'Exit Do somewhere
End Select

DoEvents

Loop

 
Bear in mind, that although the form is hidden, it must be loaded into memory and be active.

Set Form1 Property "ShowinTaskbar" to false.

Load form1
form1.windowstate = vbminimized

Form is hidden & Timer will still fire in this circumstance.
(Assuming Timer is enabled)

Hope this helps.


JagWeb.gif

[tt]'Very funny, Scotty... Now Beam down my clothes.'[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top