CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Off Topic: VB MSCOMM question

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
TL



Joined: 15 Sep 2003
Posts: 75

View user's profile Send private message

Off Topic: VB MSCOMM question
PostPosted: Wed Jan 14, 2004 7:05 am     Reply with quote

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

View user's profile Send private message Visit poster's website Yahoo Messenger MSN Messenger

PostPosted: Wed Jan 14, 2004 7:52 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Jan 14, 2004 9:05 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Wed Jan 14, 2004 10:10 am     Reply with quote

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

View user's profile Send private message

PostPosted: Wed Jan 14, 2004 10:25 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Wed Jan 14, 2004 12:26 pm     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group