Click to See Complete Forum and Search --> : MSComm Firing Too Much!! Why?
PhilipG
Dec 5th, 1999, 01:27 AM
Hello, I have built an external device that transmits ASCII characters 0,1,2,3,4,5,6,7,8,9, and * via a serial connection. Using hyperterminal, I can connect to my device and indeed my device is transfering the characters correctly. I need MSComm to fire EACH time one character has been recieved.
Now, I have a MSComm Control, here is the settings I have used:
With comKeypad
.CommPort = CInt(sSetting)
.Settings = "9600N,8,1"
.InputLen = 1
.DTREnable =False
.EOFEnable = False
.RTSEnable = False
.Handshaking = comNone
.RThreshold = 1
.SThreshold = 0
End With
Now, since my device used NO handshaking signals, OnCOMM should fire everytime one single character is recived. Inside of OnCOMM(), I'm incrementing a variable and printing it to the debug window so I can keep track of how many times OnCOMM fires. Unfortunately, OnCOMM is firing 4-7 times per character. It correctly sends the one character to, say a text box, but still, the event fired way too many times. This is a big problem for me. I need to have it either fire only once, or fire a consistant number of times for each character.
So what could be the deal? Or maybe did I set the connection up wrong?
ComputerGuy
Dec 5th, 1999, 09:01 PM
Hello Philip,
I need more info about your project.
1- Do you know the amount of byte send between your external device and the computer.
2- Does all byte sent through the COM port are ASCII.
3- Does the software request a command to the external device or is in the waiting state. (like embedded system)
On my side I only use the on_comm_event to detect error in the communication.
I use a subroutine, it's faster about 5 times faster. This only depends on speed issue for your project.
Let me know,
Regards,
Sebastian
basgauthier@videotron.ca
ComputerGuy
Dec 6th, 1999, 02:21 AM
There 2 ways to solve that problem.
If you still want to use the on_comm_event
there a property on the mscomm1 that is call inputlen. Set it to 1 and it will can the on_comm_event everytime one character is in the input buffer of the COM port. This should work.
The input mode has to be in ascii also.
(I guest you know about this, it's just because you did not mention it)
They other way you can do it, is to loop the software.
Something like:
Sub InBuffer()
Do
Doevents
loop until mscomm1.inputbuffer >= 1
data = mscomm1.input
Call YourSubroutine(data) 'For calculation or conversion
Exit sub
You can use a timer to call that function or do something similar.
Hope that help,
I you want, you can send me the code and I will have a look.
basgauthier@videotron.ca
PhilipG
Dec 6th, 1999, 11:15 AM
Thanks Sabastian:
Yes my design is an embedded project, using a 8051 to transmit ASCII bytes to the PC. I really need to be able to count exactly how many bytes have been transfered. I tried the incrementing an integer thing, but as I noted, that isn't accurate at all.
I do not know how many bytes will be sent between my device and the PC. Think of it as a keypad interfaced to the 8051. Everytime a user presses a key, it sends one byte to the PC. The software IS in a waiting state. I'm not "getting" the data from the device. The device sends it and I need to be able to detect it. Have any suggestions?
I really appreciate you taking any time you can in helping me out. This is part of our senior project and very fundamental to our project. I did not anticipate the problems with MSComm.
Thanks!!
kinjalgp
Dec 24th, 2001, 01:30 PM
I think there is a problem in your 8051 serial port initialization. This could happen if the baud rates are different. Thus the on_comm_event is fired when there is a change in the state of the serial port register but since ther is a speed mismatch only some characters are detected and displayed. Rest becomes unknown to you PC.
:D Kinjal :D
choonchin
Jan 18th, 2002, 11:23 PM
Hai, philip G. i wander did u solve the problem because i face the same problem as your's. ComputerGuy, Can u provide me with some help here, please.
Here is my situation. i got a application that will wait for a signal from my 8051 external device, when the user press a push button on my external device, my 8051 external device will send a text "R" to my application , and the OnComm_() will detect the input and fire a record function to record voice for the line in of my PC sound card. Then when the user finish recording, user need to push the switch again and the 8051 external device will send a "S" text to the serial port and again the OnComm_() will detect the input and fire a function to stop the recording. Here is my code but it is not working, can u help me out.;
Private Sub Form_Load()
If MSComm1.PortOpen = False Then MSComm1.PortOpen = True
End Sub
Private Sub MSComm1_OnComm()
Dim Dummy As String
Select Case MSComm1.CommEvent
'Events
Case comEvReceive
MSComm1.InputLen = 0
Dummy = MSComm1.Input
If Dummy = "R" Then
Do
'Open a WaveAudio called Record
mciSendString "open new type waveaudio Alias record", 0&, 0, 0
'Start Recording
mciSendString "record record", 0&, 0, 0
Loop
End If
Dim Dummy2 As String
Select Case MSComm1.CommEvent
'Events
Case comEvReceive
MSComm1.InputLen = 0
Dummy2 = MSComm1.Input
If Dummy2 = "S" Then
Do
'Stops recording
mciSendString "stop record", 0&, 0, 0
'Saves the sound
mciSendString "save record C:\Windows\Desktop\MyWav.wav", 0&, 0, 0
'Close "Record"
mciSendString "close record", 0&, 0, 0
Loop
End If
end Sub
Hope u can help me with the code. The test the code, but it is not firing when i try to send a text from my 8051 external device to the serial port. I check the device and it is fine. Please help me. can u mail me ur code about ur project. Thanks for your time .and have a nice day.
kinjalgp
Jan 20th, 2002, 05:56 AM
Hey buddy!
I think you got the same problem as PhilipG. Again I'll suggest you that you check the baud rates of your device and that of your MSComm. You code shows that you have not adjusted baud rate anywhere. That means you are using the default setting of MSComm is 9600 bauds. If your device has baud other than this, MSComm will mis the "R" or "S" characters send by your device. Please check it and tell me. If both are same, I'll think of other possibilities.
Also one more thing : Change the foloowinf Do-Loop for testing purpose and see if the event is fired or not.
Do
'Open a WaveAudio called Record
'mciSendString "open new type waveaudio Alias record", 0&, 0, 0
'Start Recording
'mciSendString "record record", 0&, 0, 0
Debug.Print "I got it!"
Loop
Hope this helps you!
:D Kinjal :D
choonchin
Jan 21st, 2002, 11:16 AM
Thanks, kinjalgp for the reply. Checked the baud rate, it is fine. i got my function done with a timer. Here is my code:
Private Sub Timer1_Timer()
If comEvReceive Then
Text2.Text = MSComm2.Input
If Text2.Text = "START RECORD" Then
'Open a WaveAudio called Record
mciSendString "open new type waveaudio Alias record", 0&, 0, 0
'Start Recording
mciSendString "record record", 0&, 0, 0
Else
If Text2.Text = "THANKS YOU" Then
'Stops recording
mciSendString "stop record", 0&, 0, 0
'Saves the sound
mciSendString "save record C:\Windows\Desktop\MyWav.wav", 0&, 0, 0
'Close "Record"
mciSendString "close record", 0&, 0, 0
End If
End If
End If
End Sub
but i face a new problem here. The save function here only allow me to save one wave file. what i need is that the save function will assign a new file name to the recorded wave file like file1, file2, file3 ect and save all of them after each recording. Can i do this, can u provide me with some caode. Thanks, Mate
kinjalgp
Jan 23rd, 2002, 10:00 AM
No Big deal in that buddy!
Here is yor modified code:
'At the beginning of the Form Code
Dim Counter as Long
Private Sub Timer1_Timer()
If comEvReceive Then
Text2.Text = MSComm2.Input
If Text2.Text = "START RECORD" Then
'Open a WaveAudio called Record
mciSendString "open new type waveaudio Alias record", 0&, 0, 0
'Start Recording
mciSendString "record record", 0&, 0, 0
Else
If Text2.Text = "THANKS YOU" Then
'Stops recording
mciSendString "stop record", 0&, 0, 0
'Saves the sound
mciSendString "save record C:\Windows\Desktop\File" & Counter & ".wav", 0&, 0, 0
Counter=Counter+1
'Close "Record"
mciSendString "close record", 0&, 0, 0
End If
End If
End If
End Sub
:D Kinjal :D
choonchin
Jan 23rd, 2002, 07:53 PM
kinjalgp, thanks for the reply. Try ur code but it is not working. It keep replace the previous recorded file after i try it for a number of time. And at the end only one file had been recorded that is File0. Can u help me again. Thanks mate.
choonchin
Jan 23rd, 2002, 08:11 PM
Sorry kinjalgp. You code work great. Just that i'm the one that is stupid. I put the declaration of the Counter in the timer event that why it keep replacing the previous recorded file. It work when i put the declaration of the counter at the beginning of the Form Code. Stupid me:( :( . But i'm very happy u help me to get this done. Thanks A lot Mate. :D :D :D :D
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.