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?
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?
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.
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.
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.
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)
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.
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
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
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
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..
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?
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
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.