|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
serialPort receive problemI 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. Show quoteHide quote "tzhumi" <fren***@googlemail.com> wrote in message news:f732599c-4d3e-48bb-9888-678782754a2a@w3g2000yqf.googlegroups.com... > 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 tzhumi wrote:
> Hi all, What is "I2"?> > 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 "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 -- On Jun 17, 10:47 am, Mike <unkn***@unknown.tv> wrote:
Show quoteHide quote > tzhumi wrote: They are the commands for our controller> > 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 > > Seehttp://en.wikipedia.org/wiki/Hayes_command_set > > -- 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 Show quoteHide quote "tzhumi" <fren***@googlemail.com> wrote in message news:f732599c-4d3e-48bb-9888-678782754a2a@w3g2000yqf.googlegroups.com... > 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
Visual Basic is Dead!
Blanking Monitor and Unblanking Monitor Optional localizing Frustrated, Bothered and Bewildered (Form AutoScroll Broken) Merging Bitmaps What are the Versions of Visual Studio 2005 and what Features are Available in Each? Help with SOAP3 - OT Bindingsource problem prevent control events from firing before form is loaded QBasic 4.5 modules in VB.Net? |
|||||||||||||||||||||||