Jump to content

Problem with "Put" in Excel

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
drheault

drheault

    Newbie

  • Members
  • Pip
  • 2 posts
Hi,

I'm using MS Excel 2002 SP3 with Visual Basic 6.5 on XP.

I have a rather simple program that fetches data on the COM port via RSAPI.DLL. The COM port is connected to an oscilloscope that is sending a TIF file.

I believe I'm getting the data correctly through the READBYTE function of the RSAPI.DLL. At least when I write the data to a MsgBox I don't see any extra '00' characters.

But then I write the data read back into a binary file, and the PUT statement seams to add an extra '00' almost everytime, except perhaps when I write a '00' (it doesn't write two of them). What am I doing wrong or what is wrong with PUT?

Below is the code. Thank-you in advance!
Denis
Option Explicit                         'force explicit variable declaration
Declare Function OPENCOM Lib "RSAPI.DLL" (ByVal S As String) As Integer
Declare Function CLOSECOM Lib "RSAPI.DLL" () As Integer
Declare Function READBYTE Lib "RSAPI.DLL" () As Integer
Declare Function SENDBYTE Lib "RSAPI.DLL" (ByVal B As Byte) As Integer
Declare Sub TIMEOUT Lib "RSAPI.DLL" (ByVal Ms As Integer)
Declare Sub TIMEINIT Lib "RSAPI.DLL" ()
Declare Function TIMEREAD Lib "RSAPI.DLL" () As Long
Declare Sub DELAY Lib "RSAPI.DLL" (ByVal Ms As Integer)
Declare Function SENDSTRING Lib "RSAPI.DLL" (ByVal S As String) As Integer
Declare Function READSTRING Lib "RSAPI.DLL" (ByVal S As String) As Integer
Declare Sub RTS Lib "RSAPI.DLL" (ByVal onezero As Integer)
Declare Sub DTR Lib "RSAPI.DLL" (ByVal onezero As Integer)
Declare Sub TXD Lib "RSAPI.DLL" (ByVal onezero As Integer)
Declare Function CTS Lib "RSAPI.DLL" () As Integer
Declare Function DSR Lib "RSAPI.DLL" () As Integer
Declare Function DCD Lib "RSAPI.DLL" () As Integer
Declare Function RI Lib "RSAPI.DLL" () As Integer
Declare Sub STRLENGTH Lib "RSAPI.DLL" (ByVal L As Integer)
Declare Sub STRREAD Lib "RSAPI.DLL" (ByVal S As String)

Sub GetDataBin(ComPort)
  Dim RecString As String, ErrorString As String
  Dim HI As Long, LO As Byte, value As Double
  Dim succ As Integer, dummy As Integer
  Dim rByte As Integer
  Dim rByteS As Long
  Dim MyFile As String
  Dim fnum As Integer
    
  succ = OPENCOM(ComPort)     'open serial communication port (DTR=1, RTS=0)

  If (succ = 0) Then
    dummy = MsgBox("RS232 connection failed: " & ComPort, vbCritical, "RSAPI.DLL")
  Else
    TIMEOUT 30       'set time to wait for data, afterwards error in rByte
    MyFile = Application.GetOpenFilename("Text Files (*.txt), *.txt")
    If MyFile <> "False" Then
       fnum = FreeFile()
       Open MyFile For Binary Access Write As fnum
    
       '************** Outer Loop *************
       'While TIMEREAD <= 400
         '************** Inner Loop *************
         Do
           rByte = READBYTE
           rByteS = rByte
           If (rByte > -1) Then
              Put #fnum, , rByte
              If MsgBox("Do you want to continue ? " & Chr(rByteS) & rByte,
                            vbOKCancel, _ "My Title") = vbCancel Then Exit Sub
           End If
           DoEvents
       Loop
       dummy = CLOSECOM
       Close #fnum
     End If
  End If
End Sub

Edited by Jaan, 09 July 2009 - 06:57 AM.
Please use code tags whrn you are posting your codes!


#2
drheault

drheault

    Newbie

  • Members
  • Pip
  • 2 posts
Got it.

had to convert the Integer to Byte...
Put #fnum, , CByte(rByte And 255)
That's all.
Drove me nuts for a few hours!
Thanks,
Denis