Serial communication in Embedded C Linux: tcsetattr() The 2019 Stack Overflow Developer Survey Results Are InHow do I prompt for Yes/No/Cancel input in a Linux shell script?How to symlink a file in Linux?How do I change permissions for a folder and all of its subfolders and files in one step in Linux?How to change the output color of echo in LinuxHow can I use grep to show just filenames on Linux?Recursively counting files in a Linux directoryHow do I copy folder with files to another folder in Unix/Linux?How do I find all files containing specific text on Linux?Typecasting bytes properly for writing to a serial handle in C++Why does the C preprocessor interpret the word “linux” as the constant “1”?

The difference between dialogue marks

How can I autofill dates in Excel excluding Sunday?

One word riddle: Vowel in the middle

Should I use my personal e-mail address, or my workplace one, when registering to external websites for work purposes?

Is an up-to-date browser secure on an out-of-date OS?

Where to refill my bottle in India?

Falsification in Math vs Science

How are circuits which use complex ICs normally simulated?

Can we generate random numbers using irrational numbers like π and e?

Can one be advised by a professor who is very far away?

What do the Banks children have against barley water?

Is bread bad for ducks?

What did it mean to "align" a radio?

Aging parents with no investments

Can you compress metal and what would be the consequences?

Is there any way to tell whether the shot is going to hit you or not?

Why can Shazam fly?

Do these rules for Critical Successes and Critical Failures seem Fair?

Worn-tile Scrabble

What are the motivations for publishing new editions of an existing textbook, beyond new discoveries in a field?

How to save as into a customized destination on macOS?

Why hard-Brexiteers don't insist on a hard border to prevent illegal immigration after Brexit?

Delete all lines which don't have n characters before delimiter

How to type this arrow in math mode?



Serial communication in Embedded C Linux: tcsetattr()



The 2019 Stack Overflow Developer Survey Results Are InHow do I prompt for Yes/No/Cancel input in a Linux shell script?How to symlink a file in Linux?How do I change permissions for a folder and all of its subfolders and files in one step in Linux?How to change the output color of echo in LinuxHow can I use grep to show just filenames on Linux?Recursively counting files in a Linux directoryHow do I copy folder with files to another folder in Unix/Linux?How do I find all files containing specific text on Linux?Typecasting bytes properly for writing to a serial handle in C++Why does the C preprocessor interpret the word “linux” as the constant “1”?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I am having problems with changing character size and parity bit in serial communication between 2 serial ports.



Currently, after opening serial ports, I want to change character size, stop bit and parity. If character size is 7bit, 8bit, writing and reading between 2 ports works fine. However, with character size of 5bit, 6bit and enable parity are not.



I'm thinking of tcsetattr() function which might set limit 7bit, 8bit in low layer linux kernel. I tried to debug around but I couldn't observe anything.



Could you guys give me any suggestion?



P/S: HW supports for whole setting.



Thanks so much!



My code as follows:



menu_serialparametersR()

printf ("Setting:n")
printf ("[Character size] [Stop bit] [Parity] [Flow control]n");
printf (" 1.5bit 5.1bit 7.Enable-Odd 10. HW n");
printf (" 2.6bit 6.2bit 8.Enable-Even 11. SW n");
printf (" 3.7bit 9.Disable n");
printf (" 4.8bit n");


setup_serial_port(int uart, unsigned char *uart_name)
CREAD);
tty.c_iflag

process_data(int fd1, int fd2, unsigned char *uart1_n, unsigned char *uart2_n)

int getatt_tst1, getatt_tst2, successdata;
struct termios tty1, tty2;

successdata = 0;

// Get attribute to print out parameters
getatt_tst1 = tcgetattr(fd1, &tty1);
getatt_tst2 = tcgetattr(fd2, &tty2);
if (getatt_tst1 < 0)

printf("Error [%d] from tcgetattr in %s: %sn", getatt_tst1, uart1_n, strerror(errno));
return;

if (getatt_tst2 < 0)

printf("Error [%d] from tcgetattr in %s: %sn", getatt_tst2, uart2_n, strerror(errno));
return;


/*** Writing random data to enable read function in fd2 ***/
// Fixed position. Don't move it after.

if ((wr_size = write (fd2, "a",1))<0)

printf ("Error [%d] while writing in %s: %sn",wr_size, uart2_n, strerror(errno));
return;


ntestdata = 100; // Just random number

successdata = write_read_byte(ntestdata, fd1, fd2, uart1_n, uart2_n);
if (successdata == ntestdata)

printf ("Finish Write/Read %d byte successful.n", successdata);

else
printf ("Failed. Only finish Write/Read %d byte successful.n", successdata);
return;


// Define random array for testing (ensure that length's above 8)
char test_data[]= "ABCDEFGHIJK12345678";

int write_read_byte(int num_testbytes, int fd1, int fd2,
unsigned char *uart1_name, unsigned char *uart2_name)

// VARIABLES
int wr_size, rd_size, array_i, rdcounter, wrcounter;
char *buf_ptr;
char buf_read[0x800];
int testbyte, successbyte;

// Initializations
wr_size = 0; // Write size
rd_size = 0; // Read size
array_i = 0; // Element index in testing array
rdcounter = 0; // Read counter
wrcounter = 0; // Write counter
successbyte = 0; // amount of byte write/read successful

memset (buf_read, 0 , 0x800);
testbyte = num_testbytes;

/**************** Start testing *************************/
while (testbyte > 0) // Do loop until num_testbytes

// Reset counter
rdcounter = 0;
wrcounter = 0;

// 1. Start writing 1 byte to fd1

wr_size = write (fd1, &test_data[array_i], 1);
printf ("Write: %c", test_data[array_i]);

if (wr_size < 0)

printf ("Error [%d] while writing in %s: %s. n", wr_size, uart2_name, strerror(errno));
return successbyte;


// 2. Start reading wr_size byte from fd2
while (wrcounter < wr_size) // Listening enough written byte

// Reset everything before read 1 byte
buf_ptr = 0;
rdcounter = 0;
rd_size = read(fd2, &buf_ptr, 1);
if (rd_size > 0)

for (rdcounter = 0; rdcounter < rd_size; rdcounter++)

printf (" - Read: %cn", buf_ptr); // using for testing
// Copy 1 read byte to read array
memcpy(&buf_read[(num_testbytes - testbyte)], &buf_ptr, 1);
// If reading right data
if (buf_read[(num_testbytes - testbyte)] == test_data[array_i])
successbyte = (successbyte + 1);
else // Wrong read byte

printf ("nTesting failed. Reason: Read wrong data in %s.nPlease check for configuration.n", uart2_name);
return successbyte;



else if (rd_size < 0)

printf ("nTesting failed. Reason: Error [%d] while reading in %s: %sn",
rd_size, uart2_name, strerror(errno));
printf ("Please check for:n1. Connection between 2 portsn2. Configurationn");
return successbyte;

else

printf ("nTesting failed. Reason: Timeout for reading in %s.n", uart2_name );
printf ("Please check for:n1. Connection between 2 portsn2. Configurationn");
return successbyte;

wrcounter = (wrcounter + 1) ;


testbyte = (testbyte-1);

// Limit test data element index
if (array_i >= (sizeof(test_data) - 2))

array_i = 0;
printf ("n");

else
array_i = (array_i + 1);

return successbyte;


#define TTYO2 "/dev/ttyO2"
#define TTYO3 "/dev/ttyO3"

int main(void)
O_SYNC);
if (fd1 < 0)

printf ("Error %d opening %s: %s.n", errno, uart1, strerror(errno));
return -1;


// 2. Open COM2
// Open to read once data available (!NONBLOCK)
fd2 = open (uart2, O_RDWR









share|improve this question
























  • Your code is not a complete and minimal example of the problem. Your description of what does not work is inadequate. Five and six-bit frames have limited (modern) applications, and 7-bit is typically limited to ASCII transfers. BTW since your termios initialization is not POSIX-compliant, your code may not be reliable. See Setting Terminal Modes Properly

    – sawdust
    Mar 23 at 1:27












  • Hi Sawdust, thanks for your reply. I updated my code as above. Besides, termios initialization is not POSIX-compliant? If it's not, why 7 or 8bit character size could work? And way back in 5 and 6 bit frame have limited, could you please suggest me some changes in kernel to make it work fine? Thanks so much!

    – Jean
    Mar 25 at 7:25











  • There is no possible "change in the kernel" to fix your broken code and your misunderstanding. A UART frame size of n bits will only write the low-order n bits of each byte from your write buffer. Only n low-order bits will be read into each byte of the read buffer. ASCII characters and the value 100 require 7 bits. You cannot transmit 7 bits with a 6-data-bit frame! Instead of simply reporting that "Read wrong data" occurred, you should display the actual write and read data, and then analyze the difference.

    – sawdust
    Mar 25 at 8:04












  • Thanks Sawdust. what you mentioned is correct.

    – Jean
    Mar 26 at 16:21











  • Hi Sawdust, then in case I want to send ASCII character by 5 or 6 bit in frame, are there any linux configuration to let uart knows it should be send 2 frame continuous? Thank you. Sorry. I'm quite new in linux then my question looks dude sometime.

    – Jean
    Mar 29 at 4:49

















0















I am having problems with changing character size and parity bit in serial communication between 2 serial ports.



Currently, after opening serial ports, I want to change character size, stop bit and parity. If character size is 7bit, 8bit, writing and reading between 2 ports works fine. However, with character size of 5bit, 6bit and enable parity are not.



I'm thinking of tcsetattr() function which might set limit 7bit, 8bit in low layer linux kernel. I tried to debug around but I couldn't observe anything.



Could you guys give me any suggestion?



P/S: HW supports for whole setting.



Thanks so much!



My code as follows:



menu_serialparametersR()

printf ("Setting:n")
printf ("[Character size] [Stop bit] [Parity] [Flow control]n");
printf (" 1.5bit 5.1bit 7.Enable-Odd 10. HW n");
printf (" 2.6bit 6.2bit 8.Enable-Even 11. SW n");
printf (" 3.7bit 9.Disable n");
printf (" 4.8bit n");


setup_serial_port(int uart, unsigned char *uart_name)
CREAD);
tty.c_iflag

process_data(int fd1, int fd2, unsigned char *uart1_n, unsigned char *uart2_n)

int getatt_tst1, getatt_tst2, successdata;
struct termios tty1, tty2;

successdata = 0;

// Get attribute to print out parameters
getatt_tst1 = tcgetattr(fd1, &tty1);
getatt_tst2 = tcgetattr(fd2, &tty2);
if (getatt_tst1 < 0)

printf("Error [%d] from tcgetattr in %s: %sn", getatt_tst1, uart1_n, strerror(errno));
return;

if (getatt_tst2 < 0)

printf("Error [%d] from tcgetattr in %s: %sn", getatt_tst2, uart2_n, strerror(errno));
return;


/*** Writing random data to enable read function in fd2 ***/
// Fixed position. Don't move it after.

if ((wr_size = write (fd2, "a",1))<0)

printf ("Error [%d] while writing in %s: %sn",wr_size, uart2_n, strerror(errno));
return;


ntestdata = 100; // Just random number

successdata = write_read_byte(ntestdata, fd1, fd2, uart1_n, uart2_n);
if (successdata == ntestdata)

printf ("Finish Write/Read %d byte successful.n", successdata);

else
printf ("Failed. Only finish Write/Read %d byte successful.n", successdata);
return;


// Define random array for testing (ensure that length's above 8)
char test_data[]= "ABCDEFGHIJK12345678";

int write_read_byte(int num_testbytes, int fd1, int fd2,
unsigned char *uart1_name, unsigned char *uart2_name)

// VARIABLES
int wr_size, rd_size, array_i, rdcounter, wrcounter;
char *buf_ptr;
char buf_read[0x800];
int testbyte, successbyte;

// Initializations
wr_size = 0; // Write size
rd_size = 0; // Read size
array_i = 0; // Element index in testing array
rdcounter = 0; // Read counter
wrcounter = 0; // Write counter
successbyte = 0; // amount of byte write/read successful

memset (buf_read, 0 , 0x800);
testbyte = num_testbytes;

/**************** Start testing *************************/
while (testbyte > 0) // Do loop until num_testbytes

// Reset counter
rdcounter = 0;
wrcounter = 0;

// 1. Start writing 1 byte to fd1

wr_size = write (fd1, &test_data[array_i], 1);
printf ("Write: %c", test_data[array_i]);

if (wr_size < 0)

printf ("Error [%d] while writing in %s: %s. n", wr_size, uart2_name, strerror(errno));
return successbyte;


// 2. Start reading wr_size byte from fd2
while (wrcounter < wr_size) // Listening enough written byte

// Reset everything before read 1 byte
buf_ptr = 0;
rdcounter = 0;
rd_size = read(fd2, &buf_ptr, 1);
if (rd_size > 0)

for (rdcounter = 0; rdcounter < rd_size; rdcounter++)

printf (" - Read: %cn", buf_ptr); // using for testing
// Copy 1 read byte to read array
memcpy(&buf_read[(num_testbytes - testbyte)], &buf_ptr, 1);
// If reading right data
if (buf_read[(num_testbytes - testbyte)] == test_data[array_i])
successbyte = (successbyte + 1);
else // Wrong read byte

printf ("nTesting failed. Reason: Read wrong data in %s.nPlease check for configuration.n", uart2_name);
return successbyte;



else if (rd_size < 0)

printf ("nTesting failed. Reason: Error [%d] while reading in %s: %sn",
rd_size, uart2_name, strerror(errno));
printf ("Please check for:n1. Connection between 2 portsn2. Configurationn");
return successbyte;

else

printf ("nTesting failed. Reason: Timeout for reading in %s.n", uart2_name );
printf ("Please check for:n1. Connection between 2 portsn2. Configurationn");
return successbyte;

wrcounter = (wrcounter + 1) ;


testbyte = (testbyte-1);

// Limit test data element index
if (array_i >= (sizeof(test_data) - 2))

array_i = 0;
printf ("n");

else
array_i = (array_i + 1);

return successbyte;


#define TTYO2 "/dev/ttyO2"
#define TTYO3 "/dev/ttyO3"

int main(void)
O_SYNC);
if (fd1 < 0)

printf ("Error %d opening %s: %s.n", errno, uart1, strerror(errno));
return -1;


// 2. Open COM2
// Open to read once data available (!NONBLOCK)
fd2 = open (uart2, O_RDWR









share|improve this question
























  • Your code is not a complete and minimal example of the problem. Your description of what does not work is inadequate. Five and six-bit frames have limited (modern) applications, and 7-bit is typically limited to ASCII transfers. BTW since your termios initialization is not POSIX-compliant, your code may not be reliable. See Setting Terminal Modes Properly

    – sawdust
    Mar 23 at 1:27












  • Hi Sawdust, thanks for your reply. I updated my code as above. Besides, termios initialization is not POSIX-compliant? If it's not, why 7 or 8bit character size could work? And way back in 5 and 6 bit frame have limited, could you please suggest me some changes in kernel to make it work fine? Thanks so much!

    – Jean
    Mar 25 at 7:25











  • There is no possible "change in the kernel" to fix your broken code and your misunderstanding. A UART frame size of n bits will only write the low-order n bits of each byte from your write buffer. Only n low-order bits will be read into each byte of the read buffer. ASCII characters and the value 100 require 7 bits. You cannot transmit 7 bits with a 6-data-bit frame! Instead of simply reporting that "Read wrong data" occurred, you should display the actual write and read data, and then analyze the difference.

    – sawdust
    Mar 25 at 8:04












  • Thanks Sawdust. what you mentioned is correct.

    – Jean
    Mar 26 at 16:21











  • Hi Sawdust, then in case I want to send ASCII character by 5 or 6 bit in frame, are there any linux configuration to let uart knows it should be send 2 frame continuous? Thank you. Sorry. I'm quite new in linux then my question looks dude sometime.

    – Jean
    Mar 29 at 4:49













0












0








0








I am having problems with changing character size and parity bit in serial communication between 2 serial ports.



Currently, after opening serial ports, I want to change character size, stop bit and parity. If character size is 7bit, 8bit, writing and reading between 2 ports works fine. However, with character size of 5bit, 6bit and enable parity are not.



I'm thinking of tcsetattr() function which might set limit 7bit, 8bit in low layer linux kernel. I tried to debug around but I couldn't observe anything.



Could you guys give me any suggestion?



P/S: HW supports for whole setting.



Thanks so much!



My code as follows:



menu_serialparametersR()

printf ("Setting:n")
printf ("[Character size] [Stop bit] [Parity] [Flow control]n");
printf (" 1.5bit 5.1bit 7.Enable-Odd 10. HW n");
printf (" 2.6bit 6.2bit 8.Enable-Even 11. SW n");
printf (" 3.7bit 9.Disable n");
printf (" 4.8bit n");


setup_serial_port(int uart, unsigned char *uart_name)
CREAD);
tty.c_iflag

process_data(int fd1, int fd2, unsigned char *uart1_n, unsigned char *uart2_n)

int getatt_tst1, getatt_tst2, successdata;
struct termios tty1, tty2;

successdata = 0;

// Get attribute to print out parameters
getatt_tst1 = tcgetattr(fd1, &tty1);
getatt_tst2 = tcgetattr(fd2, &tty2);
if (getatt_tst1 < 0)

printf("Error [%d] from tcgetattr in %s: %sn", getatt_tst1, uart1_n, strerror(errno));
return;

if (getatt_tst2 < 0)

printf("Error [%d] from tcgetattr in %s: %sn", getatt_tst2, uart2_n, strerror(errno));
return;


/*** Writing random data to enable read function in fd2 ***/
// Fixed position. Don't move it after.

if ((wr_size = write (fd2, "a",1))<0)

printf ("Error [%d] while writing in %s: %sn",wr_size, uart2_n, strerror(errno));
return;


ntestdata = 100; // Just random number

successdata = write_read_byte(ntestdata, fd1, fd2, uart1_n, uart2_n);
if (successdata == ntestdata)

printf ("Finish Write/Read %d byte successful.n", successdata);

else
printf ("Failed. Only finish Write/Read %d byte successful.n", successdata);
return;


// Define random array for testing (ensure that length's above 8)
char test_data[]= "ABCDEFGHIJK12345678";

int write_read_byte(int num_testbytes, int fd1, int fd2,
unsigned char *uart1_name, unsigned char *uart2_name)

// VARIABLES
int wr_size, rd_size, array_i, rdcounter, wrcounter;
char *buf_ptr;
char buf_read[0x800];
int testbyte, successbyte;

// Initializations
wr_size = 0; // Write size
rd_size = 0; // Read size
array_i = 0; // Element index in testing array
rdcounter = 0; // Read counter
wrcounter = 0; // Write counter
successbyte = 0; // amount of byte write/read successful

memset (buf_read, 0 , 0x800);
testbyte = num_testbytes;

/**************** Start testing *************************/
while (testbyte > 0) // Do loop until num_testbytes

// Reset counter
rdcounter = 0;
wrcounter = 0;

// 1. Start writing 1 byte to fd1

wr_size = write (fd1, &test_data[array_i], 1);
printf ("Write: %c", test_data[array_i]);

if (wr_size < 0)

printf ("Error [%d] while writing in %s: %s. n", wr_size, uart2_name, strerror(errno));
return successbyte;


// 2. Start reading wr_size byte from fd2
while (wrcounter < wr_size) // Listening enough written byte

// Reset everything before read 1 byte
buf_ptr = 0;
rdcounter = 0;
rd_size = read(fd2, &buf_ptr, 1);
if (rd_size > 0)

for (rdcounter = 0; rdcounter < rd_size; rdcounter++)

printf (" - Read: %cn", buf_ptr); // using for testing
// Copy 1 read byte to read array
memcpy(&buf_read[(num_testbytes - testbyte)], &buf_ptr, 1);
// If reading right data
if (buf_read[(num_testbytes - testbyte)] == test_data[array_i])
successbyte = (successbyte + 1);
else // Wrong read byte

printf ("nTesting failed. Reason: Read wrong data in %s.nPlease check for configuration.n", uart2_name);
return successbyte;



else if (rd_size < 0)

printf ("nTesting failed. Reason: Error [%d] while reading in %s: %sn",
rd_size, uart2_name, strerror(errno));
printf ("Please check for:n1. Connection between 2 portsn2. Configurationn");
return successbyte;

else

printf ("nTesting failed. Reason: Timeout for reading in %s.n", uart2_name );
printf ("Please check for:n1. Connection between 2 portsn2. Configurationn");
return successbyte;

wrcounter = (wrcounter + 1) ;


testbyte = (testbyte-1);

// Limit test data element index
if (array_i >= (sizeof(test_data) - 2))

array_i = 0;
printf ("n");

else
array_i = (array_i + 1);

return successbyte;


#define TTYO2 "/dev/ttyO2"
#define TTYO3 "/dev/ttyO3"

int main(void)
O_SYNC);
if (fd1 < 0)

printf ("Error %d opening %s: %s.n", errno, uart1, strerror(errno));
return -1;


// 2. Open COM2
// Open to read once data available (!NONBLOCK)
fd2 = open (uart2, O_RDWR









share|improve this question
















I am having problems with changing character size and parity bit in serial communication between 2 serial ports.



Currently, after opening serial ports, I want to change character size, stop bit and parity. If character size is 7bit, 8bit, writing and reading between 2 ports works fine. However, with character size of 5bit, 6bit and enable parity are not.



I'm thinking of tcsetattr() function which might set limit 7bit, 8bit in low layer linux kernel. I tried to debug around but I couldn't observe anything.



Could you guys give me any suggestion?



P/S: HW supports for whole setting.



Thanks so much!



My code as follows:



menu_serialparametersR()

printf ("Setting:n")
printf ("[Character size] [Stop bit] [Parity] [Flow control]n");
printf (" 1.5bit 5.1bit 7.Enable-Odd 10. HW n");
printf (" 2.6bit 6.2bit 8.Enable-Even 11. SW n");
printf (" 3.7bit 9.Disable n");
printf (" 4.8bit n");


setup_serial_port(int uart, unsigned char *uart_name)
CREAD);
tty.c_iflag

process_data(int fd1, int fd2, unsigned char *uart1_n, unsigned char *uart2_n)

int getatt_tst1, getatt_tst2, successdata;
struct termios tty1, tty2;

successdata = 0;

// Get attribute to print out parameters
getatt_tst1 = tcgetattr(fd1, &tty1);
getatt_tst2 = tcgetattr(fd2, &tty2);
if (getatt_tst1 < 0)

printf("Error [%d] from tcgetattr in %s: %sn", getatt_tst1, uart1_n, strerror(errno));
return;

if (getatt_tst2 < 0)

printf("Error [%d] from tcgetattr in %s: %sn", getatt_tst2, uart2_n, strerror(errno));
return;


/*** Writing random data to enable read function in fd2 ***/
// Fixed position. Don't move it after.

if ((wr_size = write (fd2, "a",1))<0)

printf ("Error [%d] while writing in %s: %sn",wr_size, uart2_n, strerror(errno));
return;


ntestdata = 100; // Just random number

successdata = write_read_byte(ntestdata, fd1, fd2, uart1_n, uart2_n);
if (successdata == ntestdata)

printf ("Finish Write/Read %d byte successful.n", successdata);

else
printf ("Failed. Only finish Write/Read %d byte successful.n", successdata);
return;


// Define random array for testing (ensure that length's above 8)
char test_data[]= "ABCDEFGHIJK12345678";

int write_read_byte(int num_testbytes, int fd1, int fd2,
unsigned char *uart1_name, unsigned char *uart2_name)

// VARIABLES
int wr_size, rd_size, array_i, rdcounter, wrcounter;
char *buf_ptr;
char buf_read[0x800];
int testbyte, successbyte;

// Initializations
wr_size = 0; // Write size
rd_size = 0; // Read size
array_i = 0; // Element index in testing array
rdcounter = 0; // Read counter
wrcounter = 0; // Write counter
successbyte = 0; // amount of byte write/read successful

memset (buf_read, 0 , 0x800);
testbyte = num_testbytes;

/**************** Start testing *************************/
while (testbyte > 0) // Do loop until num_testbytes

// Reset counter
rdcounter = 0;
wrcounter = 0;

// 1. Start writing 1 byte to fd1

wr_size = write (fd1, &test_data[array_i], 1);
printf ("Write: %c", test_data[array_i]);

if (wr_size < 0)

printf ("Error [%d] while writing in %s: %s. n", wr_size, uart2_name, strerror(errno));
return successbyte;


// 2. Start reading wr_size byte from fd2
while (wrcounter < wr_size) // Listening enough written byte

// Reset everything before read 1 byte
buf_ptr = 0;
rdcounter = 0;
rd_size = read(fd2, &buf_ptr, 1);
if (rd_size > 0)

for (rdcounter = 0; rdcounter < rd_size; rdcounter++)

printf (" - Read: %cn", buf_ptr); // using for testing
// Copy 1 read byte to read array
memcpy(&buf_read[(num_testbytes - testbyte)], &buf_ptr, 1);
// If reading right data
if (buf_read[(num_testbytes - testbyte)] == test_data[array_i])
successbyte = (successbyte + 1);
else // Wrong read byte

printf ("nTesting failed. Reason: Read wrong data in %s.nPlease check for configuration.n", uart2_name);
return successbyte;



else if (rd_size < 0)

printf ("nTesting failed. Reason: Error [%d] while reading in %s: %sn",
rd_size, uart2_name, strerror(errno));
printf ("Please check for:n1. Connection between 2 portsn2. Configurationn");
return successbyte;

else

printf ("nTesting failed. Reason: Timeout for reading in %s.n", uart2_name );
printf ("Please check for:n1. Connection between 2 portsn2. Configurationn");
return successbyte;

wrcounter = (wrcounter + 1) ;


testbyte = (testbyte-1);

// Limit test data element index
if (array_i >= (sizeof(test_data) - 2))

array_i = 0;
printf ("n");

else
array_i = (array_i + 1);

return successbyte;


#define TTYO2 "/dev/ttyO2"
#define TTYO3 "/dev/ttyO3"

int main(void)
O_SYNC);
if (fd1 < 0)

printf ("Error %d opening %s: %s.n", errno, uart1, strerror(errno));
return -1;


// 2. Open COM2
// Open to read once data available (!NONBLOCK)
fd2 = open (uart2, O_RDWR






linux serial-port serial-communication termios






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 2 at 3:39







Jean

















asked Mar 22 at 3:49









JeanJean

11




11












  • Your code is not a complete and minimal example of the problem. Your description of what does not work is inadequate. Five and six-bit frames have limited (modern) applications, and 7-bit is typically limited to ASCII transfers. BTW since your termios initialization is not POSIX-compliant, your code may not be reliable. See Setting Terminal Modes Properly

    – sawdust
    Mar 23 at 1:27












  • Hi Sawdust, thanks for your reply. I updated my code as above. Besides, termios initialization is not POSIX-compliant? If it's not, why 7 or 8bit character size could work? And way back in 5 and 6 bit frame have limited, could you please suggest me some changes in kernel to make it work fine? Thanks so much!

    – Jean
    Mar 25 at 7:25











  • There is no possible "change in the kernel" to fix your broken code and your misunderstanding. A UART frame size of n bits will only write the low-order n bits of each byte from your write buffer. Only n low-order bits will be read into each byte of the read buffer. ASCII characters and the value 100 require 7 bits. You cannot transmit 7 bits with a 6-data-bit frame! Instead of simply reporting that "Read wrong data" occurred, you should display the actual write and read data, and then analyze the difference.

    – sawdust
    Mar 25 at 8:04












  • Thanks Sawdust. what you mentioned is correct.

    – Jean
    Mar 26 at 16:21











  • Hi Sawdust, then in case I want to send ASCII character by 5 or 6 bit in frame, are there any linux configuration to let uart knows it should be send 2 frame continuous? Thank you. Sorry. I'm quite new in linux then my question looks dude sometime.

    – Jean
    Mar 29 at 4:49

















  • Your code is not a complete and minimal example of the problem. Your description of what does not work is inadequate. Five and six-bit frames have limited (modern) applications, and 7-bit is typically limited to ASCII transfers. BTW since your termios initialization is not POSIX-compliant, your code may not be reliable. See Setting Terminal Modes Properly

    – sawdust
    Mar 23 at 1:27












  • Hi Sawdust, thanks for your reply. I updated my code as above. Besides, termios initialization is not POSIX-compliant? If it's not, why 7 or 8bit character size could work? And way back in 5 and 6 bit frame have limited, could you please suggest me some changes in kernel to make it work fine? Thanks so much!

    – Jean
    Mar 25 at 7:25











  • There is no possible "change in the kernel" to fix your broken code and your misunderstanding. A UART frame size of n bits will only write the low-order n bits of each byte from your write buffer. Only n low-order bits will be read into each byte of the read buffer. ASCII characters and the value 100 require 7 bits. You cannot transmit 7 bits with a 6-data-bit frame! Instead of simply reporting that "Read wrong data" occurred, you should display the actual write and read data, and then analyze the difference.

    – sawdust
    Mar 25 at 8:04












  • Thanks Sawdust. what you mentioned is correct.

    – Jean
    Mar 26 at 16:21











  • Hi Sawdust, then in case I want to send ASCII character by 5 or 6 bit in frame, are there any linux configuration to let uart knows it should be send 2 frame continuous? Thank you. Sorry. I'm quite new in linux then my question looks dude sometime.

    – Jean
    Mar 29 at 4:49
















Your code is not a complete and minimal example of the problem. Your description of what does not work is inadequate. Five and six-bit frames have limited (modern) applications, and 7-bit is typically limited to ASCII transfers. BTW since your termios initialization is not POSIX-compliant, your code may not be reliable. See Setting Terminal Modes Properly

– sawdust
Mar 23 at 1:27






Your code is not a complete and minimal example of the problem. Your description of what does not work is inadequate. Five and six-bit frames have limited (modern) applications, and 7-bit is typically limited to ASCII transfers. BTW since your termios initialization is not POSIX-compliant, your code may not be reliable. See Setting Terminal Modes Properly

– sawdust
Mar 23 at 1:27














Hi Sawdust, thanks for your reply. I updated my code as above. Besides, termios initialization is not POSIX-compliant? If it's not, why 7 or 8bit character size could work? And way back in 5 and 6 bit frame have limited, could you please suggest me some changes in kernel to make it work fine? Thanks so much!

– Jean
Mar 25 at 7:25





Hi Sawdust, thanks for your reply. I updated my code as above. Besides, termios initialization is not POSIX-compliant? If it's not, why 7 or 8bit character size could work? And way back in 5 and 6 bit frame have limited, could you please suggest me some changes in kernel to make it work fine? Thanks so much!

– Jean
Mar 25 at 7:25













There is no possible "change in the kernel" to fix your broken code and your misunderstanding. A UART frame size of n bits will only write the low-order n bits of each byte from your write buffer. Only n low-order bits will be read into each byte of the read buffer. ASCII characters and the value 100 require 7 bits. You cannot transmit 7 bits with a 6-data-bit frame! Instead of simply reporting that "Read wrong data" occurred, you should display the actual write and read data, and then analyze the difference.

– sawdust
Mar 25 at 8:04






There is no possible "change in the kernel" to fix your broken code and your misunderstanding. A UART frame size of n bits will only write the low-order n bits of each byte from your write buffer. Only n low-order bits will be read into each byte of the read buffer. ASCII characters and the value 100 require 7 bits. You cannot transmit 7 bits with a 6-data-bit frame! Instead of simply reporting that "Read wrong data" occurred, you should display the actual write and read data, and then analyze the difference.

– sawdust
Mar 25 at 8:04














Thanks Sawdust. what you mentioned is correct.

– Jean
Mar 26 at 16:21





Thanks Sawdust. what you mentioned is correct.

– Jean
Mar 26 at 16:21













Hi Sawdust, then in case I want to send ASCII character by 5 or 6 bit in frame, are there any linux configuration to let uart knows it should be send 2 frame continuous? Thank you. Sorry. I'm quite new in linux then my question looks dude sometime.

– Jean
Mar 29 at 4:49





Hi Sawdust, then in case I want to send ASCII character by 5 or 6 bit in frame, are there any linux configuration to let uart knows it should be send 2 frame continuous? Thank you. Sorry. I'm quite new in linux then my question looks dude sometime.

– Jean
Mar 29 at 4:49












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55292628%2fserial-communication-in-embedded-c-linux-tcsetattr%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55292628%2fserial-communication-in-embedded-c-linux-tcsetattr%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현