-
Reeciev a single charcater (8 bits) from Serial port using Mscomm
Hi
I wish to receive single character from an electronic device connected to PC via serial comm . I have used MSCOMM control. See the code below
Mscomm_oncomm
Dim X As String
X = Convert.ToString(mscomm.input)
Dim substring As String = X.Substring(2, 1)
endsub
I am not receiving anything. However when I store the X variable into a file, I can see the charcater received. But I am interested in displaying the charcater rather than stroing into a binary file
Any help would be appreciated ?
Regards
Faisal
Last edited by pfaisalbe; 04-06-2011 at 11:15 AM.
-
I am surprised you don't get an error. you named your variable the same as one of the String objects methods.
Try using a different variable name.
Also did you realize that the Substring Methods Index is Zero based, therefore X.Substring(2, 1) returns the third character in the string.
There could also be non-printable characters in the input string, as a test, try looping through one character at a time until you find the character you are looking for.
-
Thanks Ron weller.
I am surprised you don't get an error. you named your variable the same as one of the String objects methods.
I won't be surprised , since this is ms product.
It works when I use the following lines
Mscomm_oncomm
If MSComm1.CommEvent = 2 Then
InBuff1 = MSComm1.Input
InBuff3 = InBuff3 & InBuff1
End if
If (InBuff1 = "A") Then
SENDDATA("Acknowledgment received")
End If
End sub
on the other hand If I use the following lines. It does not work
Mscomm_oncomm
If MSComm1.CommEvent = 2 Then
InBuff1 = MSComm1.Input
InBuff3 = MSComm1.Input
End if
If (InBuff3 = "A") Then
SENDDATA("Acknowledgment received")
End If
End sub
This put me in to ask question about how the Mscomm_ oncomm event is fired
For the serial communication ,
startbit , data bit and stopbit
startbit would be zero ,followed by data bit and start bit would be 1.
I guess
Oncomm event starts from start bit and finish at stop bit. This time is shorter than time needed to execute statement 2 from after statement 1 is execuited. So Inbuff3 should not have data , I am looking for
InBuff1 = MSComm1.Input // statement 1
InBuff3 = MSComm1.Input // statement 2
Please comment
Regards
Faisal
Last edited by pfaisalbe; 04-08-2011 at 07:12 AM.
-
Check out the InputLen property. It defaults to zero, which means to retreive the entire receive buffer. If you only want one character at a time then set it to one.
Mscomm1.InputLen = 1
Also there is InputMode, 0 retreives data as characters in a string, and 1 retreaves data as binary 8 bit values into a byte array.
You can also use the InBufferCount property to find out how many characters are in the receive buffer. Do this before you call Input and you can see if anything is there first.
One more property InBufferSize defaults to 1024, and is the size of the receive buffer. If you get alot of overflows you can increase this value. Of course you can avoid overflows by using handshaking.
-
One more thing...check here for information on the CommEvent.
http://msdn.microsoft.com/en-us/libr...05(VS.60).aspx
-
I do not program communication ports myself, but I am still puzzled as to why so many programmers still insist on using the 15 years old MSComm in .NET, with all the complications it seems to show (and with reasons, old VB6 stuff does not translate well into the .NET world) while there is a SerialPort control in the framework?
Jacques Bourgeois
JBFI
http://www3.sympatico.ca/jbfi/homeus.htm
-
Thanks Jacques Bourgeois
but I am still puzzled as to why so many programmers still insist on using the 15 years old MSComm in .NET, with all the complications it seems to show (and with reasons, old VB6 stuff does not translate well into the .NET world) while there is a SerialPort control in the framework?
I am not a professional programmer. My objective is to get the work done. I have tried using serial port control in vb.net. Unfortunately I was not successful , I have faced some issues in data format while sending data to Host PC. So I have reverted back to Mscomm.
Similar Threads
-
By pfaisalbe in forum .NET
Replies: 0
Last Post: 02-02-2011, 10:42 AM
-
By rciprogrammer in forum C++
Replies: 8
Last Post: 03-10-2009, 04:45 PM
-
By brumbarchris in forum VB Classic
Replies: 2
Last Post: 11-19-2008, 06:54 PM
-
Replies: 146
Last Post: 08-12-2002, 10:40 PM
-
By Rob Teixeira in forum .NET
Replies: 129
Last Post: 06-06-2002, 05:23 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Development Centers
-- Android Development Center
-- Cloud Development Project Center
-- HTML5 Development Center
-- Windows Mobile Development Center
|