Results 1 to 40 of 76

Thread: [RESOLVED] Can I send data via com ports?

Threaded View

  1. #11
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Can I send data via com ports?

    Ok the cable sounds good.
    Here's some code, obviously not tested but compiles clean. There are 2 Forms:

    frmReceiver with a CommandButton named cmdStart and an MSComm control, named MSComm1 it uses CommPort 2. This form should be the start-up form for the project.
    Code:
    '
    ' Simple Text File Transfer using Serial Port
    ' Receiver Code
    '
    Option Explicit
    Private boTransfer As Boolean
    Private intFile As Integer
    
    Private Sub cmdStart_Click()
    '
    ' Load and position the Sender form
    '
    Load frmSender
    frmSender.Visible = True
    frmSender.Caption = "Sender"
    frmSender.Left = Me.Left + Me.Width
    frmSender.Top = Me.Top
    End Sub
    
    Private Sub Form_Load()
    MSComm1.CommPort = 2
    MSComm1.Settings = "9600,N,8,1"
    MSComm1.RThreshold = 1
    MSComm1.PortOpen = True
    Me.Caption = "Receiver"
    End Sub
    
    Private Sub MSComm1_OnComm()
    Static strBuffer As String
    Static intLen As Integer
    Static intCount As Integer
    Dim strData As String
    Dim strRecord As String
    Dim strChar As String
    Dim lngPos As Long
    Dim intChar As Integer
    Dim boFinished As Boolean
    Dim strUnblock() As String
    Select Case MSComm1.CommEvent
        Case comEvReceive
            '
            ' At least RThreshold bytes are available to be read
            ' Read and append them to the buffer
            '
            strData = MSComm1.Input
            strBuffer = strBuffer & strData
            Select Case boTransfer
                Case False
                    '
                    ' We're in Command Mode
                    '
                    Do
                        lngPos = InStr(strBuffer, Chr(4))
                        '
                        ' There's at least 1 complete record
                        ' Unblock it
                        '
                        If lngPos > 0 Then
                            strRecord = Mid$(strBuffer, 1, lngPos - 1)
                            strUnblock = Split(strRecord, Chr(2))
                            Select Case strUnblock(0)
                                Case "FT"
                                    '
                                    ' File Transfer Command
                                    ' Save the file size and open a file to receive
                                    ' Switch the transfer flag on
                                    '
                                    intLen = CLng(strUnblock(1))
                                    Open "C:\MyTextRX.txt" For Output As intFile
                                    boTransfer = True
                                    '
                                    ' We can add other commands here
                                    '
                            End Select
                            '
                            ' Check if there's anything else in the buffer
                            ' if there is then move it to the front and
                            ' go round the loop.
                            ' Otherwise flush the buffer and exit
                            '
                            If lngPos < Len(strBuffer) Then
                                strBuffer = Mid$(strBuffer, lngPos + 1)
                            Else
                                strBuffer = ""
                                boFinished = True
                            End If
                        Else
                            '
                            ' Didn't find a complete record
                            ' exit and wait for the next comEvReceive event
                            '
                            boFinished = True
                        End If
                    Loop Until boFinished = True
                Case True
                        '
                        ' We're in Transfer mode
                        ' The data is being blocked by the sender (a line at a time)
                        ' Each block is terminated by a Chr(4)
                        ' so we accumulate the data until Chr(4) is received
                        ' and then write the complete record to the file,
                        ' flush the buffer, and ask the sender for the next block
                        '
                        Do
                            lngPos = InStr(strBuffer, Chr(4))
                            If lngPos > 0 Then
                                strRecord = Mid$(strBuffer, 1, lngPos - 1)
                                Put #intFile, , strRecord
                                intCount = intCount + Len(strRecord)
                                labProgress.Caption = CStr(intCount) & " Bytes Received"
                                DoEvents
                                If intCount >= intLen Then
                                    Close intFile
                                    boTransfer = False
                                    intLen = 0
                                    intCount = 0
                                    boFinished = True
                                End If
                                If lngPos < Len(strBuffer) Then
                                    strBuffer = Mid$(strBuffer, 1, lngPos + 1)
                                Else
                                    strBuffer = ""
                                    boFinished = True
                                End If
                            Else
                                boFinished = True
                            End If
                            
                        Loop Until boFinished = True
                        '
                        ' Processed this buffer so tell the sender
                        ' we're ready for the next one
                        '
                        MSComm1.Output = "OK" & Chr(4)
                        
                            
            End Select
    End Select
    End Sub
    frmSender with a CommandButton named cmdSend and an MSComm Control named MSComm1 and uses CommPort 1
    Code:
    '
    ' Simple Text File transfer using Serial Port
    ' Sender Code
    '
    Option Explicit
    Private boTransfer As Boolean
    Private intFile As Integer
    
    Private Sub cmdSend_Click()
    Dim intLen As Integer
    intFile = FreeFile
    Open "C:\MyText.txt" For Input As intFile
    intLen = LOF(intFile)
    MSComm1.Output = "FT" & Chr(2) & CStr(intLen) & Chr(4)
    End Sub
    
    Private Sub Form_Load()
    MSComm1.CommPort = 1
    MSComm1.Settings = "9600,N,8,1"
    MSComm1.RThreshold = 1
    MSComm1.PortOpen = True
    End Sub
    
    Private Sub MSComm1_OnComm()
    Dim strFileData As String
    Dim strData As String
    Dim strRecord As String
    Static strBuffer As String
    Dim lngPos As Long
    Dim boFinished As Boolean
    Select Case MSComm1.CommEvent
        Case comEvReceive
            strData = MSComm1.Input
            strBuffer = strBuffer & strData
            Do
                lngPos = InStr(strBuffer, Chr(4))
                If lngPos > 0 Then
                    strRecord = Mid$(strBuffer, 1, lngPos - 1)
                    Select Case strRecord
                        Case "OK"
                            '
                            ' Every time we get an "OK" from the receiver
                            ' send the next line of the file
                            '
                            If Not EOF(intFile) Then
                                Line Input #intFile, strFileData
                                '
                                ' Since we're only ever sending text files
                                ' we need to add the vbCrLf
                                ' since Line Input will have stripped them off
                                '
                                MSComm1.Output = strFileData & vbCrLf & Chr(4)
                                labProgress.Caption = CStr(Len(strFileData) + 2) & " Bytes Sent"
                                DoEvents
                            Else
                                Close intFile
                            End If
                    End Select
                    If lngPos < Len(strBuffer) Then
                        strBuffer = Mid$(strBuffer, 1, lngPos + 1)
                    Else
                        strBuffer = ""
                        boFinished = True
                    End If
                Else
                    boFinished = True
                End If
            Loop Until boFinished = True
    End Select
    End Sub
    Run the Project and click the Start Button on the Receiver, the Sender form should appear next to it. Click on the Send button on the Sender and the transfer should take place. It sends C:\MyText.txt to C:\MyTextRX.txt but obviously you can change the names etc.

    I suspect (know) there'll be a few problems (I've never got anything to work first time as far as I can remember) so let me kow what problems you encounter

    Disclaimer: This implementation is *very* inefficient, since it is sending a byte at a time and the Receiver sends more data than the Sender, and shouldn't be used 'in anger'. It is meant purely as an example of the logic involved. A 'proper implementation' would use Byte Arrays and buffering of data. Perhaps I'll tidy it up a bit.
    Last edited by Doogle; Jan 3rd, 2008 at 02:42 AM. Reason: Corrected a few obvious errors

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width