top of page

Group

Public·17 members
Andrew Scott
Andrew Scott

Visual Foxpro Serial Communication Port



This article gives you two techniques you can use to receive data from the serial port using the Mscomm32.ocx control. The first uses an event-driven method and does not require you to poll the serial port to check for the presence of received characters. This technique allows the most flexibility and does not require extensive coding to prevent buffer over-runs. The second technique requires you to poll the input buffer periodically to check for the presence of received characters. This article describes these two techniques and provides examples for each.




visual foxpro serial communication port



CommPort property: Set this numeric property to the desired communications port. Valid values are 1, 2, 3, or 4 depending on the serial ports available and the configuration of the individual computer. These values correspond to Com1, Com2, Com3, and Com4 respectively.


Settings property: Set this character property to the baud rate, Parity, Data Bits, and Stop Bits required by the device connected to the serial port. This property is a character, comma-delimited list. For example, to set the serial port to 14,400 baud, Even Parity, 7 Data Bits, and 1 Stop Bit, set the string to: 14400,E,7,1.


The procedure code could be called in a timer method to facilitate checking for characters at semi-regular intervals. However, if large amounts of data are expected to be received from the serial port, Technique One will reduce the chance of over-running the input buffer.


ramani (Subramanian.G),FoxAcc, ramani_g@yahoo.com RE: Enable serial port. weedz (Programmer)7 Dec 01 04:18You can also use the MSCOMM32.OCX active X control to read and write to comm ports.HTH, Weedz (Wietze Veld) My private project:www.crowncap.demon.nl\info\crwnbaseDownload the CrownBase source code !! RE: Enable serial port. rgbean (Programmer)7 Dec 01 14:07Some sample code for accessing serial data in VFP using MSCOMM32 can be found in the following: - HOWTO: Receive from the Serial Port by Using MScomm32.ocx.Also useful may be - HOWTO: Send to the Serial Port by Using Mscomm32.ocx.And of course, - HOWTO: Transmit and Receive Binary Data using MSCOMM32.If MSCOMM32 doesn't provide you with enough control, you'll need to find another 3rd party control to handle the serial communication. Remember that in the Windows environment, it controls the I/O ports - you can't do it directly - especially in WinNT, Win2000 and XP.Rick RE: Enable serial port. rianeiromiron (Programmer)12 Dec 01 01:37I think one can manage most RS-232 functions using MSCOMM32. No kidding. Only as an example of the funcionality of the MSCOMM32.OCX:1) I created a program to read data from a Panasonic Telephone Plant (A PBX), which uses standard data streams that can also be read using Hyperterminal.2) I created a program to control a security device in a CAT system (yes, a tomography). The device was custom made by an electronics egineer, but since the engineer had no knowledge at all of programming technics, all he had to do was to tell me what pins he was going to turn on or off and in what events.The MSCOMM32 is well documented at the Microsoft sites (check for Rick's links). Even though the examples are written in Visual Basic, theye are well understood and can easily be translated into VFP.However you could take Rick's advise and get a third-party software in case MSCOMM32 does not work.How can you include the MSCOMM32 in your project?Open a form, say, MAIN.Choose Tools, Options, Controls, Active X Controls, then check Microsoft Communications Control. Choose Accept.Now in the See Classes (I have the spanish version of VFP, so I'm translating as well as I can) button, choose Active X Controls. Now you will see a telephone icon. Click on it and then click anywhere in your form.You might also want to take a look at the serial ports implementation (wiring), null modems, and that sort of things. Just type "serial port" in google.com or whatever your search engine is. You could also go directly to -port.htmThey have pictures and diagrams an explanations for it.Hope that helps Rianeiro Mirón RE: Enable serial port. rianeiromiron (Programmer)12 Dec 01 01:40PART 1I think one can manage most RS-232 functions using MSCOMM32. No kidding. Only as an example of the funcionality of the MSCOMM32.OCX:1) I created a program to read data from a Panasonic Telephone Plant (A PBX), which uses standard data streams that can also be read using Hyperterminal.2) I created a program to control a security device in a CAT system (yes, a tomography). The device was custom made by an electronics egineer, but since the engineer had no knowledge at all of programming technics, all he had to do was to tell me what pins he was going to turn on or off and in what events.The MSCOMM32 is well documented at the Microsoft sites (check for Rick's links). Even though the examples are written in Visual Basic, theye are well understood and can easily be translated into VFP.However you could take Rick's advise and get a third-party software in case MSCOMM32 does not work. googletag.cmd.push(function() googletag.display('div-gpt-ad-1406030581151-2'); ); Red Flag This PostPlease let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.CancelRed Flag SubmittedThank you for helping keep Tek-Tips Forums free from inappropriate posts.The Tek-Tips staff will check this out and take appropriate action.


Hi, I am seeking a way to pull data through a serial port from a weigh scale. Would you be willing to show me what the code should look like in vb? I am using vb6. I would appreciate the help as this is something I have to figure out now. Thanks.


Hi Kamron, I too am seeking a way to pull data through a serial port from a weigh scale. Would you be willing to show me what the code should look like in vb? I am using vb6. I would appreciate the help as this is something I have to figure out now. Thanks.


It is completely written in foxpro and is known to work in VFP5 and VFP6. I use a completely different approach to serial IO for VFP3 on Macintosh, which involves calling my own C++ code. On the Windows platform, I call the following code from another program to open a cash drawer attached to the serial port. I just need to spit out 450 NUL characters to hold the transmit line high for the requisite number of milliseconds in order to fire the solenoid to open the cash drawer. Because of my minimalist requirements, I have not implemented nicieties such as flow control. Did somebody say there was third party code to do serial IO? Here is the PRG file from my library. Danny to e-mail me, remove the SPAMBEGONE from my address. "What we need is either less corruption, or more opportunity to participate in it." -------------------------------------- ************************************************************ * * A library of goodies to access low level Win32API's. * In addition to providing access to some "raw" low * level capabilities, this program also provides nice * high-level sugar-coated interfaces to those capabilities. * * In order to use this library, you need to... * * SET PROCEDURE TO LibWin32API ADDITIVE * * * Revision History: * 10/28/99 DB Written. * * DB = Danny Brewer * ************************************************************ * * Define low-level Win32API constants. * #define W32API_GENERIC_READ (0x80000000) #define W32API_GENERIC_WRITE (0x40000000) #define W32API_FILE_SHARE_READ (0x00000001) #define W32API_FILE_SHARE_WRITE (0x00000002) #define W32API_CREATE_NEW 1 #define W32API_CREATE_ALWAYS 2 #define W32API_OPEN_EXISTING 3 #define W32API_OPEN_ALWAYS 4 #define W32API_TRUNCATE_EXISTING 5 #define W32API_FILE_ATTRIBUTE_READONLY 0x00000001 #define W32API_FILE_ATTRIBUTE_HIDDEN 0x00000002 #define W32API_FILE_ATTRIBUTE_SYSTEM 0x00000004 #define W32API_FILE_ATTRIBUTE_DIRECTORY 0x00000010 #define W32API_FILE_ATTRIBUTE_ARCHIVE 0x00000020 #define W32API_FILE_ATTRIBUTE_NORMAL 0x00000080 #define W32API_FILE_ATTRIBUTE_TEMPORARY 0x00000100 #define W32API_FILE_FLAG_WRITE_THROUGH 0x80000000 #define W32API_FILE_FLAG_RANDOM_ACCESS 0x10000000 *------------------------------ * * This class is used for performing low level IO through * the Win32 API's. * The reason this was written was to communicate through * the serial port so that we could open a cash drawer. * This could be used to communicate with anything that * can be opened with the Win32 CreateFile call. * * Before you can use this class, you must first call * the procedure DeclareWin32APIs() to declare the * low level Win32 API routines. * * The way this is used to talk to a serial port is as * follows... * * DO DeclareWin32APIs * LOCAL oObj * oObj = CREATEOBJECT( "LowLevelIO", "Com1" ) * oObj.SetSerialParamStr( "baud=9600 data=8 stop=1 parity=n" ) * oObj.Write( "This is a test" + CHR(13) + CHR(10) ) * oObj = .NULL. && release the object * * Revision History: * 10/28/99 DB Written. * * DB = Danny Brewer * DEFINE CLASS LowLevelIO AS Custom PROTECTED mlIsOpen, mnHandle mlIsOpen = .F. mnHandle = -1 FUNCTION Init( pcName ) IF TYPE([pcName]) == 'C' RETURN THIS.Open( pcName ) ENDIF ENDFUNC PROCEDURE Destroy() THIS.Close() ENDPROC * To open a serial port, pass a parameter such as: "Com1" FUNCTION Open( pcName ) THIS.Close() LOCAL nHandle nHandle = Win32API_CreateFile( ; pcName, ; W32API_GENERIC_WRITE + W32API_GENERIC_READ, ; 0, ; 0, ; W32API_OPEN_EXISTING, ; 0, ; 0 ) IF nHandle != -1 THIS.mlIsOpen = .T. THIS.mnHandle = nHandle ENDIF RETURN THIS.mlIsOpen ENDFUNC PROCEDURE Close() IF THIS.mlIsOpen =Win32API_CloseHandle( THIS.mnHandle ) THIS.mnHandle = -1 THIS.mlIsOpen = .F. ENDIF ENDPROC FUNCTION IsOpen() RETURN THIS.mlIsOpen ENDFUNC FUNCTION Write( pcStringToWrite ) IF THIS.mlIsOpen LOCAL nBytesToWrite, nNumBytesActuallyWritten nBytesToWrite = LEN( pcStringToWrite ) nNumBytesActuallyWritten = -1 LOCAL nBool nBool = Win32API_WriteFile( THIS.mnHandle, ; IF nBool = 1 RETURN (nNumBytesActuallyWritten = nBytesToWrite) ENDIF ENDIF RETURN .F. ENDFUNC * Make sure that everything gets written out NOW! FUNCTION Flush() IF THIS.mlIsOpen LOCAL nBool nBool = Win32API_FlushFileBuffers( THIS.mnHandle ) RETURN (nBool = 1) ENDIF RETURN .F. ENDFUNC * Attempt to read the number of bytes in pnNumBytes. * The parameter rnBytesRead returns the number of bytes * that were *actually* read. * The parameter rcCharsRead returns the actual bytes read. * This function returns .T. if successful. FUNCTION ReadNumBytes( pnNumBytes, rnBytesRead, rcCharsRead ) rnBytesRead = 0 rcCharsRead = "" IF THIS.mlIsOpen LOCAL cBuffer, nNumBytesActuallyRead cBuffer = REPLICATE( CHR(0), pnNumBytes ) nNumBytesActuallyRead = 0 LOCAL nBool nBool = Win32API_ReadFile( THIS.mnHandle, ; IF nBool = 1 rnBytesRead = nNumBytesActuallyRead rcCharsRead = cBuffer RETURN .T. ENDIF ENDIF RETURN .F. ENDFUNC *----- * If you're communicating with a serial device, * for instance if you opened this object with a name * such as "Com1", then you may call this function to * adjust the communication settings. * Pass this routine a string such as * "baud=9600 data=8 stop=1 parity=n" * like you would use on the DOS MODE command. * You may also use a string of the form: * "96,n,8,1" * or "96,n,8,1,x" * or "96,n,8,1,p" (hardware flow control) * FUNCTION SetSerialParamStr( pcSerialParams ) IF THIS.mlIsOpen LOCAL cDCB cDCB = THIS.GetCommStateDCB() IF LEN( cDCB ) > 0 LOCAL nBool RETURN THIS.SetCommStateDCB( cDCB ) ENDIF ENDIF RETURN .F. ENDFUNC * Get the last error from the Win32 API. * FUNCTION GetLastError() RETURN Win32API_GetLastError() ENDFUNC *-------------------------------------------------- * Private functions follow *-------------------------------------------------- *----- * This returns the empty string if unsuccessful. * If successful, this returns the current DCB * from GetCommState. The DCB is returned as a string * which represents the bytes in the DCB structure. FUNCTION GetCommStateDCB() IF THIS.mlIsOpen * A DCB is a structure that contains 80 bytes. PUBLIC cDCB cDCB = REPLICATE( CHR(0), 80 ) LOCAL nBool IF nBool = 1 RETURN cDCB ENDIF ENDIF RETURN "" ENDFUNC *----- * You must pass in a valid DCB structure !!!! * You can get one by calling GetCommStateDCB above. * You can make modifications to the DCB structure * and then make those changes take effect by * passing the modified DCB back to this procedure. * This returns .T. if successful. FUNCTION SetCommStateDCB( pcDCB ) IF THIS.mlIsOpen LOCAL nBool RETURN (nBool = 1) ENDIF RETURN .F. ENDPROC ENDDEFINE *----- * Declare low-level Win32API routines. * PROCEDURE DeclareWin32APIs() DECLARE Long CreateFile IN Win32API AS Win32API_CreateFile ; String lpFileName, ; Long dwDesiredAccess, ; Long dwShareMode, ; Long lpSecurityAttributes, ; Long dwCreationDistribution, ; Long dwFlagsAndAttributes, ; Long hTemplateFile DECLARE Short CloseHandle IN Win32API AS Win32API_CloseHandle ; Long hObject DECLARE Short ReadFile IN Win32API AS Win32API_ReadFile ; Long hFile, ; Long nNumberOfBytesToRead, ; Long lpOverlapped DECLARE Short WriteFile IN Win32API AS Win32API_WriteFile ; Long hFile, ; Long nNumberOfBytesToWrite, ; Long lpOverlapped DECLARE Short FlushFileBuffers IN Win32API AS Win32API_FlushFileBuffers ; Long hFile DECLARE Short GetCommState IN Win32API AS Win32API_GetCommState ; Long hFile, ; DECLARE Short SetCommState IN Win32API AS Win32API_SetCommState ; Long hFile, ; DECLARE Short BuildCommDCB IN Win32API AS Win32API_BuildCommDCB ; String lpDef, ; DECLARE Long GetLastError IN Win32API AS Win32API_GetLastError DECLARE Short DeleteFile IN Win32API AS Win32API_DeleteFile ; String lpFileName ENDPROC Quote:> Years ago I wrote some FoxPro DOS programs to do data acquisition from > laboratory instruments - electronic calipers, balances, and the like. > Because FoxPro low-level file functions missed input from some > instruments I bought a serial communications tools package called > CometMP and used that for buffering the serial ports. This combination > worked flawlessly, When I had to move the programs to Windows 3.x > computers I bought FoxPro 2.6 for windows and ran the same code which > continued to work (a surprise to me because I thought that CometMP was > a DOS tool. Now I'm moving the programs again - this time to Windows > 95/98 computers - and though most computers work fine, some hang when I > try to initialize the serial ports. I'm wondering if it's time to get > Visual FoxPro and another package of communications routines and > rewrite these programs. > Is there anyone there who uses FoxPro for data acquisition? Can it > be done with Visual FoxPro alone or will I need a library of serial > communications routines? Any recommendations for communications > libraries - I found something called CommTools but it's pricey and > far more than I need. > Toby Sun, 01 Dec 2002 03:00:00 GMT Page 1 of 1 [ 1 post ] Relevant Pages 1. serial communications and FoxPro


About

Welcome to the group! You can connect with other members, ge...

Members

bottom of page