RS232 Communication over serial port

L

Thread Starter

llo

<p>Hi,

<p>I am interfacing an analyzer to LIS. I am trying to access the data using a serial port.

<p>I am successful in opening the port, but not able to read the contents. I would be very grateful if any one could assist me. Your feedback will be highly appreciated.

<p>The code I have written is as follows:
<pre>
MSComm1.CommPort = 1
MSComm1.Settings = "9600,N,8,1"
MSComm1.InputLen = 0
MSComm1.PortOpen = True
If Not MSComm1.PortOpen Then
MsgBox ("PORT NOT OPEN")
End If

Do Until False
DoEvents
Buf = MSComm1.Input ' Contents of the buffer
</pre>
 
Hi,

Maybe it can be very basic comment but this a probability every time:

Did you check your hardware? First you can try your serial port with another device connection. If your serial port OK (Bec. serial ports can damage easily with any wrong connection, etc.), secondly you can test your connection cable. Measure all connections between two end connectors.
Third comment can be check communication protocol of the device which you can trying to communicate.

May it be easy...
 
N

Neil Kingston (Data Layers)

<p>Hello llo,

<p>The Microsoft help file provides the following example which it looks like you've based your code upon:
<pre>
Private Sub Form_Load ()
' Buffer to hold input string
Dim Instring As String
' Use COM1.
MSComm1.CommPort = 1
' 9600 baud, no parity, 8 data, and 1 stop bit.
MSComm1.Settings = "9600,N,8,1"
' Tell the control to read entire buffer when Input
' is used.
MSComm1.InputLen = 0
' Open the port.
MSComm1.PortOpen = True
' Send the attention command to the modem.
MSComm1.Output = "ATV1Q0" & Chr$(13) ' Ensure that
' the modem responds with "OK".
' Wait for data to come back to the serial port.
Do
DoEvents
Buffer$ = Buffer$ & MSComm1.Input
Loop Until InStr(Buffer$, "OK" & vbCRLF)
' Read the "OK" response data in the serial port.
' Close the serial port.
MSComm1.PortOpen = False
End Sub
</pre>
<p>It looks like you are missing a few vital pieces. First of all, you are not sending a request to the device. Consult the manual for the analyzer to determine what is the appropriate request, for example to read data. The MS example sends the following line:
MSComm1.Output = "ATV1Q0" & Chr$(13)

<p>You need something similar, unless the analyzer automatically spews out the data continuously.

<p>Secondly, you are not trapping the response data in your example. MS do it like this:
<pre>
Do
DoEvents
Buffer$ = Buffer$ & MSComm1.Input
Loop Until InStr(Buffer$, "OK" & vbCRLF)
</pre>
<p>Your equivalent code is not going to do much as you have a Do without closing the loop block:
<pre>
Do Until False
DoEvents
Buf = MSComm1.Input ' Contents of the buffer
</pre>
<p>If there is going to be ongoing data acquisition, you should also consider trapping the input data by using the OnComm event:
<pre>
Private Sub MSComm1_OnComm()
Dim strBuff As String
If MSComm1.CommEvent = comEvReceive Then
strBuff = MSComm1.Input
Debug.Print strBuff
End If
End Sub
</pre>
<p>Also have a look at this previous post:
http://www.control.com/1026194998/index_html

<p>Regards,<br>
Neil Kingston<br>
Data Layers Ltd<br>
http://www.opcware.com
 
Top