 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Ramy Guest
|
Posted: Sun May 29, 2005 12:22 pm Post subject: How can i set the Parity Bit to 0 or 1 as i want ? |
|
|
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
}
|
|
| Back to top |
|
 |
Ramy Guest
|
Posted: Sun May 29, 2005 1:27 pm Post subject: Re: How can i set the Parity Bit to 0 or 1 as i want ? |
|
|
Is there any better group to send this question to?
Thanks,
Ramy.
"Ramy" <Ramy (AT) NoMail (DOT) com> wrote:
| Quote: |
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
}
|
|
|
| Back to top |
|
 |
Bob Gonder Guest
|
Posted: Sun May 29, 2005 5:00 pm Post subject: Re: How can i set the Parity Bit to 0 or 1 as i want ? |
|
|
Ramy wrote:
| Quote: | 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?
|
Usual setting is NoParity, which is what you have set.
Change the value to Even, Odd, Mark or Space as you require.
| Quote: | my_DCB.Parity = NOPARITY; // 0-4=no,odd,even,mark,space
|
#define NOPARITY 0
#define ODDPARITY 1
#define EVENPARITY 2
#define MARKPARITY 3
#define SPACEPARITY 4
| Quote: | strcat( com_params, ":9600, N, 8, 1" ); // Prepare COM Parameters
|
Change the N to the proper value.
Not 100% sure about m and s, but probably N, O, E, M, S
|
|
| Back to top |
|
 |
Thomas Maeder [TeamB] Guest
|
Posted: Sun May 29, 2005 6:05 pm Post subject: Re: How can i set the Parity Bit to 0 or 1 as i want ? |
|
|
"Ramy" <Ramy (AT) NoMail (DOT) com> writes:
| Quote: | Is there any better group to send this question to?
|
I don't think so - this seems like the right place for your question.
But please direct your browser at http://info.borland.com/newsgroups/
and read the newsgroup guidelines. One of them asks us not to quote
entire posts we are following up to; instead, please trim the quotes
to the parts relevant for your reply. Thanks!
|
|
| Back to top |
|
 |
Ramy Guest
|
Posted: Mon May 30, 2005 6:59 am Post subject: Re: How can i set the Parity Bit to 0 or 1 as i want ? |
|
|
Thanks, i'll try that :-)
read the newsgroup guidelines. One of them asks us not to quote
| Quote: | entire posts we are following up to; instead, please trim the quotes
to the parts relevant for your reply. Thanks!
|
|
|
| Back to top |
|
 |
Ramy Guest
|
Posted: Mon May 30, 2005 7:13 am Post subject: Re: How can i set the Parity Bit to 0 or 1 as i want ? |
|
|
But you did not answer my question -
Please show me one code for setting the parity bit to '1', and
another code for setting the parity bit to '0', thanks.
Ramy
Bob Gonder <notbg (AT) notmindspring (DOT) invalid> wrote:
| Quote: | Ramy 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?
Usual setting is NoParity, which is what you have set.
Change the value to Even, Odd, Mark or Space as you require.
my_DCB.Parity = NOPARITY; // 0-4=no,odd,even,mark,space
#define NOPARITY 0
#define ODDPARITY 1
#define EVENPARITY 2
#define MARKPARITY 3
#define SPACEPARITY 4
strcat( com_params, ":9600, N, 8, 1" ); // Prepare COM Parameters
Change the N to the proper value.
Not 100% sure about m and s, but probably N, O, E, M, S
|
|
|
| Back to top |
|
 |
Bob Gonder Guest
|
Posted: Mon May 30, 2005 2:50 pm Post subject: Re: How can i set the Parity Bit to 0 or 1 as i want ? |
|
|
Ramy wrote:
| Quote: | But you did not answer my question -
Please show me one code for setting the parity bit to '1', and
another code for setting the parity bit to '0', thanks.
|
To set the parity bit so it is a 1 for every chaaracter, use
MARKPARITY
To set the parity bit so it is a 0 for every character, use
SPACEPARITY.
To automatically set the number of 1 bits so there is always an odd
number, use ODDPARITY (Of the 9 totalt bits (8 data + 1parity),there
will be an odd number of 1 bits).
To automatically set the number of 1 bits so there is always an even
number, use EVENPARITY (Of the 9 totalt bits (8 data + 1parity),there
will be an even number of 1 bits).
Use SetCommState() with the properly modified DCB.
|
|
| Back to top |
|
 |
Ramy Guest
|
Posted: Tue May 31, 2005 8:01 am Post subject: Re: How can i set the Parity Bit to 0 or 1 as i want ? |
|
|
Thanks very much!
| Quote: | To set the parity bit so it is a 1 for every chaaracter, use
MARKPARITY
To set the parity bit so it is a 0 for every character, use
SPACEPARITY.
[...] |
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|