Announcement

Collapse
No announcement yet.

How to translated into PBWin?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to translated into PBWin?

    Hi all,

    I have read the help and repeatedly try to translate it but I can not get from Delphi to convert to PB.

    Code:
    WriteFile(PortHandle,(Pchar(Buffer))^,Length(Buffer),BytesWritten,NIL);
    I Try this in PB:
    g_PortHandle = CreateFile(VarPtr(strPort),%GENERIC_READ Or %GENERIC_WRITE, ByVal 0, ByVal 0, %OPEN_EXISTING,0,0)
    Code:
    function OpenSerial (OpenString: pchar): Integer;
    I try this in PB: 
    
    Function OpenSerialPort(ByRef szOpenStr As StringZ) As Integer
    Code:
    PortHandle:=CreateFile(PChar(PortStr),GENERIC_READ or      GENERIC_WRITE,0,NIL,OPEN_EXISTING,0,0);
    
    I try this in PB: 
    g_PortHandle = CreateFile(varptr(strPort),%GENERIC_READ OR %GENERIC_WRITE, byval 0, byval 0, %OPEN_EXISTING,0,0)
    Code:
    if ClearCommError (PortHandle, Errors, @Comstat) then
    
    I try this:
    if ClearCommError(g_PortHandle, Errors, @Comstat) then
    Can anyone help me sometimes to write in PB this?

    It's still just the bits that I need for my sll library serial port for a client and then test it and sell the library.

    Thanks in advance

    Stephane

  • #2
    Your problem seems to be with the WiteFile(), CreateFile() and ClearCommError() functions. These are NOT PowerBASIC verbs, they are standard Windows' API functions.

    Check your Windows API reference. That will tell you how those functions must be called (what parameters to pass) and what they return.
    Michael Mattias
    Tal Systems (retired)
    Port Washington WI USA
    [email protected]
    http://www.talsystems.com

    Comment


    • #3
      Hi Stephane;

      Is there some reason you cannot use PB's built in WRITE and READ statements?

      The code below is from PB's Help file
      Code:
      ' Open a sequential output file and write to it
      OPEN "FILE.TXT" FOR OUTPUT AS #1
      WRITE #1, "TEST"
      z& = -12345&
      info1$ = "Do not covet"
      info2$ = "thy neighbors ox"
      WRITE #1, z&, info1$, info2$
      WRITE #1, "TEST"
      CLOSE #1

      Comment


      • #4
        Stephane,

        Michael is right the code you have posted is WIN API related.

        However by the variable names and your comment about serial port I think you need COMM port statements.

        The API can be used for either Files or COMM Ports.

        Walt's suggestion to use PB commands is a good one.
        If you are using files then the code he posted is a good place to start.

        If you need serial port commands then look at the following PB COMM commands:
        Code:
        COMM
        COMM CLOSE
        COMM OPEN
        COMM LINE
        COMM PRINT
        COMM RECV
        COMM RESET
        COMM SEND
        COMM SET
        COMM TIMEOUT
        Also you might take a look at the PB sample COMM.BAS

        Which will be in the "\samples\Comms" directory of your PBWIN install.

        Hope this gets you in the right direction.
        Kind regards,
        Neil

        http://www.BASICProgramming.info

        Comment


        • #5
          >Is there some reason you cannot use PB's built in WRITE and READ statements

          In this case, actually yes.

          His Delphi code is opening, reading from and writing to a serial communications port.

          In PowerBasic, you have to read the chapter on "serial communications" in the help file and use the appropriate statements which I am deliberately not mentioning here.

          His problem with his WinAPI calls in the Dephi code is caused because he is mindlessly (IMNSHO) trying to do a verb-for-verb, function-for-function, statement-for-statement conversion from one source code language to another, instead of approaching his problem by asking, "What exactly do I want to do here?"

          MCM
          Michael Mattias
          Tal Systems (retired)
          Port Washington WI USA
          [email protected]
          http://www.talsystems.com

          Comment


          • #6
            Stephane,
            Finally, I think I understand the reason you seem to ask the same kinds of questions repeatedly, and why it is difficult for people on the forums to help you.

            I could be completely wrong, and if that's true, please excuse me. I am really trying to get to the bottom of the trouble I've been observing.

            You wrote that you repeatedly "try to translate" other code (C, Delphi, VB, etc.) "into PB". And FINALLY you gave us very clear examples of what you were trying to do, and how it was not working the way you expected it to.

            It's my observation that we are up against 2 different "language barriers".
            The first is between two human languages - yours and ours ("American English"). The second barrier is between two computer languages - the one you found online in someone else's code, and PB.

            As Michael, Walt, and Neil are trying to explain, you cannot "translate" in the same way that you would "translate" the English word "house" to the Spanish word "casa". It doesn't work like that.

            Maybe instead of "translate", let's use the word "convert". To CONVERT something is to "CHANGE something's form or function". For example, to CONVERT your house from oil heat to electric heat, you must change the furnace, in fact, you must remove it completely - it does not belong in the new system. Next, you must upgrade your power panel to handle the different power demand. Then, you must remove the old radiators and replace them with baseboard heaters or something similar.

            You cannot just expect electric heaters to take the place of the oil furnace.

            It seems to me that you are expecting to "translate", but the reality is that you must CONVERT the other code to PB.

            They are NOT "substitutions", but re-creations: you must create the function of the first code language USING THE COMMANDS OF THE SECOND CODE LANGUAGE.

            SO! You need to understand the PB commands, what they do and what to expect from each. THEN you can put them together properly in order to achieve the function you want.

            Again, if I am wrong and have made more of this than warranted, please excuse me, and I'll step aside and let others help.

            But if this makes any sense to you, I'm happy to try to help further. Please let me know if you find my analysis to be helpful.

            I wish you success with your efforts!
            -John

            Comment


            • #7
              Translate vs convert

              John,

              I think you are correct.

              Kind of the difference of riding a tricycle vs a bicycle vs a unicycle. While they are all transportation devices there are differences. Understanding how to ride one doesn't necessarily help how to ride the others.

              A tricycle and bicycle both have a steering wheel. Make to sharp a turn on a tricycle and your going to fall over. On a bicycle you can lean the bike to make a sharper turn. A unicycle doesn't have a steering wheel and you can turn 180 degrees without moving from your spot.

              Converting code is harder than writing a new one. You must understand two languages, not just one. 6 lines of C code might be one line of PB code or vice-versa. Trying to rewrite line by line may not be possible and you might end up with something unmaintainable.

              Comment


              • #8
                Originally posted by Michael Mattias View Post
                Your problem seems to be with the WiteFile(), CreateFile() and ClearCommError() functions. These are NOT PowerBASIC verbs, they are standard Windows' API functions.

                Check your Windows API reference. That will tell you how those functions must be called (what parameters to pass) and what they return.
                How, can I passed the parameters and the return values. This is what I read in the WinBase.inc file :

                DECLARE FUNCTION CreateFile LIB "Kernel32.dll" ALIAS "CreateFileA" _
                (lpFileName AS ASCIIZ, BYVAL dwDesiredAccess AS DWORD, _
                BYVAL dwShareMode AS DWORD, lpSecurityAttributes AS SECURITY_ATTRIBUTES, _
                BYVAL dwCreationDisposition AS DWORD, _
                BYVAL dwFlagsAndAttributes AS DWORD, BYVAL hTemplateFile AS DWORD) _
                AS DWORD

                This is the code:

                Code:
                Function OpenSerialPort(ByRef szOpenStr As AsciiZ) As Integer
                    Local strPort   As AsciiZ * 10
                    Local strParam  As AsciiZ * 10
                    Local t_DCB     As DCB
                
                    Function = 0
                    If(g_PortHandle > 0) Then CloseHandle(g_PortHandle)
                    strParam = szOpenStr
                    strPort  = Mid$(strParam,1,4)
                    'Hoe
                    g_PortHandle = CreateFile(VarPtr(strPort),%GENERIC_READ Or %GENERIC_WRITE, ByVal 0, ByVal 0, %OPEN_EXISTING,0,0)
                    GetCommState(g_PortHandle,tDCB)
                    BuildCommDCB(VarPtr(strParam), tDCB)
                    tDCB.Flags = 1
                    If(SetCommState(g_PortHandle, tDCB) Then
                        Function = 1
                    End If
                    TimeOuts(10)
                End Function
                Code:
                Sub SendString(ByRef szBuffer As AsciiZ)
                    Local BytesWritten As Dword
                
                    'Hoe schrijven in PowerBasic?
                    WriteFile(g_PortHandle,(VarPtr(szBuffer)), Length(Buffer), BytesWritten, ByVal 0)
                End Sub

                MCM:

                My intention is how the parameters correctly relay in the WinAPI functions and what I need to return? The data type PChar corresponds to the manual of PowerBASIC with Stringz but you can also use ASCIIZ or it depends on the compiler?. How do you Stringz and ASCIIZ by as a parameter? Is that with ByVal or VARPTR?

                It is not intended to translate it to PowerBASIC. But I do not know how to pass parameters and return values​​.

                What in the big red and bold font is that is the problem. I passed on these parameters and return values ​​correctly?

                I hope that I have clear explained what I mean?

                Kind regards
                Stephane

                Comment


                • #9
                  MCM told you to check your Windows API Reference. WinBase.Inc is NOT your Windows API Reference.

                  If you don't have Win32API.hlp or similar, then Google is your friend. The first reference to CREATEFILE is:

                  http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx

                  IF you read through that (which is the PRIME reference to the Windows API), you will come across:

                  Communications Resources

                  The CreateFile function can create a handle to a communications resource, such as the serial port COM1. For communications resources, the dwCreationDisposition parameter must be OPEN_EXISTING, the dwShareMode parameter must be zero (exclusive access), and the hTemplateFile parameter must be NULL. Read, write, or read/write access can be specified, and the handle can be opened for overlapped I/O.
                  To specify a COM port number greater than 9, use the following syntax: "\\.\COM10". This syntax works for all port numbers and hardware that allows COM port numbers to be specified.
                  which tells you what to pass as the:


                  "lpFileName [in]
                  The name of the file or device to be created or opened."
                  You should be able to work the rest out for yourself.

                  Comment


                  • #10
                    John,

                    My intention is not to translate into PowerBASIC but just how to pass the parameters that is my intention and what values ​​I get back.

                    PChar: is a pointer to a char and according to the manual is converted to PB datatype ASCIIZ or Stringz but how do you get that pointer through that with VARPTR or STRPTR and how do I get the contents back is that with "@" or "@ @ "?

                    Syntax Delphi :
                    The syntax to declare a pointer data type uses a caret (^) or
                    when "^" appears after a pointer variable it dereferences the pointer; that is, it returns the value stored at the memory address held by the pointer
                    The @ operator returns the address of a variable (or a function or procedure)


                    I try to convert this from Delphi to Power Basic syntax:

                    From Delphi code syntax

                    [code]

                    Var
                    iValue, j : Integer;
                    pIntValue : ^Integer;
                    Begin
                    iValue := 2001;
                    pIntValue := @iValue;
                    ...
                    j:= pIntValue^;
                    End;

                    To PowerBasic code syntax

                    Code:
                    Dim iValue      As Integer
                    Dim j           As Integer
                    Dim pIntValue   As Integer Ptr
                    
                    iValue = 2001
                    pIntValue = VarPtr(iValue)
                    j = @pIntValue
                    
                    Now I want this piece of code delphi convert into PowerBASIC syntax:
                    But how do I specify the parameters because you have several options VARPTR, STRPTR, ByVal, BYCOPY. There will always return a PChar = Pointer to a Char and would correspond in PBWin9 with ASCIIZ and PBWin10 with Stringz. Which one I use now in the declaration of the WinAPI function and passing in a function call?. That's my problem.


                    Once more I explained what my intention is see post # 8

                    Kind regards
                    Stephane

                    Comment


                    • #11
                      >For example, to CONVERT your house from oil heat to electric heat..

                      What a simply awful comparison.

                      IMO.

                      YMMV.

                      MCM
                      Michael Mattias
                      Tal Systems (retired)
                      Port Washington WI USA
                      [email protected]
                      http://www.talsystems.com

                      Comment


                      • #12
                        Now I want this piece of code delphi convert into PowerBASIC syntax:
                        But how do I specify the parameters because you have several options VARPTR, STRPTR, ByVal, BYCOPY. There will always return a PChar = Pointer to a Char and would correspond in PBWin9 with ASCIIZ and PBWin10 with Stringz. Which one I use now in the declaration of the WinAPI function and passing in a function call?. That's my problem
                        No, your problem is you lack the fundamental skills required to try to 'convert' or 'port' from one language to another; this task is NOT a 'search and replace' operation.

                        When some function is documented to require a '*char' parameter, you need to know how to do that in the PB language. Hint: there is more than one way to do this, all of which are dependent on the variable types you have chosen to use.

                        MCM
                        Michael Mattias
                        Tal Systems (retired)
                        Port Washington WI USA
                        [email protected]
                        http://www.talsystems.com

                        Comment


                        • #13
                          >WinBase.Inc is NOT your Windows API Reference

                          AMEN, MY BROTHER!
                          Michael Mattias
                          Tal Systems (retired)
                          Port Washington WI USA
                          [email protected]
                          http://www.talsystems.com

                          Comment


                          • #14
                            Originally posted by Michael Mattias View Post
                            >For example, to CONVERT your house from oil heat to electric heat..

                            What a simply awful comparison.

                            IMO.

                            YMMV.

                            MCM
                            Get a life, Michael! :laugh:

                            It was perfect - it illustrated the drastic differences between code environments. In one, you may need a furnace...

                            YFOTR,

                            -jhm

                            Comment


                            • #15
                              Stephane,

                              You wrote that in Delphi "The syntax to declare a pointer data type" ...

                              This again illustrates that you are not understanding what everyone is telling you.

                              It is NOT about changing the SYNTAX of Delphi to PB. The issue is WHETHER YOU NEED A POINTER AT ALL!!!!

                              But to know if you need a pointer or not, you need to first understand PB's methods for passing parameters. Oh, yes, you did say that.

                              So why not spend some time in the PB Help file, and read the section "Passing parameters". That section points to other useful sections, such as "Parameters passed by reference or by copy".

                              You WILL find a lot of your answers there. But they will NOT look like Delphi code.

                              You could also search these PB forums for other threads on similar issues, such as http://www.powerbasic.com/support/pb...eter+data+type

                              Note in this thread what Jose wrote in message #14
                              Translating C++ to PB requires good knowledge of the calling conventions used by both languages.
                              Now replace C++ with the language you're working with today...

                              There's a lot to learn from those discussions.

                              -John
                              Last edited by John Montenigro; 25 Oct 2013, 03:40 PM. Reason: added link

                              Comment


                              • #16
                                Just in case you couldn't, Michael,

                                YFOTR = You Figure Out The Rest... :laugh:

                                BTW, after receiving your incisive critique on my attempt to help Stephane, I noticed "your analogies not shown".

                                C'mon now, you know the rules - if you want to shoot down someone else's help, you'd better pony up a better offering.

                                So, I look forward to your positive addition to the discussion.

                                -John

                                Comment


                                • #17
                                  >So, I look forward to your positive addition to the discussion

                                  I have positively lost my patience with this member.

                                  Happy now?
                                  Michael Mattias
                                  Tal Systems (retired)
                                  Port Washington WI USA
                                  [email protected]
                                  http://www.talsystems.com

                                  Comment


                                  • #18
                                    Hate to say it...but...."In case of emergency, break glass"
                                    Engineer's Motto: If it aint broke take it apart and fix it

                                    "If at 1st you don't succeed... call it version 1.0"

                                    "Half of Programming is coding"....."The other 90% is DEBUGGING"

                                    "Document my code????" .... "WHYYY??? do you think they call it CODE? "

                                    Comment


                                    • #19
                                      Originally posted by Stephane Fonteyne View Post
                                      PChar: is a pointer to a char and according to the manual is converted to PB datatype ASCIIZ or Stringz but how do you get that pointer through that with VARPTR or STRPTR and how do I get the contents back is that with "@" or "@ @ "?
                                      In the C language, only values are passed. Those values may be actual values,
                                      or they may be a number that represents the address in memory of the actual
                                      data to be passed. When the value passed is the memory address of the data
                                      to be passed, it is called a pointer. A pointer is an address to be used as a
                                      reference to the data. It must be de-referenced to be used in C.

                                      The default state for passing variables in PowerBASIC is by reference.
                                      Take a little bit of time away from your main task and build an understanding
                                      of the tools you are using. When you pass a variable to a function requiring
                                      a pointer to type char, you pass the reference, or VARPTR() of your string.
                                      This is default behavior when passing a STRINGZ type. If you just pass a
                                      STRINGZ, you are passing the reference, or pointer of that STRINGZ.

                                      If you wanted to deliberately pass the pointer value as a LONG, then you
                                      could also do that and achieve the same results. The same value is actually
                                      passed to the called function. You will not have to dereference returned data
                                      if it is received in the proper context. If a function returns a char*, then you
                                      may receive it as a STRINGZ variable and the dereference is automatic.
                                      Or you may receive it as a LONG and assign that to a pointer of the proper
                                      type and use the @varname to dereference.

                                      Try this exercise. Figure out how and why it works. A char* is a VARPTR().
                                      This is what PowerBASIC will pass when you pass a STRINGZ value. Because
                                      the called routine expects that VARPTR() value, you could also pass that, and
                                      the called routine would not be able to tell the difference. You could even use
                                      a regular string, append a CHR$(0) to it, and pass its STRPTR() value.
                                      You could not pass the VARPTR() itself for a STRING type.
                                      Code:
                                      #COMPILE EXE
                                      #DIM ALL
                                      FUNCTION PBMAIN () AS LONG
                                          LOCAL Mystring AS STRING
                                          LOCAL Myzstring AS STRINGZ * 20
                                          Mystring = "My String"
                                          passstringvalues Mystring, VARPTR(Mystring), STRPTR(Mystring)
                                          Myzstring = "My ZString"
                                          passzstringvalues Myzstring, VARPTR(Myzstring)
                                      END FUNCTION
                                      SUB passstringvalues(s AS STRING, va AS LONG, sa AS LONG)
                                          LOCAL sva AS STRING PTR
                                          LOCAL ssa AS ASCIIZ PTR
                                          LOCAL lva AS LONG PTR
                                          sva = va
                                          ssa = sa
                                          lva = va
                                          ? "string =  " + s + STR$(STRPTR(s)) + STR$(VARPTR(s)) + STR$(@lva) + $CR + _
                                            "varptr =  " + @sva + STR$(sva) + $CR + _
                                            "strptr  =  " + @ssa + STR$(ssa)
                                      END SUB
                                      SUB passzstringvalues(s AS STRINGZ, sa AS LONG)
                                          LOCAL ssa AS ASCIIZ PTR
                                          ssa = sa
                                          ? "string =  " + s + STR$(VARPTR(s)) + $CR + _
                                            "strptr  =  " + @ssa + STR$(ssa)
                                      END SUB
                                      The world is strange and wonderful.*
                                      I reserve the right to be horrifically wrong.
                                      Please maintain a safe following distance.
                                      *wonderful sold separately.

                                      Comment


                                      • #20
                                        If I read the posts right with the OpenSerialPort question you have at least three problems.

                                        1) What serial ports exist on your machine? Be 100% sure you know, trying to open a non-existent port will be very frustrating. Mine may change every time I remove and then plug in a device, can be quite annoying.

                                        2) What is the format of a port name that OpenSerialPort requires. Be 100% sure you know, trying to open an existing port with one character out of place will be very frustrating.

                                        3) Finally, once you're sure you've answered 1 & 2, read the Microsoft docs on OpenSerialPort and attempt to figure out the PB syntax for passing parameters to the function.

                                        Also worth keeping in mind, focusing on one parameter exclusively when you have 4 or 5 others can be very frustrating if one of the other parameters is wrong. If you're really, really sure you're doing the right thing and it's not working, it's quite possible you are and the problem is somewhere else.
                                        LarryC
                                        Website
                                        Sometimes life's a dream, sometimes it's a scream

                                        Comment

                                        Working...
                                        X