Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. RS232 Communication

RS232 Communication

Scheduled Pinned Locked Moved Visual Basic
questionhardware
14 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Subjugate
    wrote on last edited by
    #1

    i got my control command to my external hardware. But the question now is i was unable to see the reply but i am sure there is a reply. Wat code should i insert to be able to see the reply?

    H B 2 Replies Last reply
    0
    • S Subjugate

      i got my control command to my external hardware. But the question now is i was unable to see the reply but i am sure there is a reply. Wat code should i insert to be able to see the reply?

      H Offline
      H Offline
      Heywood
      wrote on last edited by
      #2

      why don't you try reading the data from the port? What kind of question is this?

      1 Reply Last reply
      0
      • S Subjugate

        i got my control command to my external hardware. But the question now is i was unable to see the reply but i am sure there is a reply. Wat code should i insert to be able to see the reply?

        B Offline
        B Offline
        Bharat Jain
        wrote on last edited by
        #3

        Please be clear as to what are you using, for now i am assuming that you you are using serial port component that comes with .net to revive any thing you must add an event handler for DataRecived event of serialPort component You can the use serialPort1.ReadExisting() or serialPort.Read(), method to read from the serial port Here is an link to a similar thread , related to receiving data from a serial port. http://www.codeproject.com/script/Forums/View.aspx?fid=1649&msg=2897853[^] Let me know if you need any more info

        -Regards Bharat Jain bharat.jain.nagpur@gmail.com

        S 1 Reply Last reply
        0
        • B Bharat Jain

          Please be clear as to what are you using, for now i am assuming that you you are using serial port component that comes with .net to revive any thing you must add an event handler for DataRecived event of serialPort component You can the use serialPort1.ReadExisting() or serialPort.Read(), method to read from the serial port Here is an link to a similar thread , related to receiving data from a serial port. http://www.codeproject.com/script/Forums/View.aspx?fid=1649&msg=2897853[^] Let me know if you need any more info

          -Regards Bharat Jain bharat.jain.nagpur@gmail.com

          S Offline
          S Offline
          Subjugate
          wrote on last edited by
          #4

          Sorry for the unclear question. Ya wat i was trying to do was to retrieve data from my serial comm port. I am able to send to the comm port but not able to receive. I already seen the link u posted. I seen to be wat i am looking for but only ting is in C#. So i was wondering if u have the .net version?

          B 1 Reply Last reply
          0
          • S Subjugate

            Sorry for the unclear question. Ya wat i was trying to do was to retrieve data from my serial comm port. I am able to send to the comm port but not able to receive. I already seen the link u posted. I seen to be wat i am looking for but only ting is in C#. So i was wondering if u have the .net version?

            B Offline
            B Offline
            Bharat Jain
            wrote on last edited by
            #5

            this is the same code converted in vb

            Private Sub SerialPort1_DataReceived(ByVal sender As
            System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)
            Handles SerialPort1.DataReceived
            If SerialPort1.BytesToRead > 0 Then
            Dim byteToRead As Integer
            byteToRead = SerialPort1.BytesToRead
            Dim byteArray(byteToRead) As Byte
            Dim lineData As String
            SerialPort1.Read(byteArray, 0, byteToRead)
            lineData = ConvertToString(byteArray)
            End If
            End Sub
            Private Function ConvertToString(ByVal byteArray As Byte()) As String
            Dim tempStringToReturn As String = ""
            For Each bt As Byte In byteArray
            tempStringToReturn = tempStringToReturn + Convert.ToChar(bt)
            Next
            Return tempStringToReturn
            End Function

            hope this helps ...........

            -Regards Bharat Jain bharat.jain.nagpur@gmail.com

            B S 2 Replies Last reply
            0
            • B Bharat Jain

              this is the same code converted in vb

              Private Sub SerialPort1_DataReceived(ByVal sender As
              System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)
              Handles SerialPort1.DataReceived
              If SerialPort1.BytesToRead > 0 Then
              Dim byteToRead As Integer
              byteToRead = SerialPort1.BytesToRead
              Dim byteArray(byteToRead) As Byte
              Dim lineData As String
              SerialPort1.Read(byteArray, 0, byteToRead)
              lineData = ConvertToString(byteArray)
              End If
              End Sub
              Private Function ConvertToString(ByVal byteArray As Byte()) As String
              Dim tempStringToReturn As String = ""
              For Each bt As Byte In byteArray
              tempStringToReturn = tempStringToReturn + Convert.ToChar(bt)
              Next
              Return tempStringToReturn
              End Function

              hope this helps ...........

              -Regards Bharat Jain bharat.jain.nagpur@gmail.com

              B Offline
              B Offline
              Bharat Jain
              wrote on last edited by
              #6

              Please make note that i have not compiled the code , just did a manual conversion .......... But i think it should work without any problems . If you find any part which is not understandable please feel free to ask.

              -Regards Bharat Jain bharat.jain.nagpur@gmail.com

              1 Reply Last reply
              0
              • B Bharat Jain

                this is the same code converted in vb

                Private Sub SerialPort1_DataReceived(ByVal sender As
                System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs)
                Handles SerialPort1.DataReceived
                If SerialPort1.BytesToRead > 0 Then
                Dim byteToRead As Integer
                byteToRead = SerialPort1.BytesToRead
                Dim byteArray(byteToRead) As Byte
                Dim lineData As String
                SerialPort1.Read(byteArray, 0, byteToRead)
                lineData = ConvertToString(byteArray)
                End If
                End Sub
                Private Function ConvertToString(ByVal byteArray As Byte()) As String
                Dim tempStringToReturn As String = ""
                For Each bt As Byte In byteArray
                tempStringToReturn = tempStringToReturn + Convert.ToChar(bt)
                Next
                Return tempStringToReturn
                End Function

                hope this helps ...........

                -Regards Bharat Jain bharat.jain.nagpur@gmail.com

                S Offline
                S Offline
                Subjugate
                wrote on last edited by
                #7

                So i should create an event as well right? By the way does the code u posted shows where the data will be display if the event is trigger? Cos i not sure if it is me that missed out anything, i cant find the displaying part.. thanks.

                B 1 Reply Last reply
                0
                • S Subjugate

                  So i should create an event as well right? By the way does the code u posted shows where the data will be display if the event is trigger? Cos i not sure if it is me that missed out anything, i cant find the displaying part.. thanks.

                  B Offline
                  B Offline
                  Bharat Jain
                  wrote on last edited by
                  #8

                  The current code does not contain the code to display any data :( The data is collected in the variable linedata , you can use message box (Messagebox.show(linedata)) to display this variable or else you can use any other method to display data (data is in the variable linedata). Also i have used the event 'DataRecived' of serialport component. Are you facing any problem with the event, just let me know the issue , may be i can suggest a solution for the same.

                  -Regards Bharat Jain bharat.jain.nagpur@gmail.com

                  S 1 Reply Last reply
                  0
                  • B Bharat Jain

                    The current code does not contain the code to display any data :( The data is collected in the variable linedata , you can use message box (Messagebox.show(linedata)) to display this variable or else you can use any other method to display data (data is in the variable linedata). Also i have used the event 'DataRecived' of serialport component. Are you facing any problem with the event, just let me know the issue , may be i can suggest a solution for the same.

                    -Regards Bharat Jain bharat.jain.nagpur@gmail.com

                    S Offline
                    S Offline
                    Subjugate
                    wrote on last edited by
                    #9

                    hi.. Thanks for the info. Current already tried the code and the error "cross-thread" occurred. Beside ur code i oso added a line which is txtCommand.text = lineData, from copy the string of data received to be display on the textbox and tis line causes the error. Any way to solved tis error??:confused:

                    B 1 Reply Last reply
                    0
                    • S Subjugate

                      hi.. Thanks for the info. Current already tried the code and the error "cross-thread" occurred. Beside ur code i oso added a line which is txtCommand.text = lineData, from copy the string of data received to be display on the textbox and tis line causes the error. Any way to solved tis error??:confused:

                      B Offline
                      B Offline
                      Bharat Jain
                      wrote on last edited by
                      #10

                      just try this line in form load system.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = False this is not the best of the choice , but a quick solution , the error you are getting is because some of your control is been used in two different threads , i don't have any idea as to where any why you are using threads and hence , this is the only solution i can think of know :)

                      -Regards Bharat Jain bharat.jain.nagpur@gmail.com

                      S 1 Reply Last reply
                      0
                      • B Bharat Jain

                        just try this line in form load system.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = False this is not the best of the choice , but a quick solution , the error you are getting is because some of your control is been used in two different threads , i don't have any idea as to where any why you are using threads and hence , this is the only solution i can think of know :)

                        -Regards Bharat Jain bharat.jain.nagpur@gmail.com

                        S Offline
                        S Offline
                        Subjugate
                        wrote on last edited by
                        #11

                        Yup.. This is the solution that i am using now. But i encounter something which i find very weird. After i use the checkforillegalCrossthreadcalls = false, i put breakpoints at certain part of my code and it seens to work as i can see the reply from my hardware. But when i remove all my breakpoints the cross thread error did not occur but there isnt any reply from my hardware as well. Have been trying to find the reason behind it but till now there is still isnt any explanation.

                        B 1 Reply Last reply
                        0
                        • S Subjugate

                          Yup.. This is the solution that i am using now. But i encounter something which i find very weird. After i use the checkforillegalCrossthreadcalls = false, i put breakpoints at certain part of my code and it seens to work as i can see the reply from my hardware. But when i remove all my breakpoints the cross thread error did not occur but there isnt any reply from my hardware as well. Have been trying to find the reason behind it but till now there is still isnt any explanation.

                          B Offline
                          B Offline
                          Bharat Jain
                          wrote on last edited by
                          #12

                          This may happen because the output received from the serial port is been overridden by something going on in different thread ,Cross thread mainly occur when we try to access an object from two different treads ,by setting checkforillegalCrossthreadcalls = false we just suppress the error , but still the different thread continue to access the same object , when we apply break points , the other thread is suspended for that time and hence the application seems to work, i think you may have implemented threading in you project , you need to make sure that the all objects are used wisely , the same object must not be used in two different threads . I could have help you more if i have visibility to threading you have implemented.

                          -Regards Bharat Jain bharat.jain.nagpur@gmail.com

                          S 1 Reply Last reply
                          0
                          • B Bharat Jain

                            This may happen because the output received from the serial port is been overridden by something going on in different thread ,Cross thread mainly occur when we try to access an object from two different treads ,by setting checkforillegalCrossthreadcalls = false we just suppress the error , but still the different thread continue to access the same object , when we apply break points , the other thread is suspended for that time and hence the application seems to work, i think you may have implemented threading in you project , you need to make sure that the all objects are used wisely , the same object must not be used in two different threads . I could have help you more if i have visibility to threading you have implemented.

                            -Regards Bharat Jain bharat.jain.nagpur@gmail.com

                            S Offline
                            S Offline
                            Subjugate
                            wrote on last edited by
                            #13

                            Thanks for the info i think the breakpoint issue should be related to the threading problem as well.. btw tis is my code.. i didnt use any thread but dun know y i encounter the problem.. Private Delegate Sub SerialPort1_DataReceivedDelegate(ByVal sender As System.Object) Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles port.DataReceived If port.BytesToRead > 0 Then CheckForIllegalCrossThreadCalls = False Dim bytetoread As Integer bytetoread = port.BytesToRead Dim byteArray(bytetoread) As Byte Dim lineData As String port.Read(byteArray, 0, bytetoread) lineData = ConvertToString(byteArray) TxtCommand.Text = lineData End If End Sub Private Function ConvertToString(ByVal byteArray As Byte()) As String Dim tempStringToReturn As String = "" For Each bt As Byte In byteArray tempStringToReturn = tempStringToReturn + Convert.ToChar(bt) Next Return tempStringToReturn End Function

                            modified on Wednesday, February 25, 2009 10:46 PM

                            B 1 Reply Last reply
                            0
                            • S Subjugate

                              Thanks for the info i think the breakpoint issue should be related to the threading problem as well.. btw tis is my code.. i didnt use any thread but dun know y i encounter the problem.. Private Delegate Sub SerialPort1_DataReceivedDelegate(ByVal sender As System.Object) Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles port.DataReceived If port.BytesToRead > 0 Then CheckForIllegalCrossThreadCalls = False Dim bytetoread As Integer bytetoread = port.BytesToRead Dim byteArray(bytetoread) As Byte Dim lineData As String port.Read(byteArray, 0, bytetoread) lineData = ConvertToString(byteArray) TxtCommand.Text = lineData End If End Sub Private Function ConvertToString(ByVal byteArray As Byte()) As String Dim tempStringToReturn As String = "" For Each bt As Byte In byteArray tempStringToReturn = tempStringToReturn + Convert.ToChar(bt) Next Return tempStringToReturn End Function

                              modified on Wednesday, February 25, 2009 10:46 PM

                              B Offline
                              B Offline
                              Bharat Jain
                              wrote on last edited by
                              #14

                              No Clue !! , it does not seem that any threading is used in the code you pasted , i am really not able to think of any other cause of problem :(

                              -Regards Bharat Jain bharat.jain.nagpur@gmail.com

                              1 Reply Last reply
                              0
                              Reply
                              • Reply as topic
                              Log in to reply
                              • Oldest to Newest
                              • Newest to Oldest
                              • Most Votes


                              • Login

                              • Don't have an account? Register

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • World
                              • Users
                              • Groups