serialPort receive problem

  • Thread starter Thread starter tzhumi
  • Start date Start date
T

tzhumi

Hi all,

I have a class that creates an instance of the seril Port.
Every thing works fine except whenever I receive data I cannot display
the
recieved data. I get no errors but the recived data seems to be empty.
when I try to display it in a text box nothing happens.
I have socket stuff in the same class and it's recieve callbacks work
just
fine and displays just fine.

Here is my code:

Sub OpenComPort()
Try

If Not SerialPort1.IsOpen Then
SerialPort1.PortName = cmbPorts.SelectedItem.ToString

If cmbBitRate.SelectedIndex > 0 Then
SerialPort1.BaudRate = CInt
(cmbBitRate.SelectedItem)
End If

SerialPort1.Parity = Parity.None
SerialPort1.DataBits = 8
SerialPort1.StopBits = StopBits.One
SerialPort1.Handshake = Handshake.None
SerialPort1.Open()

End If

Catch ex As Exception
MessageBox.Show(ex.Message)

End Try
End Sub

Private Sub btnOn_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnOn.Click
SendCommand("I2" + eot)
End Sub

Private Sub SendCommand(ByVal command As String)

Dim response As String = ""
SerialPort1.Write(command)
response = SerialPort1.ReadExisting
textbox1.text=response
End sub

When I send the command the controller responds to the command and it
should return the response by some ascii characters. But I am unable
to display the response from it...Thank you for any help
 
The ReadExisting method will return whatever data is available at the serial
port. If nothing is displayed then you should assume that nothing was
available. That would be expected, as the serial port data could not have
been transmitted, and a response received, in the time that you have allowed
between the Write command and the ReadExisting call.

Data will be available at the serial port if the BytesToRead function
returns a positive value. You should call ReadExisting only after
confirming that there are bytes to be read, or following a DataReceived
event.
 
tzhumi said:
Hi all,

Private Sub btnOn_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnOn.Click
SendCommand("I2" + eot)
End Sub


When I send the command the controller responds to the command and it
should return the response by some ascii characters. But I am unable
to display the response from it...Thank you for any help

What is "I2"?

What is "eot"

Looks like an AT I# command. If so, there are 2-3 things here
possible to consider:

1) Send "ATI2"

2) If EOT is not <CR>, than send "ATI2"+chr(13)

3) The move might not be an VERBOSE response mode and needs to be
initialized. To see responses, the standard initialization for all
HAYES AT command set compatible modems are:

ATQ0V1X4

See http://en.wikipedia.org/wiki/Hayes_command_set

--
 
What is "I2"?

What is "eot"

Looks like an AT I# command.  If so, there are 2-3 things here
possible to consider:

1) Send "ATI2"

2) If EOT is not <CR>, than send "ATI2"+chr(13)

3) The move might not be an VERBOSE response mode and needs to be
initialized. To see responses, the standard initialization for all
HAYES AT command set compatible modems are:

    ATQ0V1X4

Seehttp://en.wikipedia.org/wiki/Hayes_command_set

--

They are the commands for our controller
When I send that command it will give the response...
 
Hi,

You must wait until all data that you need are present. The ReadExisting
method simple reads all data that are available when called -- and nothing
will yet have been received from your device; this code will execute much
faster than the device can respond. You can test for the number of
characters that are available by using the SerialPort.BytesToRead method.

Dick

--
Richard Grier, MVP
Hard & Software
Author of Visual Basic Programmer's Guide to Serial Communications, Fourth
Edition,
ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March
2006.
See www.hardandsoftware.net for details and contact information.
 
Wait a time before read the port, and write commands with end line.

For write is something like:

Private Sub SendCommand(ByVal command As String)
SerialPort1.Write(command & vbcrlf)
End sub

For read, you can wait a specific time, or read in the event of the Com
port, something like:

Private Sub Serial_Recibir(ByVal sender As System.Object,
ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) _
Handles Serial.DataReceived
Msgbox(Me.Serial.ReadExisting())
End Sub

Try with that, maybe is a good solution for you.

Freddy Coal
 
Hi.
Did any of you solved the problem?
I write to port:
comPort.WriteLine("saas" + "\n");
wait some time:
Thread.Sleep(20000);
and trying to read again:
int intBytes = comPort.BytesToRead;
string asdas = comPort.ReadExisting();
but nothing... empty string.

Do I miss something here?
 
Back
Top