Results 1 to 6 of 6

Thread: [RESOLVED] MSCOMM send 4kb binary data to device

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    282

    Resolved [RESOLVED] MSCOMM send 4kb binary data to device

    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?


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

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

    B.R

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: MSCOMM send 4kb binary data to device

    Quote Originally Posted by VB Client/Server
    In my curent project, I send all data, but last 16 bytes what I send become 17 bytes ( WHY )?
    Welcome to the forums.

    Post the code you are using for this.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    282

    Re: MSCOMM send 4kb binary data to device

    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

    B.R
    Last edited by VB Client/Server; Mar 23rd, 2006 at 10:52 AM.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    282

    Re: MSCOMM send 4kb binary data to device

    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.

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

    B.R

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    282

    Re: MSCOMM send 4kb binary data to device

    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

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    282

    Re: MSCOMM send 4kb binary data to device

    Xxxx

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