I've posted a few messages regardingmy trials and tribulations reading data in from a serial device. I've just about finished working through this - but have the following major roadblock.
I'm working with an application that sends and receives data from another pc. I've been tasked with the duty of making sure that the other PC is ready to receive information. The method chosen may not be the best - but it is what it is.
The application I'm working with uses a routine called SendCommand(command as string) to send a message to the other PC to handle an operational request. Sending the message is easy enough - the problem lies in trying to process the return message. All previous return messages received from the other PC always have a carriage return and line feed (in that order) as a means of showing the message is complete and to process it. This new "check" that we are trying to implement works like this:
My VB application sends the control SYN (chr(22)) character.
In response, one ofthree things will happen from the other pc:
1.) it will immediately respond with string "$" (with no carriage return or line feed) - meaning it is ready to receive.
2.) it will respond with string "%" (with no carriage return or line feed) - meaning it is not ready to receive.
3.) it will send nothing (meaning the RS-232 link is down) - not likely - but should be considered
The problem lies within the existing code (I think...) because it expects all messages to end with a carriage return and line feed. Here is that part of the code:
Code Snippet
Private Sub DataAvailable(ByVal sender As Object, ByVal e As EventArgs) Handles Serial.DataAvailable
Dim BytesIn As Integer = Serial.Available
''Serial.Available = Public Overrides ReadOnly Property Available() As Integer
''Member of: Sax.Communications.SerialConnection
Dim SingleChunk(BytesIn - 1)
As Byte
Dim Index As Integer
Dim ThisCharactor As Char
' Protect the Buffer to stop other threads writing to it
Serial.Read(SingleChunk, 0, BytesIn)
'' Serial.Read = Public Overrides Function Read(ByVal buffer() As Byte, ByVal offset ''As Integer, ByVal count As Integer) As Integer
''Member of: Sax.Communications.SerialConnection
For Index = 0 To (BytesIn - 1)
ThisCharactor = Chr(SingleChunk(Index))
If ThisCharactor = Chr(10) Then
If SerialBuffer = "+OK" Then ' the message sent was recieved ok
TransmittedOK =
True
ResultEvent.Set()
MainForm.DebugInfo.AddToLog(SerialBuffer)
ElseIf SerialBuffer = "-CHK" Then ' transmission checksum error, send again
TransmittedOK =
False
ResultEvent.Set()
AddProgress(
"Checksum error")
MainForm.DebugInfo.AddToLog(SerialBuffer)
Else ' message to be processed by the main app.
InstructionList.Items.Add(SerialBuffer)
End If
SerialBuffer =
""
ElseIf SingleChunk(Index) >= 32 Then ' add the charactor to the incomming data buffer
SerialBuffer = SerialBuffer & ThisCharactor
End If
Next Index
End Sub
So, how do I write a condition that only deals with the "$", "%", or nothing scenario listed above? ANY HELP WITH THIS WOULD BE VERY MUCH APPRECIATED!
Thanks,
javasource