Subj : Re: serial comm? To : borland.public.cpp.borlandcpp From : "Paul Wearne" Date : Fri Mar 05 2004 01:45 pm The attached files might help. I use the PC's serial port to simulate the serial port of an Microcontroller, therefore the file names may seem a bit confusing. It does Rx and Tx with no hardware handshaking. The received data is buffered by windows. Hope this helps. /*************************************************************************** * * * FILENAME: $Workfile:$ * * DESCRIPTION: * **************************************************************************** * CHANGE HISTORY: PVCS automatically inserts check in information. **************************************************************************** * $Log:$ ***************************************************************************/ /* LIBRARY INCLUDE FILES */ #include #include /* PROJECT INCLUDE FILES */ #include "sci0.h" /* LOCAL SYMBOLIC CONSTANTS */ /* LOCAL MACROS DEFINITIONS */ /* LOCAL ENUM DEFINITIONS */ /* LOCAL STRUCT DEFINITIONS */ /* LOCAL TYPE DEFINITIONS */ /* GLOBAL VARIABLE DECLARATIONS */ /* LOCAL VARIABLE DECLARATIONS */ static HANDLE h_comm = NULL; static OVERLAPPED overlapped; /* LOCAL FUNCTION PROTOTYPES */ /* FUNCTION DEFINITIONS */ /*************************************************************************** * * FUNCTION: SCI0_Initialise * DESCRIPTION: * Configures the SCI peripheral device for asynchronous serial * communications. The SCI shall be configured at the specified baud rate, * with 8 bits, 1 stop bit and no parity. * Parameters list:- * U16 baud. This is the asynchronous baud rate, typical values are:- * 110, 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 38400 * Return Value * BOOL If TRUE the SCI has been successfully configured. * IF FALSE the SCI could not be configured. * **************************************************************************** * Modification History **************************************************************************** * Initial Implementation ***************************************************************************/ BOOL SCI0_Initialise(U16 baud) { DCB dcb; COMMTIMEOUTS timeouts; CHAR dcb_string[20]; if (h_comm !=NULL) { CloseHandle(h_comm); } for (;;) { /* Open the comm port */ h_comm = CreateFile( "COM1", GENERIC_WRITE | GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_FLAG_OVERLAPPED | FILE_FLAG_WRITE_THROUGH, NULL ); /* Check its OK */ if (h_comm == INVALID_HANDLE_VALUE) { /* we are getting access denied, */ /* dwError = GetLastError(); */ } else { break; } } /* Setup parameters */ FillMemory(&dcb, sizeof(dcb), 0); dcb.DCBlength = sizeof(dcb); sprintf(dcb_string, "%u,n,8,1", baud); BuildCommDCB(dcb_string, &dcb); /* Check setup OK */ if (SetCommState(h_comm, &dcb)) { timeouts.ReadIntervalTimeout = MAXDWORD; timeouts.ReadTotalTimeoutConstant = 0; timeouts.ReadTotalTimeoutMultiplier = 0; timeouts.WriteTotalTimeoutConstant = 0; timeouts.WriteTotalTimeoutMultiplier = 0; SetCommTimeouts (h_comm, &timeouts); SetupComm( h_comm, 1600, 1600 ); (void) memset( &overlapped, 0, sizeof(overlapped) ); } return TRUE; } /*************************************************************************** * * FUNCTION: SCI0_Read * DESCRIPTION: * Reads up to in_buffer_length bytes into the supplied in_buffer_ptr, * providing there is data in the SCI's peripheral receive buffer. * If in_buffer_ptr is null no data is placed in in_buffer_ptr. The SCI's * buffer is still emptied and the number of bytes emptied is return. * Parameters list:- * U8* in_buffer_ptr. Buffer in which the received data should be placed. * U16 in_buffer_length. The size of in_buffer_ptr. * Return Value * U16 Returns the number of bytes placed into the in_buffer_ptr. * **************************************************************************** * Modification History **************************************************************************** * Initial Implementation ***************************************************************************/ U16 SCI0_Read(U8* in_buffer_ptr, U16 in_buffer_length) { U16 bytes_read = 0; U32 num_bytes_read = 0; if ( ReadFile(h_comm, in_buffer_ptr, in_buffer_length, &num_bytes_read, &overlapped ) ) { bytes_read = (U16) num_bytes_read; } return(bytes_read); } /*************************************************************************** * * FUNCTION: SCI0_Write * DESCRIPTION: * Writes the contents of buffer specified by out_buffer_ptr into the SCI's * peripheral transmit buffer. If the SCI's transmit buffer is full it * shall wait until the SCI has emptied the buffer before continuing to load * the transmit buffer. * Parameters list:- * const U8* out_buffer_ptr. Buffer which contains the data for transmission. * U16 out_buffer_length. Number of elements in the out_buffer_ptr which require transmission. * Return Value * none * **************************************************************************** * Modification History **************************************************************************** * Initial Implementation ***************************************************************************/ void SCI0_Write(const U8* out_buffer_ptr, U16 out_buffer_length) { U16 buf_idx; U32 num_bytes_written; WriteFile (h_comm, out_buffer_ptr, out_buffer_length, &num_bytes_written, &overlapped); } /* EOF */ "ge" wrote in message news:qaug401qa301loo6o6gs2fh1f67ms00v03@4ax.com... > using Borland C++ 5.0 (I have 5.5, too, but not installed) > Win NT4 > > I need to do some simple serial I/O (COM1) in a win32 console. I've > looked around, and what I've found so far is confusing. If there is > some "all you have to do is ..." URL, I'd appreciate a pointer to it. > > TIA, > George .