Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9171273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:08:25+00:00 2026-06-17T16:08:25+00:00

I am writing a code that receives heart sound from a hardware device using

  • 0

I am writing a code that receives heart sound from a hardware device using comports and plots the signal received. I am using OpenGl to plot the signal received. I am able to plot when I receive the complete data. But I want the graph to update with the updating of data received Here is my code:

    int num_samples = 100000;


long samples[100000];
DWORD        bytes_read    = 0;    // Number of bytes read from port


/* Function plotting func */
void draw( float x1, float x2, float y1, float y2, int N)
{
    float x, dx = 1.0/N; 
glPushMatrix(); /* GL_MODELVIEW is default */

glScalef(1.0 / (x2 - x1), 1.0 / (y2 - y1), 1.0);
glTranslatef(-x1, -y1, 0.0);
glColor3f(1.0, 1.0, 1.0);

glBegin(GL_LINE_STRIP); 
int k =0;
for(x = 0; x < num_samples; x += dx)
{
    glVertex2f(x, samples[k]);
    k=k+1;
}

glEnd();

glPopMatrix();

glFlush();
};

/* Redrawing func */
void redraw(void)
{
    glClearColor(0, 0, 0, 0);
    glClear(GL_COLOR_BUFFER_BIT);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    //long buf = getBuffer(samples);
    draw(0, num_samples,-693554432, 693554432, 1);
    //  draw(func, 0, 1000,0, 5000, 1);

    glutSwapBuffers();
    //glFlush();
};

/* Idle proc. Redisplays, if called. */
void idle(void)
{
    glutPostRedisplay();
};

/* Key press processing */
void key(unsigned char c, int x, int y)
{
    if(c == 27) exit(0);
};

/* Window reashape */
void reshape(int w, int h)
{
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, 1, 0, 1, -1, 1);
    glMatrixMode(GL_MODELVIEW);
};
/*
long putBuffer(long[] buffer)
{
    return buffer;
}*/

 //Main function 

int j =0;
unsigned long size = 400000;

int main(int argc, char* argv[])
{
    unsigned char INBUFFER[400000];

    char OUTBUFFER[20];
    DWORD        bytes_read    = 0;    // Number of bytes read from port
    DWORD        bytes_written = 0;    // Number of bytes written to the port
    HANDLE       comport      = NULL;  // Handle COM port
 int   bStatus;
    DCB          comSettings;          // Contains various port settings
    COMMTIMEOUTS CommTimeouts;
    strcpy(&OUTBUFFER[0], "The quick brown fox jumped over the lazy dog. \n\r\0");
    // Open COM port
    if ((comport = 
         CreateFile("\\\\.\\COM44",                // open com5:
                    GENERIC_READ | GENERIC_WRITE, // for reading and writing
                    0,                            // exclusive access
                    NULL,                         // no security attributes
                    OPEN_EXISTING,              
                    FILE_ATTRIBUTE_NORMAL,
                    NULL)) == INVALID_HANDLE_VALUE)
    {
       // cout<<"Port can't be opened"<<endl;
    }
    // Set timeouts in milliseconds
//  cout<<"Port opened"<<endl;
    //DCB dcb;
    CommTimeouts.ReadIntervalTimeout         = 0; 
    CommTimeouts.ReadTotalTimeoutMultiplier  = 0; 
    CommTimeouts.ReadTotalTimeoutConstant    = 100;
    CommTimeouts.WriteTotalTimeoutMultiplier = 0;
    CommTimeouts.WriteTotalTimeoutConstant   = 100;
    bStatus = SetCommTimeouts(comport,&CommTimeouts);
    if (bStatus != 0)
    {
        // error processing code goes here
    }
    // Set Port parameters.
    // Make a call to GetCommState() first in order to fill
    // the comSettings structure with all the necessary values.
    // Then change the ones you want and call SetCommState().
    GetCommState(comport, &comSettings);
   // memset(&dcb,0,sizeof(dcb));

    comSettings.fBinary = 1;
    comSettings.fDtrControl = DTR_CONTROL_ENABLE;
    comSettings.fRtsControl = RTS_CONTROL_ENABLE;
    comSettings.fOutxCtsFlow = 1;
    comSettings.fRtsControl = DTR_CONTROL_HANDSHAKE;


    comSettings.BaudRate = 921600;
    comSettings.StopBits = ONESTOPBIT;
    comSettings.ByteSize = 8;
    comSettings.Parity   = NOPARITY;
    comSettings.fParity  = FALSE;

    bStatus = SetCommState(comport, &comSettings);
    ofstream outdata;
     outdata.open("hsm.txt"); // opens the file
   if( !outdata ) { // file couldn't be opened
      cerr << "Error: file could not be opened" << endl;
      exit(1);
   }
    if (bStatus == 0)
    {
        // error processing code goes here
    }
    int flag = 1;
    int fl =1;
    glutInit(&argc, argv);
    glutInitWindowSize(500,500);
    glutInitWindowPosition(500,200);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);


    //glutMainLoop();

  while(flag<120)
   {

        bStatus = ReadFile(comport,   // Handle
                &INBUFFER,            // Incoming data
                size,                  // Number of bytes to read
                &bytes_read,          // Number of bytes read
                NULL);
        if (bStatus != 0)
        {
            //cout<<"Error receiving"<<endl;
        }
//  long samples[100000];

 for (int i=0; i<bytes_read; i++)
 {
    if((unsigned char)INBUFFER[i]==0x99&&(unsigned char)INBUFFER[i+4]==0x99)

    {
    samples[j] = 0x00|(unsigned char)INBUFFER[i+1]<<8|(unsigned char)INBUFFER[i+2]<<16|(unsigned char)INBUFFER[i+3]<<24;
    if(samples[j]!=0)
         {
          outdata << samples[j] <<"\n";
          j++;
         }      
    }


 } 

    flag++;
    glutCreateWindow("Graph plotter");
    glutDisplayFunc(redraw);
    glutReshapeFunc(reshape);
    glutIdleFunc(idle);
    glutMainLoop();

  }
  CloseHandle(comport);
outdata.close();


 return 0;
}

Basically when the samples buffer gets updated, the graph should also update. But the graph is plotted in first iteration and then nothing happens.
Can anyone tell me how to fix it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:08:26+00:00Added an answer on June 17, 2026 at 4:08 pm

    When you call glutMainLoop, it will draw the screen, handle window events, and call the idle function repeatedly. Since all your idle function does now is to ask for the screen to be redrawn, it will not be reading additional samples from your input serial port.

    You should try reading from the serial port and populating additional samples when your idle function is called.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using c#. In many cases I'm writing code that can benefit from a
I am writing some code that connects to a website, and using C#, and
I am writing some testing software that receives some source code, compiles it on
I'm writing a UDP server that currently receives data from UDP wraps it up
The code I'm writing receives an ArrayList from unmanaged code, and this ArrayList will
I am writing some code that receives functions with variable numbers of arguments. I
I'm using a javascript function that receives some html code that was included using
I am currently writing an iPhone application that sends and receives JSON data from
I'm writing some JavaScript/AJAX code. Is there anyway to ensure that the server receives
I am writing an application that receives some input from a long range radio

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.