Page 1 of 2 12 LastLast
Results 1 to 40 of 76

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

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Canada
    Posts
    297

    Resolved [RESOLVED] Can I send data via com ports?

    Is it possible to send data via the com port on my computer using the "MsComm1"..

    Is it possible for when I click a command button, it sends the information in Text1.Text via the com port where another system running a timer recives the data?

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

    Re: Can I send data via com ports?

    Yes, but remember that it's a serial (RS232) interface and you'll need an Application Protocol so that the two ends understand each other.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Canada
    Posts
    297

    Re: Can I send data via com ports?

    How do I go about doing it?

    I did some searching, would something like this work to send data:

    Code:
    Private Sub Send_Click()
    MSComm1.Output = "data_to_send_here"
    End Sub
    
    Private Sub Timer1_Timer()
    MSComm1.CommPort = 2
    MSComm1.Settings = "9600,N,8,1"
    MSComm1.PortOpen = True
    Timer1.Enabled = False
    End Sub
    After the timer runs and opens the port, it stops its self. Then when I click the button, it sends the data through com port 2?

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

    Re: Can I send data via com ports?

    Yes, but I'm not sure why you've got the Timer. MSComm can be event driven. You could set-up and open the port in the Form_Load and then when you click Send it sends the data.

    Have you got a particular application in mind ?

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Canada
    Posts
    297

    Re: Can I send data via com ports?

    Yeah, I could put it in the form loader..

    I want to be able to send data from one computer using a simple exe. The data would be a text file or text from a text box, which ever is do-able. I would prefer the text file.

    On the other end, I would like a program to receive the file and place it locally.

    Possible?
    Thanks

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

    Re: Can I send data via com ports?

    Oh yes, very possible. As I said earlier you'd need to develop the Application Protocol so that each end understands each other.

    For example one end would start off 'listening' and then accept a 'command' from the other effectively saying "I'lm about to send you this file". On receipt of that 'command' they might respond "Ok, I'm ready" and then the transfer starts. Also needed would be a method of determining when the transfer had finished, the easiest way might be to send the length of the file before the file itself, that way the receiver could count the bytes and when they've received them all it would know that the transfer was complete.

    So you might define the AP quite simply:
    Command: FT = File Transfer
    Format: FT & Chr(2) & length of file & Chr(4)
    where Chr(2) = Field separator and Chr(4) = Record Separator
    Response to the FT command: "OK" & Chr(4)

    So, once the comms are established you'd do something like
    Code:
    inrFile = FreeFile
    Open "C:\MyText.txt" For Input As intFile
    intLen = Lof(intFile)
    MSCOMM1.Output "FT" & Chr(2) & Cstr(intLen) & Chr(4)
    then wait for the "OK" response from the other end and then send the data.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Canada
    Posts
    297

    Re: Can I send data via com ports?

    Thanks for the in-depth responce. This is totally un-charted VB code for me so how do I Impliment this stuff?

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Canada
    Posts
    297

    Re: Can I send data via com ports?

    This is what I have in the sending application:

    Code:
    Private Sub Form_Load()
    MSComm1.CommPort = 1
    MSComm1.Settings = "9600,N,8,1"
    MSComm1.PortOpen = True
    End Sub
    
    Private Sub MSComm1_OnComm()
    intFile = FreeFile
    Open "C:\MyText.txt" For Input As intFile
    intLen = LOF(intFile)
    End Sub
    
    Private Sub Send_Click()
    MSComm1.Output = "FT" & Chr(2) & CStr(intLen) & Chr(4)
    End Sub
    I have 2 - RS-232 ports on this computer.. Can I use this computer to send and receive the commands? (To test it)

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

    Re: Can I send data via com ports?

    OK. I should also point out that RS232 is not as sophisticated as TCP/IP where these underlying protocols manage the buffering of data and you can quite easily get away with sending umpteen Kb of data all in one go. With RS232 you have to manage all that yourself and you could run into buffer overrun problems (ie you're sending stuff quicker than the other end can receive and process it) so you may also need some sort of handshaking.

    The simplest way would be to implement software handshaking (although MSComm will support hardware handshaking)

    I'll knock up a simple example for you so you can get the idea.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Canada
    Posts
    297

    Re: Can I send data via com ports?

    Quote Originally Posted by Doogle
    I'll knock up a simple example for you so you can get the idea.
    Most appreciated!

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

    Re: Can I send data via com ports?

    You'll need a 'Null Modem Cable" to connect the two ports together. This is basically a 3 wire cable, TX Data (Pin2), RX Data (Pin 3) and Signal Ground(Pin7) on a 25 way connector or Pin 2, Pin 3 and Pin 5 respectively on a 9 way connector. The wiring goes
    Code:
    End "A"            End "B"
    Pin 2  Connects to Pin 1
    Pin 1              Pin 2
    Pin 5              Pin 5 on a 9 way, Pins 7 to 7 on a 25 way

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Canada
    Posts
    297

    Re: Can I send data via com ports?

    I've got a cable, not sure if its a null modem one.

    Its one that I used in Windows 98 to do file sharing, the PC to PC over rs-232 file sharing, recall that?

    With the cable, I have on end plugged in to one port, and the other plug in to the other port. They are connected.

  13. #13
    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

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Canada
    Posts
    297

    Re: Can I send data via com ports?

    Thanks so much for posting it!

    So far it loads, and when the file didnt exist it told me, so i created it.. but when i click send, it just sits.. nothing happens, no files appear.

    Is there a way to check the status?

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

    Re: Can I send data via com ports?

    OK. I've tiedied it up a bit and introduced some buffering. Also I've added a label to each form (labProgress) which should report the bytes sent and received. You'll have to copy and paste the updated code again from Post #13

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Canada
    Posts
    297

    Re: Can I send data via com ports?

    Compile Error:
    Loop without Do

    Line: Loop Until boFinished = True
    (Receiver)




    Compile Error:
    Loop without Do

    Line: Loop Until boFinished = True
    (Sender)

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

    Re: Can I send data via com ports?

    Yeah just saw and corrected that - sorry. You'll have to copy and paste the Receiver again

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Canada
    Posts
    297

    Re: Can I send data via com ports?

    Still nothing, the label isn't changing ither. I might be missing something, do you have the project file?

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

    Re: Can I send data via com ports?

    Of course, I've missed the obvious.... how dumb am I?
    In receiver:
    Code:
    Private Sub Form_Load()
    MSComm1.CommPort = 2
    MSComm1.Settings = "9600,N,8,1"
    MSComm1.RThreshold = 1
    MSComm1.PortOpen = True
    Me.Caption = "Receiver"
    End Sub
    in Sender:
    Code:
    Private Sub Form_Load()
    MSComm1.CommPort = 1
    MSComm1.Settings = "9600,N,8,1"
    MSComm1.RThreshold = 1
    MSComm1.PortOpen = True
    labProgress.Caption = "0"
    End Sub
    without RThreshold <> 0 the OnComm event never triggers!

  20. #20

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Canada
    Posts
    297

    Re: Can I send data via com ports?

    They both load fine, but when I click "Send" in the frmSender I get

    "Run-time error '52'':
    Bad file name or number

    The text file exists tho.

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

    Re: Can I send data via com ports?

    Are you sure you've got the filename and path correct ? It doesn't do that here.

  22. #22

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Canada
    Posts
    297

    Re: Can I send data via com ports?

    Yes, they are all correct.

    Do you have a .vbp, and .frm(s) file of the project?

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

    Re: Can I send data via com ports?

    Attached Zip....
    Attached Files Attached Files

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

    Re: Can I send data via com ports?

    Please let me know how you get on and if there are corrections to be made. I don't want to leave problematic code lying around the forum.

  25. #25

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Canada
    Posts
    297

    Re: Can I send data via com ports?

    Thanks for taking the time to help me with this.

    My network went down right after you posted it so I couldn't download it until this morning. At anyrate, I downloaded your project, and still got the same errors.

    The file existed, I made sure Windows could see it, I copied the text file name, went Start > Run > C:\MyText.txt and it loaded, so it is there.

    However, since the file exists, it had to be that number thing it was talking about.. So I changed a line of code in the Sender..

    I changed this:
    Code:
    MSComm1.Output = "FT" & Chr(2) & CStr(intLen) & Chr(4)
    To this:
    Code:
    MSComm1.Output = "FT" & Chr(2) & CStr(intLen) & Chr(3)
    Now doing this change didn't get anything transfer or activity wise, but it did stop that error from appearing, any ideas?

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

    Re: Can I send data via com ports?

    That shouldn't have anything to do with the problem. Chr(4) is part of the Application Protocol and nothing to dowith the file. If you get to that line then the file is open. Whe it fails what line of code is highlighted?

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

    Re: Can I send data via com ports?

    Ok. I think I've found the problem. It's in frmReceiver not frmSender.
    I forgot to initialise intFile. You need to add intFile = Freefile as below.
    Code:
                            Select Case strUnblock(0)
                                Case "FT"
                                    '
                                    ' File Transfer Command
                                    ' Save the file size and open a file to receive
                                    ' Switch the transfer flag on
                                    '
                                    intFile = FreeFile
                                    intLen = CLng(strUnblock(1))
                                    Open "C:\MyTextRX.txt" For Output As intFile
                                    boTransfer = True
                                    '
                                    ' We can add other commands here
                                    '
                            End Select

  28. #28

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Canada
    Posts
    297

    Re: Can I send data via com ports?

    Runtime:


    When I click End it highlights nothing, when I click Debug I get this in the receiver:

  29. #29

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Canada
    Posts
    297

    Re: Can I send data via com ports?

    Okay, so now it creates the "MyTextRX.txt".. But nothings within the file, 0KB.

    If I click Send again, I get a "Bad File Mode".

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

    Re: Can I send data via com ports?

    Right in frmReceiver you need to add another line. MSComm1.OutPut = "OK", Chr(4) as below:
    Code:
                                    Open "C:\MyTextRX.txt" For Output As intFile
                                    boTransfer = True
                                    MSComm1.Output = "OK" & Chr(4)

  31. #31

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Canada
    Posts
    297

    Re: Can I send data via com ports?

    Now we're getting somewhere!

    However:


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

    Re: Can I send data via com ports?

    Which line in which form?

  33. #33

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Canada
    Posts
    297

    Re: Can I send data via com ports?

    Receiver:
    Code:
    Put #intFile, , strRecord

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

    Re: Can I send data via com ports?

    The problem is in the same area as before I've not opened the file correctly. It should read:
    Code:
    Open "C:\MyTextRX.txt" For Binary As intFile
    EDIT: BTW you should make sure that C:\MyTextRX.txt doesn't exist before you run the program

  35. #35

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Canada
    Posts
    297

    Re: Can I send data via com ports?



    It sent some stuff! It said it received only 7 of 26 bytes. And only the first line of text went with it, as you can see..

    Debug says: (Sender)
    Code:
    labProgress.Caption = CStr(Len(strFileData) + 2 + CInt(labProgress.Caption)) & " Bytes Sent"
    Is the problem..

    Very neat how its sort of working tho! I'm learning alot!

  36. #36

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Canada
    Posts
    297

    Re: Can I send data via com ports?

    I removed that line, things worked (except obivliously the staus label didnt update on the sender and the send / receive worked like a charm.

    It then ended with an error after clicking the 2 ok buttons..

    Debug: (Sender)
    Code:
    If Not EOF(intFile) Then

  37. #37
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Can I send data via com ports?

    I can't play along, but I think the error can be corrected with this. Replace CInt with Val
    Code:
    labProgress.Caption = CStr(Len(strFileData) + 2 + Val(labProgress.Caption)) & " Bytes Sent"

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

    Re: Can I send data via com ports?

    I am really dumb.
    Need a small change in frmSender.
    Just after "Static strBuffer" put
    Code:
    Static intSent As Integer
    and replace the line that's failing with these two
    Code:
                                intSent = intSent + Len(strFileData)
                                labProgress.Caption = CStr(intSent) & " Bytes Sent"

  39. #39

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2007
    Location
    Canada
    Posts
    297

    Re: Can I send data via com ports?

    Quote Originally Posted by LaVolpe
    I can't play along, but I think the error can be corrected with this. Replace CInt with Val
    Code:
    labProgress.Caption = CStr(Len(strFileData) + 2 + Val(labProgress.Caption)) & " Bytes Sent"
    Yep!

    Sender:
    28 Bytes Sent

    Receiver:
    28 of 26 Received

    Odd? lol

    Still ending with the error in the Sender of:
    Code:
    If Not EOF(intFile) Then

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

    Re: Can I send data via com ports?

    Thanks LaVolpe I always forget how flexible Val is!

    I think the problem is that I send an "OK" after the transfer is complete and therefore intfIle is closed hence the If EOF is causing a problem. I'll have to go and look at the logic.

Page 1 of 2 12 LastLast

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