Dear BCI2000 community !
I wanted to ask a question about the implementation of the blocking read-function that gets a sample block from an A/D-Source:
I created a function 'read_channels' as part of my
'ModularEEGADC' - EEG-Data-Source that supports the "Modular EEG"-Amplifier by Joerg Hansmann (this is an rs232-device that sends 256Hz 10bit 6 Channel EEG-data, see: http://openeeg.sf.net for details)
First i did not specify any timeout and issued a sleep(0) - command in case that no byte is available from the serial port. This produced heavy CPU load and the process did not terminate in case the EEG is disconnected or switched off. (when no more samples arrive, the current packet will not be finished and the function will not return)
so i decided to use a timeout (set to ca. 50 ms) that prevents this failure.
now, the data aquisition seems to work properly, but i am not sure about the jitter.
do you have any ideas or comments about the code ?
i could post/upload the new modules to a site if someone wants to use
the very affordable modularEEG system.
best regards,
chris veigl.
void read_channels (ser_t handle, int protocol)
// reads one packet (all 6 channels) from serial port
// this function is called n-times by the process() member-function
// to get a sample block of size n.
{
unsigned char buf[1];
BCITIME time1, time2;
time1=BCITIME::GetBCItime_ms(); // get timestamp for timeout-test
do
{
if (readSerial(handle, buf, 1)==1) // read one byte from serial port
{ // byte available: parse the selected protocol
if (protocol==2) parse_byte_P3(buf[0]);
else parse_byte_P2(buf[0]);
}
else Sleep(1); // no byte available: free CPU
// sleep(0) caused a CPU-load of more than 50% on a 1,6 GHZ win2000 laptop
// sleep(1) improved this situation
}
while ((PACKET.readstate!=PACKET_FINISHED) &&
(BCITIME::TimeDiff(time1,BCITIME::GetBCItime_ms())<COM_TIMEOUT));
// read until the packet is finished or a timeout has occured
PACKET.readstate=0;
}
Modular EEG Data Source
-
mellinger
- Posts: 1341
- Joined: 12 Feb 2003, 11:06
Try using a relatively large timeout (e.g., a few seconds) to allow for detecting a broken serial connection.
Ideally, your readSerial() function should block indefinitely while there is no data available -- thus, no CPU time will be used when there is no data processing taking place.
If the large timeout is exceeded, this will indicate a broken connection, so you should abort data acquisition by writing an error message into the bcierr stream as in
bcierr << "Connection broken" << endl;
Hope this helps,
Juergen
Ideally, your readSerial() function should block indefinitely while there is no data available -- thus, no CPU time will be used when there is no data processing taking place.
If the large timeout is exceeded, this will indicate a broken connection, so you should abort data acquisition by writing an error message into the bcierr stream as in
bcierr << "Connection broken" << endl;
Hope this helps,
Juergen
Who is online
Users browsing this forum: No registered users and 0 guests
