LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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 01-15-2006, 09:38 PM   #1
lucky6969b
Member
 
Registered: Nov 2005
Posts: 337

Rep: Reputation: 30
flushing memory buffers


Hi
If I
read (fileHandle, buff,...);
how do I flush the fileHandle and buff?
I tried both
flush(fileHandle);
flush(buff);
Thanks
Jack
 
Old 01-15-2006, 10:25 PM   #2
foo_bar_foo
Senior Member
 
Registered: Jun 2004
Posts: 2,553

Rep: Reputation: 51
you just read till you come up short
you know
bytes_read != sizeof(buffer)
all done
 
Old 01-16-2006, 03:17 AM   #3
lucky6969b
Member
 
Registered: Nov 2005
Posts: 337

Original Poster
Rep: Reputation: 30
///// RS232.cpp

//
// C++ Implementation: thread
//
// Description:
//
//
// Author: root <root@dhcppc2>, (C) 2005
//
// Copyright: See COPYING file that comes with this distribution
//
//

#include <termios.h>
//#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/signal.h>
#include <sys/types.h>
#include "rmves.h"
#include "baseobject.h"
#include "RS232.h"


#include "Packet.h"

#define LOBYTE(x) ((x) & 0xF)
#define HIBYTE(x) ((x >> 4) & 0xF)

void signal_handler_IO(int a);

volatile int STOP=FALSE;
int wait_flag = TRUE;




void *cbfunc(void *);




// PortMonitorThread Thunk
void *cbfunc(void *a)
{

rs232->PortMonitorThread(a);

}

//*********************************************
// CreateThread: Create Thread Object

bool RS232_Controller::CreateThread(void)
{
int res;

// m_ClientExec = (ThreadExeFunc) //&RS232_Controller::PortMonitorThread;
// if requesting the 5th thread, rejects it
if (m_iNumThreadsRunning >= MAX_THREADS)
return FALSE;
pthread_create (&m_Threads[m_iNumThreadsRunning], NULL, cbfunc, NULL);
if (res == 0)
return FALSE;

m_iNumThreadsRunning++; // Increment the count
printf ("%d Thread%c %s running\n", m_iNumThreadsRunning, (m_iNumThreadsRunning <= 1) ? ' ' : 's', (m_iNumThreadsRunning <= 1) ? "is" : "are");


;

return TRUE;
}


//**************************************************
// Join the Thread Pool
void RS232_Controller::Join (pthread_t pthread)
{
pthread_join (pthread, NULL);

}


/////////////////////////// Above is correct, stay away from it




//**************************************************
// PortMonitorThread : Monitoring each thread
// One thread monitors one device
// one is RS232 Port, the other is the Camera

void* RS232_Controller::PortMonitorThread(void* a)
{
struct termios oldtio, newtio;
struct sigaction saio;
unsigned char *buff;
PacketEx *packet;
int res;
static bool bhdrchk;
int iPacketSize = 18;

packet = new PacketEx(iPacketSize);
buff = (unsigned char *) malloc(65535);
fileHandle = open (LINUX_SERIAL_DEVICE, O_RDWR | O_NOCTTY); // | O_NONBLOCK);

// Signal Handler, equals PortMonitorThread

saio.sa_handler = signal_handler_IO;
// saio.sa_mask = 0;
saio.sa_flags = 0;
saio.sa_restorer = NULL;
sigaction ( SIGIO, &saio, NULL );

fcntl ( fileHandle, F_SETOWN, getpid());

// Set to the file for nonblocking read
fcntl ( fileHandle, F_SETFL, FASYNC); // | O_NONBLOCK);

// clear attributes
bzero (&newtio, sizeof (newtio));

// Save old attributes
tcgetattr (fileHandle, &oldtio);

//Control Flag
newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
// newtio.c_cflag &= ~(PARENB | PARODD);
// newtio.c_cflag &= ~CSIZE;
// newtio.c_cflag &= ~CSTOPB;


// Input Flag
newtio.c_lflag = IGNPAR | ICRNL;

// Output Flag
newtio.c_oflag = 0;
newtio.c_lflag = ISIG;
newtio.c_cc[VMIN] = 1;
newtio.c_cc[VTIME] = 0;
tcflush ( fileHandle, TCIFLUSH );

tcsetattr (fileHandle, TCSANOW, &newtio);


// Monitor Sensors signals
while (STOP == false) {
// how do you gurantee buff is cleared until next use
res = read (fileHandle, buff, iPacketSize);
buff[res] = 0;

if (strlen ((char *)buff) == 0)
continue;

packet->SetBuff(buff);
if (res) {
printf ("Some Message has arrived\n");
// printf ("%s\n", packet->GetBuff());
if (bhdrchk == false) {
bhdrchk = packet->checkheader();
//if (bhdrchk)
// bhdrchk = true;
packet->SkipHeader();

}
if (!packet->checkcontent())
{
STOP = true;
// continue;
}

if (packet->CheckCheckSum())
{
printf ("Warning: Invalid CheckSum\n");
// continue;
}
else
printf ("CheckSum Correct\n");
// if (packet->CheckTerminate())
// {
// STOP = true;
// continue;
// }



// if (packet->GetEOFStatus())
// STOP = true;
// else
// STOP = false;
memset (packet->GetBuff(), 0, iPacketSize);
// buff[0] = 0;;
// packet->SetBuff(buff);
// STOP = TRUE;

bhdrchk = false;

packet->SkipCheckSum(); // skip chksum
}

}

tcsetattr (fileHandle, TCSANOW, &oldtio);
// checkheader(buff);
close (fileHandle);
}

bool PacketEx::CheckTerminate(void)
{

}





void signal_handler_IO(int a)
{
}


//////////////
// packet.cpp

//
// C++ Implementation: Packet
//
// Description:
//
//
// Author: root <root@dhcppc2>, (C) 2005
//
// Copyright: See COPYING file that comes with this distribution
//
//
#include "rmves.h"
#include "Packet.h"
////
//#include "RS232.h"

extern "C" {
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
}
///////////
//extern RS232_Controller *rs232;
////////////
PacketEx::PacketEx(int iDataSize)
{
printf ("iDataSize = %d\n", iDataSize);
m_iDataSize = iDataSize;
printf ("m_iDataSize = %d\n", m_iDataSize);
m_iPacketSize = SIZEOF_CHECKSUM+m_iDataSize+SIZEOF_HEADER;
printf ("m_iPacketSize = %d\n", m_iPacketSize);
m_pPacket = (unsigned char *) malloc (m_iPacketSize);
m_pData = (unsigned char *) malloc (m_iDataSize);
// m_pData = (unsigned char *) malloc (22);
m_pData = m_pPacket + SIZEOF_HEADER;
printf ("m_pData is %s\n", m_pData);
printf ("strlen of m_pdata is %d\n", strlen((const char *) m_pData));
// for validation
m_pCheckSumData = (char *) malloc(22);
memcpy (m_pCheckSumData, m_pData, 22);
printf ("strlen m_pCheckSumData = %d\n", strlen ((const char *)m_pCheckSumData));
printf ("m_pCheckSumData = %s\n", m_pCheckSumData);
// memcpy (m_pChkSumData, m_pData, m_iPacketSize);
// SkipCheckSumData();
// m_pStreamChkSumData = (char *) malloc(m_iPacketSize);
// memcpy (m_pStreamChkSumData, m_pData, m_iPacketSize);
// SkipStreamChkSumData();
}

void PacketEx::SetBuff(unsigned char *buff)
{
m_pData = buff;
}

unsigned char *PacketEx::GetBuff(void)
{
return m_pData;
}

bool PacketEx::CheckCheckSum(void)
{
char CheckSum;;
char sent;

char diff;

CheckSum = CalcByteCheckSum();
sent = GetCheckSum();
diff = sent+CheckSum;
printf ("CheckSum is %c\n", CheckSum);
printf ("sent is %c\n", sent);
printf ("diff is %c\n", diff);


//m_pData++;
return (diff == 0);


}

char PacketEx::CalcByteCheckSum(void)
{
unsigned char chksum = 0;
// Starting with '#'
unsigned char *pData = m_pData;


int count = m_iDataSize;
for (int x = 0; x < count; x++)
{
chksum += *pData++;
}
return chksum;
}



char PacketEx::GetCheckSum(void)
{
// 1 2 3 4 5 6 7 8 cs1 cs2
// Get these 2 bytes
return *(m_pCheckSumData)<< 4 | *(m_pCheckSumData+1);
}

bool PacketEx::CheckEOF(void)
{
// static char EOFMarker = CARRIAGE_RETURN; // return code
// 1 2 3 4 5 6 7 8 cs1 cs2 #
// ^Currentpos
return (*(m_pData+2) == '#' || *(m_pData+2) == CARRIAGE_RETURN || (*m_pData == NULL));
}


bool PacketEx::GetEOFStatus(void)
{
return m_bEOF;
}


////////////////////////////////////////////// below is compiled okay
bool PacketEx::checkheader(void)
{
const char *teststring = "#z";
char *hdr;
bool bhdrchk2 = false;

hdr = (char *)malloc (2);
strncpy (hdr, (const char *) m_pData, 2);
printf ("teststring = %s, hdr = %s\n", teststring, hdr);

if (!strncmp (hdr, teststring, 2))
printf ("Header Correct\n");
else
printf ("Header incorrect\n");

bhdrchk2 = true;
return bhdrchk2;
}

float PacketEx::GetFloatData (const unsigned int DataIndex)
{
long temp;
float ret;
long *cast;
cast = (long *)&ret;
// unsigned int idx = DataIndex * 8;


// This is the implanted version
printf ("Size of long: %d\n", sizeof (long));
printf ("Size of float: %d\n", sizeof (float));


temp = (long) AsciiToHex (m_pData[0]) << 28 |
(long) AsciiToHex (m_pData[1]) << 24 |
(long) AsciiToHex (m_pData[2]) << 20 |
(long) AsciiToHex (m_pData[3]) << 16 |
(long) AsciiToHex (m_pData[4]) << 12 |
(long) AsciiToHex (m_pData[5]) << 8 |
(long) AsciiToHex (m_pData[6]) << 4 |
(long) AsciiToHex (m_pData[7]);
*cast = temp;
printf ("Temp is %ld\n", temp);
return ret;
}


/*short PacketEx::GetShortData (const unsigned int DataIndex)
{
//unsigned int idx = DataIndex * 4;

}*/

//////////////
char PacketEx::AsciiToHex (char hexchar)
{
printf ("char is %c\n", hexchar);
if (hexchar >= '0' && hexchar <= '9')
return hexchar - '0';
else if (hexchar >= 'A' && hexchar <= 'F')
return hexchar - 'A' + 10;

// if (hexchar == '#' || hexchar == 'z')
// return hexchar;

fprintf (stderr, "EOF, Invalid or Corrupted Data\n");
return NULL;
// exit (-1);
}


bool PacketEx::checkcontent (void)
{
unsigned int i;
float j;

// Get a bundle of data from the sensor
for (i = 0; i < NO_OF_ITERATIONS; i++) {
// Check whether it is "No more data", ended with "56" or carriage return
if (CheckEOF() || i >= 50) {
m_bEOF = true;
return true;
}



j = GetFloatData (i);
printf ("Reading %5.2f\n", j);
m_pData+=8;
}

}




short PacketEx::GetShortData (const unsigned int DataIndex)
{

}


I have been staring at this code for ages. If anyone sight the logic error, could you help anyone?

The problem I am facing is,
when i have a data stream on the serial port say
#a 01020304 0a0b0c0d 56
The readings were correct, but
The first loop read up to 0d, which meant 56 appeared on the subsequent read, which was wrong! I need one read until the end of the string. Anyone? And could anyone check with me checksum stuff? I am having trouble with that too.
Thanks
Jack
 
Old 01-16-2006, 03:20 AM   #4
lucky6969b
Member
 
Registered: Nov 2005
Posts: 337

Original Poster
Rep: Reputation: 30
A little bit longgish, sorry about that. And my senior has been complaining about revealing code to public. But I had nothing to choose other than post... it is open source mate!
Thanks
Jack
 
Old 01-16-2006, 09:30 PM   #5
foo_bar_foo
Senior Member
 
Registered: Jun 2004
Posts: 2,553

Rep: Reputation: 51
this is what i meant anyway
Code:
size_t bytes_read;

do {
  bytes_read = read(fileHandle, buff, sizeof(buff));
}
while  (bytes_read == sizeof(buf));
that way it goes back until in finds the end
 
Old 01-18-2006, 07:33 PM   #6
lucky6969b
Member
 
Registered: Nov 2005
Posts: 337

Original Poster
Rep: Reputation: 30
Hi,
The program will read variable-length of characters. Is that okay too?
Thanks
Jack
 
Old 01-18-2006, 09:42 PM   #7
foo_bar_foo
Senior Member
 
Registered: Jun 2004
Posts: 2,553

Rep: Reputation: 51
yea the logic is so simple i try again (not that this is the only solution)

if when you read the buffer and the whole darn thing is full up then go back and see if there is some you left behind. But if you got to the end of whatever was in there then that must be the end of whatever was in there.

someone else have ideas about this ?
 
  


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
Ide Flushing Colinux_Tlinux_noob Linux - Newbie 2 10-14-2005 12:25 AM
Iptables listing and flushing stefaandk Linux - Security 2 07-27-2005 05:55 PM
iptable flushing name_in_use450 Linux - Security 1 06-07-2004 01:26 PM
About buffers? eshwar_ind Programming 2 04-30-2004 05:13 AM
flushing RAM? carboncopy Slackware 11 07-31-2003 11:03 PM


All times are GMT -5. The time now is 03:54 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration