LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-19-2015, 09:09 AM   #1
ruc
LQ Newbie
 
Registered: Nov 2015
Posts: 3

Rep: Reputation: Disabled
Multithreaded program for UART


Hi,

I am new comer to linux programing.I need to run a multithreaded application which will read and write data to 7 UART ports.
for each UART port there will be 2 deidcated threads (i.e. read and write threads).I have a c++ code which creates thread to read and write data to any particualr UART port.But i want to run all the 7 ports parallel.Plese help me to know how i need to modify the below code so that it can create 7 write and read threads for 7 UART ports and could simultaneoulsy read/write from/to all the ports.
Thank u so much in advance.


void *Uartsend(void * threadParameter);
void *Uartread(void * threadParameter);


int m_fd = -1;
struct termios m_tio;
int trun = 1;
int rrun = 1;
pthread_t p_Uartsend, p_Uartread;

char test_str[] = "All questions asked by five watch experts amazed the judge.The quick brown fox jumps over the lazy dog.The five boxing wizards jump quickly.\r\n";
int main(int argc, char *argv[])
{

int i, ret;

pthread_t p_Uartsend, p_Uartread;
void *thread_res;

if (argc < 2)
{
printf("Insufficient arguments, exiting\n");
exit(EXIT_FAILURE);
}

// setup serial port
Serial_setupData_t port;
port.uart_dev_name[0] = '\0'; // No name for default
port.uart_dev_name[MAX_UART_NAME_LEN - 1] = '\0'; // Null terminate

port.baud_rate = DEFAULT_BAUD_RATE; /* baudrate */
//port.baud_rate = argv[2]; /* baudrate */

port.char_size = 8; /* 7 or 8 */
port.parity = 0; /* 0 = None, 1 = odd, 2= even */
port.stop_bits = 1; /* 1 or 2 */
port.flow_control = 0; /* 0 = none, 1 = RTS/CTS, 2 = XON/XOFF */

strncpy(port.uart_dev_name, argv[1], MAX_UART_NAME_LEN);

m_fd = SerialPort_Init(port, &m_tio);
//printf("Serial port fd received = %d\n", m_fd);

trun = 1;
rrun = 1;

ret = pthread_create(&p_Uartsend, NULL, Uartsend, NULL);
if (ret < 0)
{
printf("Error creating thread\n");
exit(EXIT_FAILURE);
}

ret = pthread_create(&p_Uartread, NULL, Uartread, NULL);
if (ret < 0)
{
printf("Error creating thread\n");
exit(EXIT_FAILURE);
}

//printf("test begin, press 'c' to exit\n");

trun = 0;
ret = pthread_join(p_Uartsend, &thread_res);
if (ret < 0)
{
printf("Failed to stop Uartsend thread\n");
}
else
{
//printf("Tx thread stopped\n");
}

ret = pthread_join(p_Uartread, &thread_res);
if (ret < 0)
{
printf("Failed to stop Uartread thread\n");
}
else
{
//printf("Rx thread stopped\n");
}


close(m_fd);
printf("\nExiting Test\n");

return 0;

}



int Serial_WriteData(int fd, char* buffer, int len)
{
int n = write(fd, buffer, len);
if (n < 0)
{
printf("ERROR writing to serial port\n");
}
else
{
// printf("Sent %d bytes ", n);
// DisplayString("", n, buffer);
}
return n;
}

int Serial_ReadData(int fd, int timeoutMs, char* buffer, int maxSize)
{
fd_set readSet;
struct timeval timeout;

FD_ZERO(&readSet); /* clear the set */

FD_SET(fd, &readSet); /* add our file descriptor to the set */

timeout.tv_sec = 0;
timeout.tv_usec = 1000 * timeoutMs;

int bytesRead = 0;
int rv = select(fd + 1, &readSet, NULL, NULL, &timeout);
if (rv == -1)
{
//perror("select"); /* an error occured */
printf("ERROR select in Serial_ReadData()\n");
return 0;
}
else if (rv == 0)
{
bytesRead = 0; // timeout without any bytes received
}
else
{
bytesRead = read(fd, buffer, maxSize);
if (bytesRead < 0)
{
printf("ERROR reading from serial port\n");
bytesRead = 0;
}
}
return bytesRead;
}

void *Uartsend(void * threadParameter)
{
double speed;
double time;

//char str[] = "Serial test message\r\n";
int strLen = sizeof(test_str);
int writtenBytes = 0;

do
{
printf("Port Name:");
for (int i=0; i<MAX_UART_NAME_LEN; i++)
{

printf("%c",uart_name_buff[i]);

}
printf("\n");
writtenBytes = Serial_WriteData(m_fd, test_str, strLen);

//printf("sent %d bytes with speed %f bps\n", strLen, speed);
//printf("Sent %d bytes ", writtenBytes);
if (writtenBytes < 0)
{
printf("Error Writing Data");
}
else
{
DisplayString("Sent", writtenBytes, str);

sleep(TX_SEND_DATA_PERIOD_SEC);
}
} while(1); // Run just once

return 0;
}

void *Uartread(void * threadParameter)
{
char buffer[MAX_STR_LEN];
int numbytesRead = 0;

while (rrun)
{
numbytesRead = Serial_ReadData(m_fd, READ_TIMEOUT_MS, buffer, MAX_STR_LEN);

if (numbytesRead > 0)
{
//printf("Read %d bytes", numbytesRead);
//printf("Read %d bytes: ", numbytesRead);
for (int k = 0; k < numbytesRead; k++)
{
printf("%c", buffer[k]);
if (buffer[k] == '\n')
{
//rrun = 0; // exit
break;
}
}
//DisplayString("", numbytesRead, buffer);
}
}
return 0;
}
 
Old 11-19-2015, 08:13 PM   #2
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
I doubt I'll be able to help with the program, but you should format it in a more readable way and also have your post moved to the programming forum.
 
Old 11-20-2015, 10:07 AM   #3
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
first comment I see is that line with "} while(1); // Run just once" will run forever.

The second is a question:

What are you trying to do?

Last edited by jpollard; 11-20-2015 at 10:08 AM.
 
Old 11-20-2015, 02:28 PM   #4
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Hi,

Please use [code][/code]] tags to enclose your code. There are instructions in the LQ FAQ as well as instructions in one of my signature links. Or just observe the advanced edit mode tools to see how to do that.

I'm not sure you need threads, I do a lot of multiple serial receive and transmit with either multiple processes or just one process and the needed file descriptors per serial interface.

How fast are the serial connections?

Is the data coming in continuously, or not?

What do you need to do with the data when received? Plot? Save? Retransmit? Decode and act upon?

When you next post code I recommend that you do the following:
  1. Post complete code sections which compile. The code you've posted has far too many errors, undefined values, unresolved definitions
  2. Separate the chunks. You have one section that is your main and then your intended receive and transmit functions. For me it's clearly more helpful to see the code, but also be informed that a particular section has been tested, debugged, and is not considered to be your problem.

You can consider viewing my blog entries on select(), and one on a USB serial receive parser. These illustrate how to do things like manage UART transmit and receive, decode of frames, and check multiple file descriptors for data.

Just saying you need to do the following sounds more like an assignment versus that you've envisioned an architecture like this for a particular reason.

You've posted code. Please explain where this code falls short and also what it does do at this point.
 
Old 12-01-2015, 06:56 AM   #5
ruc
LQ Newbie
 
Registered: Nov 2015
Posts: 3

Original Poster
Rep: Reputation: Disabled
Hi please find the attached files for more description of the code.

I have change the extension of the files to .txt as i could not attach files with .cpp and .h extensions.
- uart_cpp.txt (source code .cpp file)

- serialtest_h_file.txt (header file to be included in .cpp spurce file)

Capture1.jpg : expected output on serial port terminal
Capture2.jpg : output i am getting right now.


thanks
Attached Thumbnails
Click image for larger version

Name:	Capture1.JPG
Views:	50
Size:	113.6 KB
ID:	20191   Click image for larger version

Name:	Capture2.JPG
Views:	32
Size:	35.5 KB
ID:	20192  
Attached Files
File Type: txt serialtest_h_file.txt (3.1 KB, 144 views)
File Type: txt uart_cpp.txt (14.0 KB, 198 views)
 
Old 12-01-2015, 07:46 AM   #6
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
The text for "ERROR writing to serial port" is on line 415 in the code. write(2) will give you an errno value. Check that to determine what is wrong.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] multithreaded program,pthread ankit,garg Programming 6 01-19-2012 05:27 AM
Problem with a MultiThreaded program in C++? jeremy28 Programming 3 10-23-2010 08:05 PM
Multithreaded Program Ashok_mittal Linux - Newbie 2 01-04-2008 12:56 AM
Probelm regarding multithreaded program? geethu Programming 2 08-10-2005 12:39 AM
Debugging Multithreaded Program villie Programming 2 08-17-2004 11:24 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:54 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration