|
-
Jul 9th, 2009, 03:52 PM
#1
Thread Starter
Fanatic Member
Serial port confusion
My brain has turned to mush over this one.
I was in the process of writing a lengthy post showing my code and what I've tried but just erased it because it started sounding ridiculous.
I'm reading data from the serialport and everything is fine -- the data received event triggers the serialport1.read and I'm writing the data into an array which I then look for "this and that".
What I want to do is also try to add a "connected" and "disconnected" label to the program in case someone pulls the wire to the PC.
So, long story short, I know I can probably use serialport1.cdholding (the carrier detect pin) and sample it for on/off.
The problem is that it starts to get very confusing because I need to have 2 serial port procedures set up and both would require open and close (which would cause a conflict).
So, I tried a "serial" approach to it --
(1) sense the CD pin and then if true
(2) read the data
But after writing a dozen different attempts I couldn't get that to work either.
Any push in the right direction would be appreciated -- it's been a couple of months away from writing code so I'm rusty to say the least.
And there you go -- I wrote ANOTHER lengthy post !
-
Jul 9th, 2009, 04:02 PM
#2
Re: Serial port confusion
Is this one way communication (that is, you can only read from the device) or is it a 2-way communication (you can send a command to the device and expect a reply from it)?
If it's only 1-way, you can guestimate the frequency of the device sending out data, and use a stopwatch or something to monitor for data arrivals. If after x amount of time and you do not receive any data, you display some notification to the user.
If it's 2-way communication, you send a command to the device and time how long you get a reply back. If after x amount of time and n tries you still don't get a reply, you can inform the user to check cables, check device to see if it power on...
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Jul 9th, 2009, 04:41 PM
#3
Thread Starter
Fanatic Member
Re: Serial port confusion
It's one way Stan -- it's very confusing because if you try to do anything with the data read from the port (as to whether it's on/off), all you have to sample is the last data written into the array....which is after the fact of a wire being connected or disconnected.
I'm going to sleep on it -- which always works.
I think I need to get out my scope, I do have a couple of ideas.
It really I think is going to come down to the carrier detect bit and some way of constantly monitoring it without interfering with my data read.
Maybe I'll have to change the 8N1 status or something and use that bit itself if possible.
I need a beer for now.
-
Jul 10th, 2009, 05:46 AM
#4
Thread Starter
Fanatic Member
Re: Serial port confusion
I know I'm over-thinking this and once again it will come down to a simple answer but just off the top of my head this morning, it's really about this --
It's easy to think in terms of code executing in response to an event --
serialport1.datareceived
do stuff
end sub
But how do I respond to the "non" event? Like when the serilaport is NOT receiving data.
No data? > do this stuff
That's what I need to figure out.
-
Jul 10th, 2009, 06:12 AM
#5
Thread Starter
Fanatic Member
Re: Serial port confusion
serialport1.datareceived --
If serialport1.bytestoread > 0 then
do my code
endif
? (If no bytes -- nothing would execute?)
I'll try it -- I don't know what happens when the serialport is disconnected (as in wire off) (then bytestoread = 0?)
-
Jul 10th, 2009, 07:26 AM
#6
Re: Serial port confusion
It's fairly simple. For example, if you expect the data to be received at least once every 5 miniutes, use a timer and set its interval to 5 minutes. Start the timer when you connect to the serial port. Then in the data received event, you stop the timer, do your normal stuff with the received data and before you get out of that subroutine, you start the timer. Handle the timer.tick event and do whatever needed there (note that with this set up, whenever the timer.tick event is raised, it means that you have not received any data from the port for the period of the timer interval)
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Jul 10th, 2009, 09:19 AM
#7
Re: Serial port confusion
You can run a looping function (as another thread perhaps) that simply checks:
Code:
abort = False
Do Until abort
Try
Threading.Thread.Sleep(100) '100 ms wait between checks
If Not serialport1.IsOpen Then serialport1.Open
Catch
End Try
Loop
-
Jul 10th, 2009, 10:32 AM
#8
Thread Starter
Fanatic Member
Re: Serial port confusion
Rather than checking on the port being open or closed, what I'm trying to do is detect when the actual hardwire has been disconnected from the port.
It's a one way connection to the PC that is RS232 but just one pair for the data, no other pins are used.
So I have to react to the data only.
Looking at serialport.errorreceived right now -- maybe that's the right track.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|