Serial communication via RS232

General Help regarding HMG, Compilation, Linking, Samples

Moderator: Rathinagiri

User avatar
mol
Posts: 3825
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: Serial communication via RS232

Post by mol »

When I've changed name of hb20_lib_hbwin to libhbwin_rs.a and added to configuration of IDE, I've got another error: undefinded reference to hb_osDecode

I think, It's required another one library, but don't know which...

definition of function hb_osDecode is placed in harbour20\source\vm\vm.c
How to include it into my program?
User avatar
Rathinagiri
Posts: 5482
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Contact:

Re: Serial communication via RS232

Post by Rathinagiri »

Do you want to create a library from a c source code?
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.
Mario Mansilla
Posts: 271
Joined: Wed Aug 13, 2008 2:35 pm
Location: Córdoba - Argentina

Re: Serial communication via RS232

Post by Mario Mansilla »

Hola Mol :
Te construi un ejemplo de lectura de una balanza con la libreria , fijate tambien esta la funcion de escritura en el puerto serie , me demore en contestarte porque no conseguia una balanza para probar . Este proyecto de comunicacion esta en desarrollo me faltan depurar algunos errores como tomar y mostrar los errores con GetLasterr() ya que tengo poca experiencia en C .
Espero que puede ayudarte , y si alguien lo puede mejorar bienvenido .
Saludos .
Mario Mansilla

Hello Mol:
We constructed an example of reading a balance with the library, Also note that the function of writing in the serial port, delay in seeking to answer because I did not get a balance to try. This project is in development communication fail me debug some errors like take and display errors with GetLasterr () because I have little experience in C.
I hope you can help, and if anyone can improve welcome.
Greetings.
Mario Mansilla
Attachments
SerialPort.rar
(644.46 KiB) Downloaded 592 times
User avatar
mol
Posts: 3825
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: Serial communication via RS232

Post by mol »

I've recompiled harbour-nightly, placed into hmg, and now it works .
there were some changes in hbwin:
Win_Port was changed to Win_COM...

Many thanks Mario!
I'll look up for your sample.
Marek
User avatar
Roberto Lopez
HMG Founder
Posts: 4023
Joined: Wed Jul 30, 2008 6:43 pm

Re: Serial communication via RS232

Post by Roberto Lopez »

mol wrote:I've recompiled harbour-nightly, placed into hmg, and now it works .
there were some changes in hbwin:
Win_Port was changed to Win_COM...

Many thanks Mario!
I'll look up for your sample.
Marek
Sorry. I don't knew that com port support were added to hbwin.
Regards/Saludos,

Roberto


(Veritas Filia Temporis)
User avatar
mol
Posts: 3825
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: Serial communication via RS232

Post by mol »

I found, that harbour 2.0 includes communication via RS!!!

(Maybe it looks like a discovering of America, but I'm lucky - after two days of searching I found it and realized my small but very useful application for operating with fiscal printer - it open the door to rebuild app. for printing fiscal checks...).

It's realized in hbwin library.
Good sample is placed in harbour\contrib\hbwin folder.
It's written in console mode, but good commented.
Current version of HMG works fine. It's only required to add library libhbwin.a
to hmg\harbour\lib folder.
For all, who don't want to download harbour sources, I place source code of
harbour\contrib\hbwin\tests\testcom2.prg

Code: Select all

/*
 * $Id: testcom2.prg 12732 2009-10-19 21:17:04Z vszakats $
 */

/*
 * Harbour Project source code:
 * Windows communications library
 *
 * Copyright 2005-2009 Alex Strickland <sscc@mweb.co.za>
 * www - http://www.harbour-project.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this software; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307 USA (or visit the web site http://www.gnu.org/).
 *
 * As a special exception, the Harbour Project gives permission for
 * additional uses of the text contained in its release of Harbour.
 *
 * The exception is that, if you link the Harbour libraries with other
 * files to produce an executable, this does not by itself cause the
 * resulting executable to be covered by the GNU General Public License.
 * Your use of that executable is in no way restricted on account of
 * linking the Harbour library code into it.
 *
 * This exception does not however invalidate any other reasons why
 * the executable file might be covered by the GNU General Public License.
 *
 * This exception applies only to the code released by the Harbour
 * Project under the name Harbour.  If you copy code from other
 * Harbour Project or Free Software Foundation releases into a copy of
 * Harbour, as the General Public License permits, the exception does
 * not apply to the code that you add in this way.  To avoid misleading
 * anyone as to the status o such modified files, you must delete
 * this exception notice from them.
 *
 * If you write modifications of your own for Harbour, it is your choice
 * whether to permit this exception to apply to your modifications.
 * If you do not wish that, delete this exception notice.
 *
 */

#include "hbwin.ch"

PROCEDURE Main( cPortName )

   LOCAL oWinPort
   LOCAL cString := Space( 32 )
   LOCAL lCTS, lDSR, lRing, lDCD
   LOCAL lCTSHold, lDSRHold, lDCDHold, lXoffHold, lXoffSent, nInQueue, nOutQueue
   LOCAL nResult

   IF Empty( cPortName )
      ? "Usage : winport portname"
      RETURN
   ENDIF

   ? "Attach a scanner now ..."
   ?
   Inkey( 0 )

   oWinPort := win_Com():Init( cPortName, CBR_9600, ODDPARITY, 7, ONESTOPBIT )
   IF ! oWinPort:Open()
      ? "Open() failed :", oWinPort:Error()
   ELSE
      ? "Open() succeeded"
      ?
      IF oWinPort:Status( @lCTS, @lDSR, @lRing, @lDCD )
         ? "Status() succeeded : CTS ", lCTS, ", DSR ", lDSR, ", Ring ", lRing, ", DCD ", lDCD
      ELSE
         ? "Status() failed :", oWinPort:Error()
      ENDIF
      ?
      ? "Testing DTR, configure a scanner to require DTR"
      ? "Scan something... and press enter (shouldn't scan)"
      Inkey( 0 )
      ? "Read() ", oWinPort:Read( @cString, 32 ), Len( cString ), cString
      ?
      IF oWinPort:SetDTR( .T. )
         ? "SetDTR( .T. ) succeeded"
      ELSE
         ? "SetDTR( .T. ) failed :", oWinPort:Error()
      ENDIF
      ? "Scan something... and press enter (read should work)"
      Inkey( 0 )
      ?
      IF oWinPort:QueueStatus( @lCTSHold, @lDSRHold, @lDCDHold, @lXoffHold, @lXoffSent, @nInQueue, @nOutQueue )
         ? "QueueStatus() : CTSHold", lCtsHold, ", DSRHold", lDsrHold, ", DCDHold", lDCDHold, ", XoffHold", lXoffHold, ;
                 ", Xoff Sent ", lXoffSent, ", InQueue ", nInQueue, ", nOutQueue ", nOutQueue
      ELSE
         ? "QueueStatus() failed :", oWinPort:Error()
      ENDIF
      ?
      ? "Read() ", oWinPort:Read( @cString, 32 ), Len( cString ), cString
      ?
      IF oWinPort:QueueStatus( @lCTSHold, @lDSRHold, @lDCDHold, @lXoffHold, @lXoffSent, @nInQueue, @nOutQueue )
         ? "QueueStatus() : CTSHold", lCtsHold, ", DSRHold", lDsrHold, ", DCDHold", lDCDHold, ", XoffHold", lXoffHold, ;
                 ", Xoff Sent ", lXoffSent, ", InQueue ", nInQueue, ", nOutQueue ", nOutQueue
      ELSE
         ? "QueueStatus() failed :", oWinPort:Error()
      ENDIF
      ?
      IF oWinPort:RTSFlow( RTS_CONTROL_HANDSHAKE )
         ? "RTSFlow( RTS_CONTROL_HANDSHAKE ) succeeded"
      ELSE
         ? "RTSFlow( RTS_CONTROL_HANDSHAKE ) failed :", oWinPort:Error()
      ENDIF
      ? oWinPort:DebugDCB( WIN_COM_DBGFLOW )
      IF oWinPort:SetRTS( .F. )
         ? "SetRTS( .F. ) succeeded (it shouldn't according to docs!)"
      ELSE
         ? "SetRTS( .F. ) failed (it should) :", oWinPort:Error()
      ENDIF
      ?
      IF oWinPort:RTSFlow( RTS_CONTROL_DISABLE )
         ? "RTSFlow( RTS_CONTROL_DISABLE ) succeeded"
      ELSE
         ? "RTSFlow( RTS_CONTROL_DISABLE ) failed :", oWinPort:Error()
      ENDIF
      ? oWinPort:DebugDCB( WIN_COM_DBGFLOW )
      IF oWinPort:SetRTS( .F. )
         ? "SetRTS( .F. ) succeeded (it should)"
      ELSE
         ? "SetRTS( .F. ) failed :", oWinPort:Error()
      ENDIF
      ?
      ? "Scan something... we'll not read it but purge it, press enter"
      Inkey( 0 )
      IF oWinPort:QueueStatus( @lCTSHold, @lDSRHold, @lDCDHold, @lXoffHold, @lXoffSent, @nInQueue, @nOutQueue )
         ? "QueueStatus() : CTSHold", lCtsHold, ", DSRHold", lDsrHold, ", DCDHold", lDCDHold, ", XoffHold", lXoffHold, ;
                 ", Xoff Sent ", lXoffSent, ", InQueue ", nInQueue, ", nOutQueue ", nOutQueue
      ELSE
         ? "QueueStatus() failed :", oWinPort:Error()
      ENDIF
      IF oWinPort:Purge( .T., .T. )
         ? "Purge() succeeded"
      ELSE
         ? "Purge() failed :", oWinPort:Error()
      ENDIF
      ? "InQueue should be zero"
      IF oWinPort:QueueStatus( @lCTSHold, @lDSRHold, @lDCDHold, @lXoffHold, @lXoffSent, @nInQueue, @nOutQueue )
         ? "QueueStatus() : CTSHold", lCtsHold, ", DSRHold", lDsrHold, ", DCDHold", lDCDHold, ", XoffHold", lXoffHold, ;
                 ", Xoff Sent ", lXoffSent, ", InQueue ", nInQueue, ", nOutQueue ", nOutQueue
      ELSE
         ? "QueueStatus() failed :", oWinPort:Error()
      ENDIF
      ?
      ? "Read ", oWinPort:Read( @cString, 32 ), Len( cString ), cString
      ?
      ? "Close", oWinPort:Close()
   ENDIF

   ?
   ? "This is going to fail, so no device needed"
   Inkey( 0 )

   oWinPort := Win_Com():Init( cPortName, CBR_9600, NOPARITY, 99, ONESTOPBIT )
   IF !oWinPort:Open
      ? "Open() failed :", oWinPort:Error()
   ELSE
      ? "Open succeeded"
      ? "Close", oWinPort:Close()
   ENDIF

   ? "Attach a printer now ..."
   ?
   Inkey( 0 )

   oWinPort := Win_Com():Init( cPortName, CBR_9600, NOPARITY, 8, ONESTOPBIT )
   IF ! oWinPort:Open
      ? "Open() failed :", oWinPort:Error()
   ELSE
      ? "Open succeeded"
      ?
      ? oWinPort:DebugDCB( WIN_COM_DBGFLOW )
      ? "Printers will probably have CTS, DSR and DCD high, IF they are off they'll all be low"
      IF oWinPort:Status( @lCTS, @lDSR, @lRing, @lDCD )
         ? "Status() succeeded : CTS ", lCTS, ", DSR ", lDSR, ", Ring ", lRing, ", DCD ", lDCD
      ELSE
         ? "Status() failed :", oWinPort:Error()
      ENDIF
      ?

      ? "With no flow control the write should succeed whether printer is on or off"
      cString := "this is a test string " + Replicate( "012356789", 30 ) + Chr( 13 ) + Chr( 10 )
      IF ( nResult := oWinPort:Write( cString ) ) == Len( cString )
         ? "Write() succeeded"
      ELSE
         ? "Write() failed, returned ", nResult, " expected ", Len( cString )
      ENDIF
      IF oWinPort:QueueStatus( @lCTSHold, @lDSRHold, @lDCDHold, @lXoffHold, @lXoffSent, @nInQueue, @nOutQueue )
         ? "QueueStatus() : CTSHold", lCtsHold, ", DSRHold", lDsrHold, ", DCDHold", lDCDHold, ", XoffHold", lXoffHold, ;
                 ", Xoff Sent ", lXoffSent, ", InQueue ", nInQueue, ", nOutQueue ", nOutQueue
      ELSE
         ? "QueueStatus() failed :", oWinPort:Error()
      ENDIF

      ?
      ? "Switch all handshaking on (normally only hardware and RTS)"
      IF oWinPort:RTSFlow( DTR_CONTROL_HANDSHAKE )
         ? "RTSFlow( RTS_CONTROL_HANDSHAKE ) succeeded"
      ELSE
         ? "RTSFlow( RTS_CONTROL_HANDSHAKE ) failed :", oWinPort:Error()
      ENDIF
      IF oWinPort:DTRFlow( DTR_CONTROL_HANDSHAKE )
         ? "DTRFlow( DTR_CONTROL_HANDSHAKE ) succeeded"
      ELSE
         ? "DTRFlow( DTR_CONTROL_HANDSHAKE ) failed :", oWinPort:Error()
      ENDIF
      IF oWinPort:XonXoffFlow( .T. )
         ? "XonXoffFlow( .T. ) ", oWinPort:XonXoffFlow( .T. )
      ELSE
         ? "XonXoffFlow( .T. ) failed :", oWinPort:Error()
      ENDIF
      ?
      ? oWinPort:DebugDCB( WIN_COM_DBGFLOW )

      ? "If it's on then no Hold status should be on, IF off then probably CTS and DSR"
      IF oWinPort:QueueStatus( @lCTSHold, @lDSRHold, @lDCDHold, @lXoffHold, @lXoffSent, @nInQueue, @nOutQueue )
         ? "QueueStatus() : CTSHold", lCtsHold, ", DSRHold", lDsrHold, ", DCDHold", lDCDHold, ", XoffHold", lXoffHold, ;
                 ", Xoff Sent ", lXoffSent, ", InQueue ", nInQueue, ", nOutQueue ", nOutQueue
      ELSE
         ? "QueueStatus() failed :", oWinPort:Error()
      ENDIF
      ?
      ? Seconds()
      IF ( nResult := oWinPort:Write( cString ) ) == Len( cString )
         ? "Write() succeeded"
      ELSE
         ? "Write() failed, returned ", nResult, " expected ", Len( cString )
      ENDIF
      ? Seconds()
      ? "Close", oWinPort:Close()
   ENDIF

   RETURN 

Next time I discovered power of HMG and Harbour!

Best regards, Marek.
User avatar
mol
Posts: 3825
Joined: Thu Sep 11, 2008 5:31 am
Location: Myszków, Poland
Contact:

Re: Serial communication via RS232

Post by mol »

I wanna ask, if somebody know how to get list of com ports installed in system. Maybe from windows registry?
Any idea how to do it?


regards, Marek
User avatar
dhaine_adp
Posts: 457
Joined: Wed Aug 06, 2008 12:22 pm
Location: Manila, Philippines

Re: Serial communication via RS232

Post by dhaine_adp »

Hi Marek,

Please visit this link: http://logix4u.net/Legacy_Ports/Paralle ... _BIOS.html

Although I have doubt if that would help but never the less posted it anyway....

Regards,

Danny
Regards,

Danny
Manila, Philippines
Post Reply