Solved

Reading Real time data thru RS232 port!!

Posted on 2002-03-24
6
17,365 Views
Last Modified: 2013-12-13


Hi

I have wriiten a program to read from a data file from disk and plot result. Now I want to obtain my data thru the serial port coming from a device that reads patient's heart data. The data comes every second and written to a file. I have to read it as it is comming from the device. But since I have to read the data in real time while my device reading the patient heart data and then plot it when the device stops (after 60 minutes max).
The problem I am facing is when the file is open by my medical device for writing then how can I read it?
Is there a way to do this please ?
All thoughts and suggestions are welcome.

PS: I don't have the interface between the medical device and the computer yet. It is going to be a card.

Thanks
0
Comment
Question by:nawaf
6 Comments
 
LVL 22

Expert Comment

by:cookre
Comment Utility
Have you tried explicitly opening the file in read only mode in the plot program?
0
 
LVL 22

Expert Comment

by:cookre
Comment Utility
Also, much depends on how the file is opened by the monitor program.  If you open it exclusive, don't.

If the monitor program opens it in buffered mode, then the plot ptogram won't always have up to the second data and will also have to deal with partial records.

Since you only sample once per second, you'll have plenty of time to flush the file after each write.

Which OS and language are you using?
0
 
LVL 2

Expert Comment

by:Ariel Garcia
Comment Utility
Please>>

Specify lenguage for APPs

In you can get a file OCX for this propouse i dont get the address site but i can search....

answer this before i search...
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:nawaf
Comment Utility
I am using fortran C.Thanks
0
 
LVL 22

Accepted Solution

by:
cookre earned 200 total points
Comment Utility
For lack of anymore details, here's some sample code that use a normal stream to capture and save observations once per second.  Note the fflush() after each write.  I simulated the data fetch with rand().

#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <windows.h>

FILE  *DataFile;
int    ctr;
char   DateStr[9];
char   TimeStr[9];

int main(int argc, char* argv[])
{
DataFile=fopen("c:\\testy.dat","w+t");
if (DataFile==NULL)
   {
   printf("Can't open data file\n");
   return 1;
   }
srand((unsigned)time(NULL));
for (ctr=0; ctr<3600; ctr++)
    {
    _strdate(DateStr);
    _strtime(TimeStr);
    fprintf(DataFile,"Obs %03d on %s at %s = %010d\n",ctr+1,DateStr,TimeStr,rand());
    fflush(DataFile);
    Sleep(1000);
    }
fclose(DataFile);
return 0;
}


While that program is running, you can run this one any number of times to take a look at what has been captured and saved by the above.  Note that the data fie is opened 'read only' and that each time the program is run it appends to the listing file.

#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <windows.h>

FILE  *DataFile;
FILE  *ListFile;

char   DateStr[9];
char   TimeStr[9];
char   LineIn[256];


int main(int argc, char* argv[])
{

DataFile=fopen("c:\\testy.dat","rt");
if (DataFile==NULL)
   {
   printf("Can't open data file\n");
   return 1;
   }

ListFile=fopen("c:\\testy.txt","a+t");
if (ListFile==NULL)
   {
   printf("Can't open list file\n");
   return 1;
   }
_strdate(DateStr);
_strtime(TimeStr);
fprintf(ListFile,"Observations captured %s %s\n",DateStr,TimeStr);

while(NULL!=fgets(LineIn,256,DataFile))
     {
     fprintf(ListFile,"   %s\n",LineIn);
     }
fclose(ListFile);
fclose(DataFile);
return 0;
}
0
 

Expert Comment

by:antonyleung
Comment Utility
I will use VB and a control mscomm32.ocx to implement your solution.  Since, I don't know the protocol of your device, I guess it just return some characters with control bits.

The mscomm32 object provides an OnComm event to handle all the signal transfer between PC and an input device.  As you see from the codes below, I will read the incoming signal into a buffer and scan through the buffer to remove all the unwanted control signal (you should refer to your hardware manual to find out what are they), then print it on a textbox for realtime monitoring and at the same time write it to a record file.

The codes follows:


Public PrepareLog as Boolean

Private Static Sub MSComm1_OnComm()
    Dim SignalString$  
    Select Case MSComm1.CommEvent
        ' Event messages.
        Case comEvReceive
            Dim buffer As Variant
            buffer = MSComm1.Input
           
         ' Eliminate transferring signal from screen.
            SignalString$ = Chr$(8) & Chr$(32) & Chr$(8)  'the unwnated control signal of the device
            For i = 1 To Len(buffer)
                If i = InStr(buffer, SignalString$) Then
                    buffer = Left$(buffer, i - 1) & Mid$(buffer, i + 3)
                End If
            Next
 
         'provide onscreen realtime signal respond on a textbox "txtTerminal"    
           txtTerminal.Text = txtTerminal.Text & buffer
           txtTerminal.SelStart = Len(txtTerminal.Text)

        'write the file to record the process
        PrintLog buffer, filelognum

           txtTerminal.SelStart = Len(txtTerminal.Text)
    End Select
End Sub

Public Sub PrintLog(PrintString As String, FileNo As Integer)
    If PrepareLog = False Then Form_Initialize
    Dim length As Long
    length = LOF(filenum)
    Put #FileNo, length + 1, PrintString
End Sub

Private Sub Form_Initialize()
start = Now
PrepareLog = True
    filelognum = FreeFile
    OpenLog
        PrintLog "This file record down all the comm-port activities that carried out by the host.", filelognum
        PrintLog "Written by Antony Leung.", filelognum
        PrintLog "********************************************************************************", filelognum
        PrintLog "Application:" + App.EXEName + ".exe", filelognum
        PrintLog "Working path:" + App.Path, filelognum
        PrintLog "Login Time:" + Format(start), filelognum
        PrintLog "", filelognum
End Sub

Public Sub OpenLog()
    Open App.Path & "\filename.extension" For Binary As #filelognum 'your record file goes here
End Sub
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

I know it’s not a new topic to discuss and it has lots of online contents already available over the net. But Then I thought it would be useful to this site’s visitors and can have online repository on vim most commonly used commands. This post h…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now