
>>>>> Serial Communications
Quote:
> I have been tasked with writing a software package that sends
> commands through a serial cable. I am not really sure how to
> do this, I have sent serial commands to a modem before but
> that's it.
> If anyone knows of a resource that outlines this process, or a
> website that may describe it, I would greatly appreciate it.
> Thank you,
> Jim
You should use the MsComm Control.
If you're sending commands to a pre-defined device, then
get the manual or spec for that device. It will tell the
message that you need to use.
If you are allowed to define the format, then you could
use the NMEA-0183 spec. It's used by marine and GPS devices
to talk to each other. The info is here:
ftp://sundae.triumf.ca/pub/peter/index.html
You'll need to create a COMMLIB.BAS module, that
has these four functions:
Function b_CommOpen (ctl_Comm As Control) As Integer
Function b_CommClose (ctl_Comm As Control) As Integer
Function b_CommSendMessage (ctl_Comm As Control,
s_Message As String) As Integer
Function b_CommReceiveMessage (ctl_Comm As Control,
s_Message As String) As Integer
The MsComm control is passed in as a parameter.
In the b_CommReceiveMessage function, the string
that is received from the device is passed back
by reference to the caller.
Each function returns a boolean value that just tells
the calling code SUCCESS or FAILURE.
The Send and Receive functions, above, each have a
Do Loop that does the major action. The MsComm
control is queried (via reading a property) to
see if it can accept, or has a character ready,
respectively. Read the MsComm help file on
its properties for detailed info.
You'll need to check if the Do Loop timed out, in
each case. Here's some sample code for that part:
Dim dte_Timeout As Variant 'Define the Timeout value
'Init the timeout time. Note: COMM_RECEIVE_TIMEOUT = 5
dte_Timeout = Now + TimeSerial(0, 0, COMM_RECEIVE_TIMEOUT)
Do
.
.
.
If (Now >= dte_Timeout) Then 'Did we time out ?
s_ErrorMsg = "Timed out while waiting for message."
b_Retval = FAILURE 'If so, exit the loop
GoTo b_CommReceiveMessage_Exit
End If
.
.
.
Loop
(I've left the main loop code out of this example.)
This timeout code is only accurate to +/- 1 second, but
for the short messages I'm sending (500 chars max)
and the baud rate (4800,N,8,1), it works fine.
This should at least get you started.