check state of serial port
check state of serial port
check state of serial port
check state of serial port
check state of serial port
check state of serial port check state of serial port check state of serial port check state of serial port check state of serial port check state of serial port check state of serial port check state of serial port
check state of serial port check state of serial port
check state of serial port
Go Back  Xtreme Visual Basic Talk > > > check state of serial port


Reply
 
Thread Tools Display Modes
  #1  
Old 01-08-2011, 02:42 PM
isaksson isaksson is offline
Newcomer
 
Join Date: Jan 2011
Posts: 1
Default check state of serial port


Hello.

Iam very new to this but i have with alote of help maked a program that comunicates with a relay board over rs232.

This is the part in my code that opens the Com port

Code:
Private Sub OpenSerPort()

        If sp.IsOpen Then Exit Sub

        sp.PortName = "COM1"
        sp.BaudRate = 2400
        sp.Parity = IO.Ports.Parity.None
        sp.DataBits = 8
        sp.StopBits = IO.Ports.StopBits.One
        sp.DtrEnable = True
        sp.Open()

    End Sub
Everything works as it should in this code but my problem is that iam using a program (Switch King) that calls my program when its time to change some relays on my relay board.
Lets say that i wanna change the state of 4 relays 20:00 then Switch king starts my program 4 times at 20:00 (iam using arguments so that the board knows what relay to change.)

Here is the error message that i get.

Faulting application name: RelayController.exe, version: 1.0.0.0, time stamp: 0x4d277715
Faulting module name: KERNELBASE.dll, version: 6.1.7600.16385, time stamp: 0x4a5bdbdf
Exception code: 0xe0434352
Fault offset: 0x0000b727
Faulting process id: 0xd34
Faulting application start time: 0x01cbaea9a9c87a8e
Faulting application path: C:\RelayController\RelayController.exe
Faulting module path: C:\Windows\syswow64\KERNELBASE.dll
Report Id: e78cddec-1a9c-11e0-a63e-000c29b3cf85

Application: RelayController.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.UnauthorizedAccessException
Stack:
at System.IO.Ports.InternalResources.WinIOError(Int32, System.String)
at System.IO.Ports.SerialStream..ctor(System.String, Int32, System.IO.Ports.Parity, Int32, System.IO.Ports.StopBits, Int32, Int32, System.IO.Ports.Handshake, Boolean, Boolean, Boolean, Byte)
at System.IO.Ports.SerialPort.Open()
at RelayController.RelayController.OpenSerPort()
at RelayController.RelayController.Main()

So i guess that the problem is that the Com-port is busy becourse the program runs 4 times almost simultaneously.

Is there a fix for this? could i sheck if the port is opened by "other" program and if it is then wait until its closed and then continue with the code?
Reply With Quote
  #2  
Old 01-09-2011, 11:06 AM
SteveNutt SteveNutt is offline
Freshman
 
Join Date: Jun 2005
Location: Thailand
Posts: 35
Default

Yesterday I discovered and downloaded a free serial port tool from the MacAffee web site. There are other serial sniffers around too that may help you diagnose your problem.
__________________
IP Alarms - built with VB and ASP.net with a SQL Server back end.
Alarm Monitoring - built with ASP.Net web services.
Reply With Quote
  #3  
Old 01-09-2011, 02:01 PM
mkaras's Avatar
mkarascheck state of serial port mkaras is offline
Ultimate Contributor

Retired Leader
* Expert *
 
Join Date: Mar 2004
Location: Beaverton, OR
Posts: 1,874
Default

Let me first suggest that you dig out the reason that the unhandled exception is happening and then add code to your program to take action to handle the condition in a graceful or meaningful way.

You do not want to put code into your program that hangs the system waiting for activity in some other arbitrary program that you have little or no control over.

Lastly I would suggest that you open and close the Comm port each time your program is instantiated. If you suspect that you are being called too fast such that the OpSys thinks the port is still open when you come back in again then your best course of action is to delay the exit from your program in the first instance just after you have closed the port. You could put in a Sleep() call for your program for a reasonable delay period or time out some timer ticks. Alternatively you could try to devise code that would confirm that the serial port was closed.

There may be a whole other approach to take where you keep your program loaded and do not exit each time. Feasibility on this may depend a lot on what options you have for the link between your program and the controlling program. If you could leave yours open and only close it at such time you see the controlling program terminate then you could take control of the serial port at the beginning and then just leave it open throughout the extended life of yours.

Michael Karas
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
check state of serial port
check state of serial port
check state of serial port check state of serial port
check state of serial port
check state of serial port
check state of serial port check state of serial port check state of serial port check state of serial port check state of serial port check state of serial port check state of serial port
check state of serial port
check state of serial port
 
check state of serial port
check state of serial port
 
-->