PDA

Click to See Complete Forum and Search --> : [RESOLVED] MSCOMM send 4kb binary data to device


VB Client/Server
Mar 23rd, 2006, 04:25 AM
Hello,

Here is my problem and if someone can help me solve it...
I try to send 4kb binary data to COM Port to some electronic device.
Writing data to eeprom.
Settings for mscomm are 115000,N,8,1
When I load data 4kb I need to send them to device random as 16 bytes
until all data will be sent.
How to load file and make buffer for sending 16 bytes only? :confused:


In my curent project, I send all data, but last 16 bytes what I send become 17 bytes ( WHY )? :confused:

Can someone post here how code would look like for doing this right.

B.R

Hack
Mar 23rd, 2006, 07:18 AM
In my curent project, I send all data, but last 16 bytes what I send become 17 bytes ( WHY )? :confused: Welcome to the forums. :wave:

Post the code you are using for this.

VB Client/Server
Mar 23rd, 2006, 08:37 AM
Private Sub Command1_Click()
Cdlg.DialogTitle = "Send file..."
Cdlg.Filter = "Binary File (*.BIN)|*.bin|All Files (*.*)|*.*"
Cdlg.InitDir = App.Path
Cdlg.FileName = ""
Cdlg.ShowOpen

' The Function Call
If Cdlg.FileName <> "" Then SendFile (Cdlg.FileName)

End Sub

Private Sub Form_Load()

' Opens the selected com port
MSComm1.CommPort = 1
MSComm1.PortOpen = True

End Sub


' here is the function
Function SendFile(tmp$)

Dim temp$
Dim hsend, bsize, LF&

' Open file
Open tmp$ For Binary Access Read As #2
' Check size on Mscomm1 OutBuffer
bsize = MSComm1.OutBufferSize
' Check file length
LF& = LOF(2)

' This code makes tiny pieces of data (Buffer sized)
' And send's it

Do Until EOF(2)

If LF& - Loc(2) <= bsize Then
bsize = LF& - Loc(2) + 1
End If

' Make room for some data
temp$ = Space$(bsize)

' Put the data piece in the Temp$ string
Get #2, , temp$

MSComm1.Output = temp$

Do

' Wait until the buffer is empty
Loop Until MSComm1.OutBufferCount = 0
Loop

' close file
Close #2

End Function


And MSCOMM settings are:
DTREnable=False
EOFEnable=True
Handshaking=0
InBufferSize=16384
InputMode=1 - comInputModeBinary
OutBufferSize=16
ParityReplace=N
RThreshold=1
RTSEnable=False
Settings=115200,n,8,1
SThreshold=1

And thanx for welcoming note :wave:

B.R

VB Client/Server
Mar 24th, 2006, 02:41 AM
Function SendFile(tmp$)
On Error GoTo Error
If MSComm1.PortOpen = True Then
MSComm1.PortOpen = False
End If

If Option1.Value = True Then MSComm1.CommPort = 1
If Option2.Value = True Then MSComm1.CommPort = 6
If Option3.Value = True Then MSComm1.Settings = "57600,N,8,1"
If Option4.Value = True Then MSComm1.Settings = "115200,N,8,1"

MSComm1.RTSEnable = True
MSComm1.DTREnable = False
MSComm1.PortOpen = True
' Send binary data to prepare device for writing
Binary ("06")
' wait for device become ready
pause (10)
Dim temp$
Dim hsend, bsize, LF&

' Open file
Open tmp$ For Binary Access Read As #2
' Check size on Mscomm1 OutBuffer
bsize = MSComm1.OutBufferSize
' Check file length
LF& = LOF(2)

' This code makes tiny pieces of data (Buffer sized)
' And send's it

Do Until EOF(2)


If LF& - Loc(2) <= bsize Then
bsize = LF& - Loc(2) + 0
End If

' Make room for some data
temp$ = Space$(bsize)

' Put the data piece in the Temp$ string
Get #2, , temp$

MSComm1.Output = temp$

Do
pause 10
Loop Until MSComm1.OutBufferCount = 0
Loop
Error:
MsgBox "xxxxx", vbInformation, "xxxxx"


Close #2

End Function


Now I have make some changes to code and device are programmed OK.
But still some error's like, when I send all data, COM Port still stay's active
and I just dont know how to make code that afther sending all data
when buffer is empty close Port. :rolleyes:

Any sugestion's "HACK" ? :wave:
I know that code must be somwhere in Do - Loop...

B.R

VB Client/Server
Mar 24th, 2006, 09:13 AM
Problem solved!
I put timer to close port few sec afther all data are sent.
Maybe not best solution, but it works OK on few PC-s.

B.R

VB Client/Server
Apr 23rd, 2006, 08:53 AM
Xxxx