RS232 Communication
-
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?
-
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?
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
-
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
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?
-
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?
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 Functionhope this helps ...........
-Regards Bharat Jain bharat.jain.nagpur@gmail.com
-
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 Functionhope this helps ...........
-Regards Bharat Jain bharat.jain.nagpur@gmail.com
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
-
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 Functionhope this helps ...........
-Regards Bharat Jain bharat.jain.nagpur@gmail.com
-
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.
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
-
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
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:
-
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:
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
-
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
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.
-
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.
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
-
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
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
-
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
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