LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-14-2010, 06:37 AM   #1
knobby67
Member
 
Registered: Mar 2006
Posts: 627

Rep: Reputation: 43
serial port problem in C


hi All,
I've written some code which reads in the serial port, it all appeared to work well except I've reached a problem when I send 0xff my code ignores this as the read function sees -1 as a nothing on the port. My code is as follows

unsigned char RxChar(COMPORT *port)
{
unsigned char readletter[1];
pthread_mutex_lock( &rxmutex );

if(read(port->id, readletter, 1)==-1) /* comms port has nothing*/
{
pthread_mutex_unlock( &rxmutex);
return(0xff);
}
else
{
pthread_mutex_unlock( &rxmutex);
return(readletter[0]);

}

pthread_mutex_unlock( &rxmutex);
return(-1);
}


Is there a way I can check to see if there is anything in the serial buffer without pulling it out using read?

Normally I would call the fucntion with something like

rx = RxChar(&Comms_1)'
if( rx ! = 0xff)
{
....
}

how can I make it something like
if(RxBufferEmpry(&Coms_1)
{

.....

}
So basically test to see if the comms port buffer is empty without pulling the character is in it? thanks
 
Old 07-14-2010, 06:54 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
I would suggest that you do not read the comm port unless you know for a fact that there is data to be read. You can verify that data is available using select().

As for your function RxChar(), it has a declaration/definition issue. You have indicated that it will return an unsigned char, yet in one place you return a -1. This is equivalent to 255 decimal, or 0xFF. If 0xFF is considered a valid return value, then you are going to have to redefine the function to return a wider set of data.

I would suggest that you define RxChar() to return an int value, which if needed, can be cast to an unsigned char when you have determined that you have valid data. For example:
Code:
#include <stdio.h>

int function()
{
   int rtn = -1;

   if (1)
   {
      rtn = 0xFF;
   }

   return rtn;
}

int main()
{
   int val = function();

   if (val == -1) {
      printf("error, val = %d\n", val);
   }
   else {
      unsigned char data = val;
      printf("success, data = %x\n", data);
   }

   return 0;
}
Btw, you should also refactor RxChar(). Consider the following:
Code:
int RxChar(COMPORT *port)
{
   int           rtn = -1;
   unsigned char readletter;

   pthread_mutex_lock(&rxmutex);

   if(read(port->id, &readletter, sizeof(readletter)) > 0)
   {
      rtn = readletter;
   }

   pthread_mutex_unlock(&rxmutex);

   return rtn;
}
And of course, always post code within CODE tags.

Last edited by dwhitney67; 07-14-2010 at 06:57 AM.
 
Old 07-15-2010, 02:49 AM   #3
knobby67
Member
 
Registered: Mar 2006
Posts: 627

Original Poster
Rep: Reputation: 43
thanks, I was hoping not to use an int, i thought like in an embedded system there would be some way to see if anything was in the UHART rather the just pulling it out to see. I'll look into select()
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
serial port problem! kievari Programming 3 02-08-2010 05:27 PM
serial port problem nasr-noor Programming 3 11-30-2008 10:30 PM
serial port problem bekaar Ubuntu 2 03-24-2007 06:44 PM
Serial port problem! instcode Ubuntu 3 01-05-2007 11:19 PM
Problem with serial port billyseth Linux - Kernel 1 10-27-2006 01:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:12 PM.

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