Article View: borland.public.cppbuilder.nativeapi
Article #6243Re: How can i set the Parity Bit to 0 or 1 as i want ?
From: Ramy
Date: Sun, 29 May 2005 13:27
Date: Sun, 29 May 2005 13:27
119 lines
7363 bytes
7363 bytes
Is there any better group to send this question to?
Thanks,
Ramy.
"Ramy" <Ramy@NoMail.com> wrote:
>
>Hi, down below is the function that i use to initiate my com port,
>for some messages i need to set the Parity Bit to '1', and for
>other messages i need to reset this bit to '0', how do i do that?
>
>Thanks!
>
>Ramy.
>
>
>int InitCommunication( void )
>{
> COMMTIMEOUTS read_timeout; // timeout structure
> char com_params[60];
> char ComName[30];
> HANDLE ComHndl = NULL;
> DCB my_DCB; // device control block structure
>
> if ( SystemInfo.ErrorInfo.Status != NO_ERRORS ) // If Error -> Exit
> return 0;
>
> if ( SystemInfo.ComInfo.ComHandle ) // If Port already open
> {
> CloseHandle( SystemInfo.ComInfo.ComHandle ); // Close Port
> SystemInfo.ComInfo.ComHandle = NULL; // Reset Port Handle
> }
>
> my_DCB.DCBlength= sizeof( my_DCB ); // Sizeof(DCB)
> my_DCB.BaudRate = SystemInfo.ComInfo.BaudRate; // Current baud rate
> my_DCB.fBinary = true; // Binary mode, no EOF check
> my_DCB.ByteSize = 8; // Number of bits/byte, 4-8
> my_DCB.Parity = NOPARITY; // 0-4=no,odd,even,mark,space
> my_DCB.StopBits = 1; // 0, 1, 2 = 1, 1.5, 2
> my_DCB.fOutxCtsFlow = 0; // CTS output flow control
> my_DCB.fOutxDsrFlow = 0; // DSR output flow control
> my_DCB.fDtrControl = DTR_CONTROL_ENABLE; // DTR flow control type
> my_DCB.fDsrSensitivity = 0; // DSR sensitivity
> my_DCB.fTXContinueOnXoff = 0; // XOFF continues Tx
> my_DCB.fOutX = 0; // XON/XOFF out flow control
> my_DCB.fInX = 0; // XON/XOFF in flow control
> my_DCB.fErrorChar = 0; // Enable error replacement
> my_DCB.fNull = false; // Enable null stripping
> my_DCB.fRtsControl = RTS_CONTROL_ENABLE; // RTS flow control
> my_DCB.fAbortOnError = 0; // Abort reads/writes on error
> my_DCB.XonLim = 50; // Transmit XON threshold
> my_DCB.XoffLim = 50; // Transmit XOFF threshold
> my_DCB.XonChar = 0; // Tx and Rx XON character
> my_DCB.XoffChar = 0; // Tx and Rx XOFF character
> my_DCB.ErrorChar = 0; // Error replacement character
> my_DCB.EofChar = 0; // End of input character
> my_DCB.EvtChar = 0; // Received event character
> read_timeout.ReadIntervalTimeout = 0; // Read Interval Timeout
> read_timeout.ReadTotalTimeoutMultiplier = 0; // Read Total Timeout Multiplier
> read_timeout.ReadTotalTimeoutConstant = SystemInfo.ComInfo.RxTimeout; // Read Total Timeout Constant
> read_timeout.WriteTotalTimeoutMultiplier = 0; // Write Total Timeout Multiplier
> read_timeout.WriteTotalTimeoutConstant = SystemInfo.ComInfo.TxTimeout; // Write Total Timeout Constant
> strcpy( com_params, SystemInfo.ComInfo.ComPort ); // Prepare com_params
> strcpy( ComName, SystemInfo.ComInfo.ComPort ); // Set ComName
>
> switch ( SystemInfo.ComInfo.BaudRate )
> {
> case CBR_9600:
> strcat( com_params, ":9600, N, 8, 1" ); // Prepare COM Parameters
> break;
>
> case CBR_14400:
> strcat( com_params, ":14400, N, 8, 1" ); // Prepare COM Parameters
> break;
>
> case CBR_19200:
> strcat( com_params, ":19200, N, 8, 1" ); // Prepare COM Parameters
> break;
>
> case CBR_38400:
> strcat( com_params, ":38400, N, 8, 1" ); // Prepare COM Parameters
> break;
>
> case CBR_56000:
> strcat( com_params, ":56000, N, 8, 1" ); // Prepare COM Parameters
> break;
>
> case CBR_57600:
> strcat( com_params, ":57600, N, 8, 1" ); // Prepare COM Parameters
> break;
>
> case CBR_115200:
> strcat( com_params, ":115200, N, 8, 1" ); // Prepare COM Parameters
> break;
> };
>
> ComHndl = CreateFile( ComName, GENERIC_WRITE + GENERIC_READ, 0, NULL, // Open the asked Com Port
> OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL );
>
> if ( ComHndl == INVALID_HANDLE_VALUE ) // Check COM Handle
> return FAIL;
>
> if ( !BuildCommDCB( com_params, &my_DCB ) ) // Fills DCB Structure with Com Values
> return FAIL;
>
> if ( !SetCommState( ComHndl, &my_DCB ) ) // Configures Com Port
> return FAIL;
>
> if ( !SetCommTimeouts( ComHndl, &read_timeout ) ) // Set Tx & Rx Timeouts
> return FAIL;
>
> SystemInfo.ComInfo.ComHandle = ComHndl; // Set Communication Handle
>
> return PASS; // COM Init was successful
>}
>
Technical Details
Message-ID:
<4299c349$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!feed5.newsreader.com!newsreader.com!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mailReferences:
<4299b415$1@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,cb603903b4ecd1d3 X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Ramy" <Ramy@NoMail.com> Sender: "Ramy" <Ramy@NoMail.com> Reply-To: "Ramy" <Ramy@NoMail.com> Subject: Re: How can i set the Parity Bit to 0 or 1 as i want ? Newsgroups: borland.public.cppbuilder.nativeapi X-User-Info: 62.0.27.159 References: <4299b415$1@newsgroups.borland.com> NNTP-Posting-Host: 207.105.83.66 Message-ID: <4299c349$1@newsgroups.borland.com> Date: 29 May 2005 06:27:37 -0700 X-Trace: newsgroups.borland.com 1117373257 207.105.83.66 (29 May 2005 06:27:37 -0700) Lines: 120 Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!border1.nntp.dca.giganews.com!border2.nntp.dca.giganews.com!nntp.giganews.com!feed5.newsreader.com!newsreader.com!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail