I'm totally newbie about this thing and I want to know where to start..
I have a manual that specifies data link layer that includes command and response frames to access a device connected in /dev/ttyUSB0.
Example of the given command frame is setting the baud rate
Head = 0x0A
Address = NULL/blank
Length = 0x03
Command = 0x20
Parameter = 0x00
Check = cc
Where parameter 0x00 is equal to baud rate 9600bps.
My question is how do I use this in programming? can I use it on C language?
My OS platform is ubuntu 12.04.
Any link or idea would be a great help.
UPDATE
This is the command I used in read()
unsigned char rx_buffer[1024];
size_t RX_buffer_len;
ssize_t bytes_read;
int fd;
RX_buffer_len = sizeof(rx_buffer);
bytes_read = read (serial, rx_buffer, RX_buffer_len);
You could start defining the your packet message structure
// Enable 1 byte alignment
#pragma pack(1)
typedef struct
{
uint8_t Head;
uint8_t Address;
uint8_t Length;
uint8_t Command;
uint8_t Parameter;
uint8_t Check;
}typ_packet;
// Restore the original alignment
#pragma pack()
Then you can access and configure ttyUSB0. A simple example:
struct termios2 t;
int serial, baud;
// Open the uart low level device driver and set up all params
serial_fd = open("/dev/ttyUSB0", O_NOCTTY | O_NDELAY);
if (serial != -1)
{
baud = 9600;
if (ioctl(serial, TCGETS2, &t))
{
// Fails to read tty pars
exit(1);
}
t.c_cflag &= ~CBAUD;
t.c_cflag |= BOTHER;
t.c_cflag |= CSTOPB;
t.c_ospeed = baud;
// Noncanonical mode, disable signals, extended
// input processing, and echoing
t.c_lflag &= ~(ICANON | ISIG | IEXTEN | ECHO);
// Disable special handling of CR, NL, and BREAK.
// No 8th-bit stripping or parity error handling.
// Disable START/STOP output flow control.
t.c_iflag &= ~(BRKINT | ICRNL | IGNBRK | IGNCR | INLCR |
INPCK | ISTRIP | IXON | PARMRK);
// Disable all output processing
t.c_oflag &= ~OPOST;
if (ioctl(serial, TCSETS2, &t))
{
// Failed to set serial parameters
exit(1);
}
}
else
{
// Failed to open ttyUSB0
exit(1);
}
Then you can simply read from serial line by
res = read (serial_fd, rx_buffer, RX_buffer_len);
And write using
typ_packet packet;
packet.Head = 0x0A;
packet.Address = 0x00;
packet.Length = 0x03;
packet.Command = 0x20;
packet.Parameter = 0x00;
packet.Check = 0xCC;
write(serial_fd, &packet, 6);
Related
I have a serial device connected via a FTDI serial to usb cable. The device sends 78 bytes every 10 msec. I am calling read() every 1 msec, so I expect read() to return 78 bytes every 10 calls (possibly split over 2 calls), and 0 bytes for 8-9 calls in between (my read() call provides a 2k byte buffer). However, what actually happens is read() returns 496 bytes every ~63 msec. I get all the data, but not as soon as it's available. It seems to be buffered somewhere until 496 bytes are accumulated.
Here's my code snippet that sets termios. Everything should be set for raw data. I believe that somehow, the buffering is happening in the linux USB driver, since VMIN and VTIME are both set to 0. Any ideas on how to get the data as soon as it's available? Possibly by tweaking the USB driver settings?
char *portname = "/dev/ttyUSB0";
fd = open(portname, O_RDWR | O_NOCTTY | O_NDELAY);
if (fd < 0)
{
printf("Failed to open %s. errno = %d (%s)\n", portname, errno, strerror(errno));
return -1;
}
printf("%s opened successfully\n\n", portname);
struct termios settings;
if (tcgetattr(fd, &settings) < 0)
{
printf("tcgettattr() failed (%i): %s\n", errno, strerror(errno));
return -1;
}
cfsetispeed(&settings, B460800); /* set baud rate */
cfsetospeed(&settings, B460800);
settings.c_cflag &= ~PARENB; /* No parity */
settings.c_cflag &= ~CSTOPB; /* 1 stop bit */
settings.c_cflag &= ~CSIZE; /* Clears the mask for setting the data size */
settings.c_cflag |= CS8; /* Set the data bits = 8 */
settings.c_cflag &= ~CRTSCTS; /* No Hardware flow Control */
settings.c_cflag |= CREAD | CLOCAL; /* Enable receiver, Ignore Modem Control lines */
settings.c_iflag &= ~(IXON | IXOFF | IXANY); /* Disable XON/XOFF flow control both i/p and o/p */
settings.c_iflag &= ~(BRKINT | IGNBRK | ICRNL | INLCR | PARMRK | ISTRIP | IGNCR);
settings.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHONL | ISIG | IEXTEN); /* Non Cannonical mode */
settings.c_lflag |= NOFLSH;
settings.c_oflag &= ~(OPOST | ONLCR); /* No Output Processing */
settings.c_cc[VMIN] = 0;
settings.c_cc[VTIME] = 0;
if ((tcsetattr(fd, TCSANOW, &settings)) != 0) /* Set the attributes to the termios structure */
{
printf("Error setting attributes\n");
return -1;
}
I am working on project which requires to send command in hex with device setting as follows :
Baud rate : 9600, Parity : None, Stop bits : 2 , data bits : 8 (9600, 8N2).
I have verified by sending data to device with minicom. I am able to see data on CRO. But when I am sending data via C code and unable to see data on CRO and getting program output as FFFFF always for read function.
Source code for serial read and write :
int32_t Read()
{
printf("Entering Read function \n");
int fd;
fd = open("/dev/ttyS1", O_RDWR | O_NOCTTY | O_NDELAY);
int j=0;
if (fd == -1)
{
perror("open_port: Unable to open /dev/ttyS1\n");
exit(1);
}
else
{
printf("Port /dev/ttyS1 opened successfully\n");
}
//---------- Setting the Attributes of the serial port using termios structure ---------
struct termios SerialPortSettings; // Create the structure
tcgetattr(fd, &SerialPortSettings); // Get the current attributes of the Serial port
cfsetispeed(&SerialPortSettings,B9600); // Set Read Speed as 9600
cfsetospeed(&SerialPortSettings,B9600); // Set Write Speed as 9600
SerialPortSettings.c_cflag &= ~PARENB; // Disables the Parity Enable bit(PARENB),So No Parity
SerialPortSettings.c_cflag &= ~PARODD; // added
SerialPortSettings.c_cflag |= CS8; // Set the data bits = 8
SerialPortSettings.c_cflag &= CSTOPB; // CSTOPB = 2 Stop bits
SerialPortSettings.c_cflag &= ~CSIZE; // Clears the mask for setting the data size
SerialPortSettings.c_cflag &= ~CRTSCTS; // No Hardware flow Control
SerialPortSettings.c_cflag |= (CREAD | CLOCAL); // Enable receiver,Ignore Modem Control lines
SerialPortSettings.c_lflag =0; /* RAW input */ // added
SerialPortSettings.c_iflag &= ~(IXON | IXOFF | IXANY); // Disable XON/XOFF flow control both i/p and o/p
SerialPortSettings.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG); // Non Cannonical mode
SerialPortSettings.c_cc[VMIN] = 0; // added for testing // added 1 sec
SerialPortSettings.c_cc[VTIME] = 5; // added for testing // added
SerialPortSettings.c_iflag = 0; /* SW flow control off, no parity checks etc */ // added
SerialPortSettings.c_oflag &= ~OPOST;// No Output Processing
if((tcsetattr(fd,TCSANOW,&SerialPortSettings)) != 0) // Set the attributes to the termios structure
printf("\n ERROR ! in Setting attributes");
else
printf("\n BaudRate = 9600 \n StopBits = 2 \n Parity = None\n");
//------------------------------- Write data to serial port -----------------------------
unsigned char write_buffer[] = {0x28,0x11,0xDF,0xBC}; // Buffer containing characters to write into port
int bytes_written = 0; // Value for storing the number of bytes written to the port
for(j=0;j<4;j++)
{
bytes_written = write(fd,write_buffer[j],sizeof(write_buffer));// use write() to send data to port
// "fd" - file descriptor pointing to the opened serial port
// "write_buffer" - address of the buffer containing data
// "sizeof(write_buffer)" - No of bytes to write
//
printf("Byte written : %x\n",write_buffer[j]);
printf("\n %d Bytes written to ttyS1", bytes_written);
printf("\n +----------------------------------+\n\n");
}
sleep(2);
//------------------------------- Read data from serial port -----------------------------
tcflush(fd, TCIFLUSH); // Discards old data in the rx buffer
unsigned char read_buffer[40]; // Buffer to store the data received
int bytes_read = 0; // Number of bytes read by the read() system call
int i = 0;
bytes_read = read(fd,&read_buffer,40); // Read the data
printf("\n\n Bytes Rxed -%x", bytes_read); // Print the number of bytes read
printf("\n\n ");
for(i=0;i<bytes_read;i++) //printing only the received characters
printf("%x",(int)(*(unsigned char*)(&read_buffer[i])));
printf("\n +----------------------------------+\n\n\n");
close(fd); // Close the serial port
printf("Exiting Read function \n");
}
Output :
Entering Read function
Port /dev/ttyS1 opened successfully
BaudRate = 9600
StopBits = 2
Parity = None
Byte written : 28
-1 Bytes written to ttyS1
+----------------------------------+
Byte written : 11
-1 Bytes written to ttyS1
+----------------------------------+
Byte written : df
-1 Bytes written to ttyS1
+----------------------------------+
Byte written : bc
-1 Bytes written to ttyS1
+----------------------------------+
Bytes Rxed -ffffffff
+----------------------------------+
Exiting Read function
1st of all add error checking to this call:
tcgetattr(fd, &SerialPortSettings);
like
if (0 > tcgetattr(fd, &SerialPortSettings))
{
/* log and handle error */
}
Then here
SerialPortSettings.c_cflag |= CS8;
you set the data bits.
Two lines below you clear it
SerialPortSettings.c_cflag &= ~CSIZE;
Do this the other way round.
Also this
SerialPortSettings.c_cflag &= CSTOPB;
unsets.
If you want to use 2 stop bits set it by doing
SerialPortSettings.c_cflag |= CSTOPB;
Instead of just doing
SerialPortSettings.c_oflag &= ~OPOST;
do
SerialPortSettings.c_oflag = 0;
The code calls write() wrongly. This
for(j=0;j<4;j++)
{
bytes_written = write(fd,write_buffer[j],sizeof(write_buffer));
ought to be
for(j=0;j<4;j++)
{
bytes_written = write(fd, &write_buffer[j], 1)
Or just drop the loop and do
bytes_written = write(fd, write_buffer, sizeof write_buffer);
The call to read() is wrong as well.
It should be
bytes_read = read(fd, read_buffer, ...
For some of those mistakes with write and read the compiler should have warned you about.
Please also note that read() and write() return ssize_t not int.
Last not least I doubt flushing the input buffer (tcflush(fd, TCIFLUSH);) before reading it makes sense.
I have to read a data from Serial port using C program. I am sending Hex array data and the device will response the corresponding response for the request.
I have tried with the GTK+ Serial port Terminal, For Example, if i write data "FC 05 40 2B 15" the device will resturn the response as "FC 05 50 AA 05".
Someone please guide me to get this, i have been trying for so long.
I have attached my code here.
void main()
{
int fd;
struct termios SerialPortSettings;
char write_buffer[512];
int dispense_card[5] = { 0XFC,0X05,0x40,0x2B,0x15 };
//char dispense[7] = {0x02,0x31,0x35,0x44,0x43,0x03,0x02};
int bytes_read = 0;
FILE* lfp;
time_t now;
time(&now);
//lfp = fopen("carddispense.log","a+");
fd = open("/dev/ttyUSB1",O_RDWR | O_NOCTTY);
if(fd == -1)
{
//fprintf(lfp,"\nError! in Opening ttyUSB0 %s", ctime(&now));
printf("\nError in Opening in ttyUSB0\n");
exit(1);
}
else
{ printf("\nttyUSB0 Opened Successfully\n");
//fprintf(lfp,"Card reader has been used %s", ctime(&now));
tcgetattr(fd, &SerialPortSettings); /* save current serial port settings */
cfsetispeed(&SerialPortSettings,B9600); /* Baud rate for read */
cfsetospeed(&SerialPortSettings,B9600);
SerialPortSettings.c_cflag &= PARENB; // No Parity
SerialPortSettings.c_cflag &= ~CSTOPB; //Stop bits = 1
SerialPortSettings.c_cflag &= ~CSIZE; /* Clears the Mask */
SerialPortSettings.c_cflag |= CS8; /* Set the data bits = 8 */
SerialPortSettings.c_cflag &= ~CRTSCTS; /*Turn off hardware based flow control */
SerialPortSettings.c_cflag |= CREAD | CLOCAL; /* Turn on the receiver of the serial port (CREAD) */
SerialPortSettings.c_iflag &= ~(IXON | IXOFF | IXANY); /*Turn off hardware based flow control */
SerialPortSettings.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); /* NON Cannonical mode for serial communication */
SerialPortSettings.c_cc[VMIN] = 100; /* Read 128 characters */
SerialPortSettings.c_cc[VTIME] = 0; /* Wait indefinitely */
tcsetattr(fd,TCSANOW,&SerialPortSettings);
int pos =0,percent_count = 0;
int i;
for(i=0;i<5;i++)
{
printf("number = %x\t",dispense_card[i]);
sprintf(write_buffer+(i),"%c", dispense_card[i]);
}
printf("write_buffer%s\n",write_buffer);
//printf("write_buffer length: %d\n",strlen(write_buffer));
write(fd,write_buffer,strlen(write_buffer));
close(fd);
}
}
Try this
char tx_buf[5];
char rx_buf[5];
sprintf(tx_buf, "%c%c%c%c%c",0XFC,0X05,0x40,0x2B,0x15);
write(fd, tx_buf, 5);
while (1) {
int n = read (fd, rx_buf, 5);
if (n > 0) {
for(int i=0; i<n; i++) {
printf("data i: %02X ", rx_buf[i]); // "FC 05 50 AA 05"
}
}
}
close(fd);
I have a problem with my uart read (on raspberry pi). It works ok but it stops in a loop and waits on data...
I made an option O_NDELAY but is stops even so.
I use two terminal windows:
one is used for uart program
on the second i write:
echo '123445' > /dev/ttyAMA0
(raspberry Pi uart port)
Complete code is below.
#include <stdio.h>
#include <unistd.h> //Used for UART
#include <fcntl.h> //Used for UART
#include <termios.h> //Used for UART
int main(int argv, char * argc[])
{
// Setting Up The UART
//-------------------------
//----- SETUP USART 0 -----
//-------------------------
//At bootup, pins 8 and 10 are already set to UART0_TXD, UART0_RXD (ie the alt0 function) respectively
int uart0_filestream = -1;
//OPEN THE UART
//The flags (defined in fcntl.h):
// Access modes (use 1 of these):
// O_RDONLY - Open for reading only.
// O_RDWR - Open for reading and writing.
// O_WRONLY - Open for writing only.
//
// O_NDELAY / O_NONBLOCK (same function) - Enables nonblocking mode. When set read requests on the file can return immediately with a failure status
// if there is no input immediately available (instead of blocking). Likewise, write requests can also return
// immediately with a failure status if the output can't be written immediately.
//
// O_NOCTTY - When set and path identifies a terminal device, open() shall not cause the terminal device to become the controlling terminal for the process.
uart0_filestream = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY); //Open in non blocking read/write mode
if (uart0_filestream == -1)
{
//ERROR - CAN'T OPEN SERIAL PORT
printf("Error - Unable to open UART. Ensure it is not in use by another application\n");
}
//CONFIGURE THE UART
//The flags (defined in termios.h - see http://pubs.opengroup.org/onlinepubs/007908799/xsh/termios.h.html):
// Baud rate:- B1200, B2400, B4800, B9600, B19200, B38400, B57600, B115200, B230400, B460800, B500000, B576000, B921600, B1000000, B1152000, B1500000, B2000000, B2500000, B3000000, B3500000, B4000000
// CSIZE:- CS5, CS6, CS7, CS8
// CLOCAL - Ignore modem status lines
// CREAD - Enable receiver
// IGNPAR = Ignore characters with parity errors
// ICRNL - Map CR to NL on input
// PARENB - Parity enable
// PARODD - Odd parity (else even)
struct termios cfg;
//get existing configuration setup
tcgetattr(uart0_filestream, &cfg);
//fcntl(deviceFD, F_SETFL, FNDELAY);
fcntl(uart0_filestream, F_SETFL, 0);
////set both incoming and outgoing baud rates...
cfsetispeed(&cfg, B115200);
cfsetospeed(&cfg, B115200);
cfg.c_cflag |= (CLOCAL | CREAD);
////8N1 (8 data bits, No parity, 1 stop bit)
cfg.c_cflag &= ~PARENB;
cfg.c_cflag &= ~CSTOPB;
cfg.c_cflag &= ~CSIZE;
cfg.c_cflag |= CS8;
cfg.c_cflag &= ~CRTSCTS; //~CNEW_RTSCTS; //disable hardware flow control
//use RAW unbuffered data mode (eg, not canonical mode)
cfg.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG | IGNBRK);
cfg.c_iflag &= ~(IGNPAR | IXON | IXOFF | IXANY);
//raw (unprocessed) output mode
cfg.c_oflag &= ~OPOST;
tcsetattr(uart0_filestream, TCSANOW, &cfg);
//Transmitting Bytes
//----- TX BYTES -----
unsigned char tx_buffer[20];
unsigned char *p_tx_buffer;
p_tx_buffer = &tx_buffer[0];
*p_tx_buffer++ = 'H';
*p_tx_buffer++ = 'e';
*p_tx_buffer++ = 'l';
*p_tx_buffer++ = 'l';
*p_tx_buffer++ = 'o';
if (uart0_filestream != -1)
{
int count = write(uart0_filestream, &tx_buffer[0], (p_tx_buffer - &tx_buffer[0])); //Filestream, bytes to write, number of bytes to write
if (count < 0)
{
printf("UART TX error\n");
}
}
//Receiving Bytes
//----- CHECK FOR ANY RX BYTES -----
while(1) {
printf("loop\n");
if (uart0_filestream != -1)
{
// Read up to 255 characters from the port if they are there
unsigned char rx_buffer[256];
int rx_length = read(uart0_filestream, (void*)rx_buffer, 255); //Filestream, buffer to store in, number of bytes to read (max)
if (rx_length < 0)
{
//An error occured
printf("UART RX error\n");
}
else if (rx_length == 0)
{
//No data waiting
printf("no data UART RX test commit\n");
}
else
{
//Bytes received
rx_buffer[rx_length] = '\0';
printf("%i bytes read : %s\n", rx_length, rx_buffer);
//break;
}
}
sleep(1);
}
//Closing the UART if no longer needed
//----- CLOSE THE UART -----
close(uart0_filestream);
return 0;
}
I used to have the same problem, and problem is not related with code and solution is just disable login on serial port,
there is a file at /etc/inittab
open this file with nano and find this line:
T0:23:respawn:/sbin/getty -L ttyAMA0 115200 vt100
and put a # char at begining this line than save and reboot and it is working weirdly.
You can use O_NONBLOCK flag to make the read/write operation non blocking.
In my program I read from the serial device (Linux, 8N1) without any problem. But in the case I want to write out a single byte, I get nothing out on the interface. I assume that my serial output settings are wrong. But there aren't that many ways how to set c_oflag...
My code:
#define TTYDEVICE "/dev/ttyS0"
#define BAUDRATE B9600
int openSerialDevice(const char* devTTY, struct termios oldTio) {
//----< Open serial device >----------------------------------
int fileDescriptor;
// fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
fileDescriptor = open(devTTY, O_RDWR | O_NOCTTY);
//fileDescriptor = open(devTTY, O_RDWR | O_NOCTTY /*| OPOST*/);
if (fileDescriptor == -1) {
perror("Error while opening serial interface occurred!");
return -99;
}
// set new parameters to the serial device
struct termios newtio;
bzero(&newtio, sizeof(newtio));
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
// set to 8N1
newtio.c_cflag &= ~PARENB;
newtio.c_cflag &= ~CSTOPB;
newtio.c_cflag &= ~CSIZE;
newtio.c_cflag |= CS8;
newtio.c_iflag = IGNPAR;
// output mode to
//newtio.c_oflag = 0;
newtio.c_oflag |= OPOST;
/* set input mode (non-canonical, no echo,...) */
newtio.c_lflag = 0;
newtio.c_cc[VTIME] = 10; /* inter-character timer 1 sec */
newtio.c_cc[VMIN] = 0; /* blocking read disabled */
tcflush(fileDescriptor, TCIFLUSH);
if (tcsetattr(fileDescriptor, TCSANOW, &newtio)) {
perror("could not set the serial settings!");
return -99;
}
//----< / Open serial device >----------------------------------
return fileDescriptor;
}
int ACK[1] = { 6 };
int main() {
// old termios to restablish
struct termios oldTIO;
// filedescriptor
int fd;
fd = openSerialDevice(TTYDEVICE, oldTIO);
if ((fd == -1) | (fd == -99)) {
perror("Could not open TTY-Device. Exit on failure!");
return EXIT_FAILURE;
}
write(fd, ACK, 1); // Problem !!
return 0:
}
Now, if I use
screen /dev/ttyS1 9600 8n1
to verify what's coming out on /dev/ttyS0. I can't see anything. Same if I sniff with Docklight 1.8.
Any suggestions? thanks
How do you verify nothing is coming out ?
You can try to drop the RTSCTS, and try again. Infact, if you want minimal interference from the tty layer, you should set your terminal to raw, using this :
cfmakeraw(&newtio);
You're giving write() the data argument of ACK, which is a pointer to int. This is probably not what you mean. Depending on the endianness of the computer you're on, this means write() will "see" a buffer containing the chars { 6, 0, 0, 0 } (little-endian) or { 0, 0, 0, 6 } (big-endian). This assumes that sizeof (int) == 4 is true, adjust for other sizes as needed, the problem remains.
You should very probably make the buffer unsigned char instead. Also, if you had made the call like this:
int wrote = write(fd, ACK, sizeof ACK);
printf("Wrote %d bytes\n", wrote);
You would have gotten direct feedback. You should test something like this, to see that the write actually succeeds.
The activated Hardware-Flow-Control (CRTSCTS) was the reason why write() blocked and finally nothing has appeared on the serial output.
thanks!
Code snap which works:
int openSerialDevice(const char* devTTY, struct termios oldTio) {
//----< Open serial device >----------------------------------
int fileDescriptor;
// fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
fileDescriptor = open(devTTY, O_RDWR | O_NOCTTY);
//fileDescriptor = open(devTTY, O_RDWR | O_NOCTTY /*| OPOST*/);
if (fileDescriptor == -1) {
perror("Error while opening serial interface occurred!");
return -99;
}
// set new parameters to the serial device
struct termios newtio;
fcntl(fileDescriptor, F_SETFL, 0);
// set everything to 0
bzero(&newtio, sizeof(newtio));
// again set everything to 0
bzero(&newtio, sizeof(newtio));
newtio.c_cflag |= BAUDRATE; // Set Baudrate first time
newtio.c_cflag |= CLOCAL; // Local line - do not change "owner" of port
newtio.c_cflag |= CREAD; // Enable receiver
newtio.c_cflag &= ~ECHO; // Disable echoing of input characters
newtio.c_cflag &= ~ECHOE;
// set to 8N1
newtio.c_cflag &= ~PARENB; // no parentybyte
newtio.c_cflag &= ~CSTOPB; // 1 stop bit
newtio.c_cflag &= ~CSIZE; // Mask the character size bits
newtio.c_cflag |= CS8; // 8 data bits
// output mode to
newtio.c_oflag = 0;
//newtio.c_oflag |= OPOST;
// Set teh baudrate for sure
cfsetispeed(&newtio, BAUDRATE);
cfsetospeed(&newtio, BAUDRATE);
newtio.c_cc[VTIME] = 10; /* inter-character timer */
newtio.c_cc[VMIN] = 0; /* blocking read until */
tcflush(fileDescriptor, TCIFLUSH); // flush pending data
// set the new defined settings
if (tcsetattr(fileDescriptor, TCSANOW, &newtio)) {
perror("could not set the serial settings!");
return -99;
}
//----< / Open serial device >----------------------------------
return fileDescriptor;
}