View previous topic :: View next topic |
Author |
Message |
TL
Joined: 15 Sep 2003 Posts: 75
|
Off Topic: VB MSCOMM question |
Posted: Wed Jan 14, 2004 7:05 am |
|
|
Hi,
I�m trying to receive 4 bytes of �char� data (eg, �$�, �FF�, �01�, �ED�) in MSCOMM from the pic. A command button in VB can be pressed to send them all back to the PIC. I have the following VB6 code but I�ve been getting compiling errors in VB. I�m new to VB. Could someone please amend the code under �Private Sub MSComm1_OnComm()� and �Command1_Click()� to make it work for me. Also, how do I save and retrieve the 4 bytes onto the PC hard disk? Thanks.
� Send button
Private Sub Command1_Click()
MSComm1.Output = <TBA>
End Sub
Private Sub Form_Load()
MSComm1.CommPort = 1
MSComm1.Settings = "9600,N,8,1"
MSComm1.InputLen = 4
MSComm1.PortOpen = True
MSComm1.RThreshold = 4
End Sub
Private Sub MSComm1_OnComm()
Dim lDataLength As Long
Dim sData As String
Dim sChar(5) As String
Dim x As Long
If (MSComm1.CommEvent = comEvReceive) Then
sData = MSComm1.Input
lDataLength = Len(sData)
For x = 1 To lDataLength
sChar(x) = Mid$(sData, x, 1)
Next x
End If
End Sub |
|
|
jamesjl
Joined: 22 Sep 2003 Posts: 52 Location: UK
|
|
Posted: Wed Jan 14, 2004 7:52 am |
|
|
Your code seemed to work pretty well. I changed the order of a few things and added a text box the the form so that I could see the contents of sData. THe code is below:
Code: |
Private Sub Form_Load()
MSComm1.CommPort = 2
MSComm1.Settings = "9600,N,8,1"
MSComm1.Handshaking = comNone
MSComm1.RThreshold = 4
MSComm1.PortOpen = True
End Sub
Private Sub MSComm1_OnComm()
Dim lDataLength As Long
Dim sData As String
Dim sChar(5) As String
Dim x As Long
If (MSComm1.CommEvent = comEvReceive) Then
sData = MSComm1.Input
lDataLength = Len(sData)
For x = 1 To lDataLength
sChar(x) = Mid$(sData, x, 1)
Next x
Text1.Text = sData
WriteToFile (sData)
End If
End Sub
Private Sub WriteToFile(sData As String)
Dim sFName As String
sFName = "C:\PICData.dat"
Open sFName For Append As #1
Print #1, sData
Close #1
End Sub
|
Could you please let us know exactly what errors you are getting from the compiler so that we can take another look at it.
Regards,
Jason |
|
|
TL
Joined: 15 Sep 2003 Posts: 75
|
|
Posted: Wed Jan 14, 2004 9:05 am |
|
|
Jason,
Thanks very much for your time. BTW, both TX and RX are on COM1. How do I pass the sChar() data into the Command1_Click() function (command button) ? I have tried writing the following code but I got a compile error:
Compile error:
"Procedure declaration does not match description of event or procedure having the same name." The "Private Sub Command1_Click(sChar() As Long)" was highlighted.
Code: | ' Send button
Private Sub Command1_Click(sChar() As Long)
Dim y As Long
For y = 1 To 4
MSComm1.Output = sChar(y)
Next y
End Sub |
I think sChar() need to be converted into ASCII characters before they can be sent?
The incoming data is displayed correctly in the text box and the data file is writing to the hard disk.
Thanks
Tommy |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Wed Jan 14, 2004 10:10 am |
|
|
You cannot change the declaration for the control. You would need to use a global var to do it. |
|
|
TL
Joined: 15 Sep 2003 Posts: 75
|
|
Posted: Wed Jan 14, 2004 10:25 am |
|
|
Thanks Mark. Can you please give me more details as I'm only a beginner in VB? Thanks. |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Wed Jan 14, 2004 12:26 pm |
|
|
Declare this at the top of the file
Dim sChar(5) As String
Then your code should be:
Code: |
' Send button
Private Sub Command1_Click()
Dim y As Long
For y = 1 To 4
MSComm1.Output = sChar(y)
Next y
End Sub
|
|
|
|
|