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?
Printable View
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?
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.
How do I go about doing it?
I did some searching, would something like this work to send data:
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?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
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 ?
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 :)
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
then wait for the "OK" response from the other end and then send the data.Code:inrFile = FreeFile
Open "C:\MyText.txt" For Input As intFile
intLen = Lof(intFile)
MSCOMM1.Output "FT" & Chr(2) & Cstr(intLen) & Chr(4)
Thanks for the in-depth responce. This is totally un-charted VB code for me so how do I Impliment this stuff?
This is what I have in the sending application:
I have 2 - RS-232 ports on this computer.. Can I use this computer to send and receive the commands? (To test it)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
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.
Most appreciated!Quote:
Originally Posted by Doogle
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
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.
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.
frmSender with a CommandButton named cmdSend and an MSComm Control named MSComm1 and uses CommPort 1Code:'
' 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
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.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
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.
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?
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
Compile Error:
Loop without Do
Line: Loop Until boFinished = True
(Receiver)
Compile Error:
Loop without Do
Line: Loop Until boFinished = True
(Sender)
Yeah just saw and corrected that - sorry. You'll have to copy and paste the Receiver again
Still nothing, the label isn't changing ither. I might be missing something, do you have the project file?
Of course, I've missed the obvious.... how dumb am I?
In receiver:
in Sender: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
without RThreshold <> 0 the OnComm event never triggers!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
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.
Are you sure you've got the filename and path correct ? It doesn't do that here.
Yes, they are all correct.
Do you have a .vbp, and .frm(s) file of the project?
Attached Zip....
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.
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:
To this:Code:MSComm1.Output = "FT" & Chr(2) & CStr(intLen) & Chr(4)
Now doing this change didn't get anything transfer or activity wise, but it did stop that error from appearing, any ideas?Code:MSComm1.Output = "FT" & Chr(2) & CStr(intLen) & Chr(3)
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
Runtime:
http://i166.photobucket.com/albums/u...da/runtime.png
When I click End it highlights nothing, when I click Debug I get this in the receiver:
http://i166.photobucket.com/albums/u.../highlight.png
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".
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)
Now we're getting somewhere! :)
However:
http://i166.photobucket.com/albums/u...hangesmade.png
Which line in which form?
Receiver:
Code:Put #intFile, , strRecord
The problem is in the same area as before I've not opened the file correctly. It should read:
EDIT: BTW you should make sure that C:\MyTextRX.txt doesn't exist before you run the programCode:Open "C:\MyTextRX.txt" For Binary As intFile
http://i166.photobucket.com/albums/u...pemismatch.png
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)
Is the problem..Code:labProgress.Caption = CStr(Len(strFileData) + 2 + CInt(labProgress.Caption)) & " Bytes Sent"
Very neat how its sort of working tho! I'm learning alot!
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
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"
I am really dumb.
Need a small change in frmSender.
Just after "Static strBuffer" put
and replace the line that's failing with these twoCode:Static intSent As Integer
Code:intSent = intSent + Len(strFileData)
labProgress.Caption = CStr(intSent) & " Bytes Sent"
Yep!Quote:
Originally Posted by LaVolpe
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
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.