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...
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.
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.
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?)
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)
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
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.