Usart and the Linux

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Greetings to the community,

i need your advice please,
i am on a project with stk1000 and ap7000, what i want to do is to send some hex strings to the serial port aand read some hex responses.

I am using this function for send characters:

int Serial::sendChar(char data) {
	switch (blocking) {
		// Non-blocking mode
		case 0:
			// If the port is actually open, send a byte
			if (fd != -1) {
				// Send the data and return number of bytes actually written
				write(fd, &data, 1);
				return 1;
			}
			else return -1;
			break;
		// Blocking mode
		case 1:
			// If the port is actually open, send a byte
			if (fd != -1) {
				// Send the data and return number of bytes actually written
				write(fd, &data, 1);
				return 1;
			}
			else return -1;
			break;
		// Blocking variable is messed up
		default:
			printf("SERIAL: Error with blocking setting!\n");
			return -1;
			break;
	}
}

and i am trying something simple like this..

char xdd[12] = {0x01,0x0A,0x00,0x13,0x20,0x03,0x20,0x01,0xFF,0x05,0x03,0x39};

int main()
{

for (i=0;i<12;i++)
{
port.sendChar(xdd[i]);
}

return 0;
}

rest of the code is ommited for simplicity.

Serial output from the code above is:
01 0D 0A 00 13 20 03 20 01 FF 05 03 39

As we know, Linux manage the serial ports as simply read write to a file, ttyS0 for example.

My problem is that when i am trying to send characters in HEX like 0x0A which is the ascii code of Line feed LF (\n) i get on the serial output a carriage return CR (\r).

This is a problem because i want to follow a protocol that is not based on character communication and these "extra" hex characters coming from nowhere are disastrous.

Is there any way i can change it??

thanks for your time!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

KostasTheGreat wrote:
Greetings to the community,

i need your advice please,
i am on a project with stk1000 and ap7000, what i want to do is to send some hex strings to the serial port aand read some hex responses.

I am using this function for send characters:

int Serial::sendChar(char data) {
	switch (blocking) {
		// Non-blocking mode
		case 0:
			// If the port is actually open, send a byte
			if (fd != -1) {
				// Send the data and return number of bytes actually written
				write(fd, &data, 1);
				return 1;
			}
			else return -1;
			break;
		// Blocking mode
		case 1:
			// If the port is actually open, send a byte
			if (fd != -1) {
				// Send the data and return number of bytes actually written
				write(fd, &data, 1);
				return 1;
			}
			else return -1;
			break;
		// Blocking variable is messed up
		default:
			printf("SERIAL: Error with blocking setting!\n");
			return -1;
			break;
	}
}

and i am trying something simple like this..

char xdd[12] = {0x01,0x0A,0x00,0x13,0x20,0x03,0x20,0x01,0xFF,0x05,0x03,0x39};

int main()
{

for (i=0;i<12;i++)
{
port.sendChar(xdd[i]);
}

return 0;
}

rest of the code is ommited for simplicity.

Serial output from the code above is:
01 0D 0A 00 13 20 03 20 01 FF 05 03 39

As we know, Linux manage the serial ports as simply read write to a file, ttyS0 for example.

My problem is that when i am trying to send characters in HEX like 0x0A which is the ascii code of Line feed LF (\n) i get on the serial output a carriage return CR (\r).

This is a problem because i want to follow a protocol that is not based on character communication and these "extra" hex characters coming from nowhere are disastrous.

Is there any way i can change it??

thanks for your time!

This is a known "feature" (really anoying when you are new to this). The ttys are set up using something called termios, which controls and manages all the settings of the tty, speed, flowcontrol and much more. See also man stty for more information.

One way to do this is:

static int open_modem_tty(const char *tty)
{
	struct termios ts;
	int ttySx;

	ttySx = open(tty, O_RDWR);// | O_NONBLOCK);
	if (ttySx == -1) {
		ne_printf("<%s> Faild to open %s\n", mod.name, tty);
		return -1;
	}
tcgetattr(ttySx, &ts);
	
	/* No echo and no CR/LF mangling */
	ts.c_lflag &= ~ECHO;
	ts.c_oflag &= ~ONLCR;
	ts.c_oflag &= ~OPOST;

	/* 9600 8n1 */
	speed_t termspeed = B9600;
	cfsetospeed(&ts, termspeed);
	cfsetispeed(&ts, termspeed);

	/* no flowcontrol */
	ts.c_cflag &= ~CRTSCTS;
	ts.c_iflag &= ~(IXON | IXOFF | IXANY);
	
	ts.c_cflag |= CS8; /* 8bits */
	ts.c_cflag &= ~CSTOPB; /* must be un-set, set means two stop bits */
	ts.c_cflag &= ~PARENB; /* no parity bit */

	/* store termios settings */
	tcsetattr(ttySx, TCSAFLUSH, &ts);

	write(ttySx, "I LIVE AGAIN\r", 13);
	
	return ttySx;
}

Use a function like this to initiate your tty, then your extra \r should be gone.

-rb

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thank you rogerbirkeland , i understood that linux handles serial ports by default as "terminal-ready".

so i changed the mode to NON-CANONICAL and the extra chars dissapeared :-)

have a nice day!