ПЗ (1204320), страница 8
Текст из файла (страница 8)
}
}
void GatherDialog::on_startStopButton_clicked(bool checked)
{
if(checked){
potok->changeState(true);
potok->start();
ui->startStopButton->setText("Stop");
ui->connectButton->setEnabled(false);
ui->saveButton->setEnabled(false);
}
else{
potok->changeState(false);
ui->startStopButton->setText("Start");
ui->connectButton->setEnabled(true);
ui->saveButton->setEnabled(true);
}
}
void GatherDialog::on_saveButton_clicked(){
saveFilename = QFileDialog::getSaveFileName(this,QString("Место для сохранения файла"),QStandardPaths::writableLocation(QStandardPaths::DesktopLocation),"Файл данных (*.dat)");
potok->saveToFile(saveFilename,ui->saveTxtCheckBox->isChecked());
}
void GatherDialog::on_testButton_clicked()
{
potok->testRun();
}
void GatherDialog::on_settingButton_clicked()
{
SetingsDialog setting;
setting.setData(potok->getData());
setting.exec();
potok->setData(setting.getData());
}
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
void WorkerThread::testRun(){
U32 st = RSH_API_SUCCESS;
RSH_U32 timeToWait = 10000;
RshInitDMA p;
double a[2];a[0]=0;a[1]=0;
p.startType = RshInitDMA::Program; //программное управление
p.dmaMode = RshInitDMA::Single; //сбор в режиме старт стоп
p.SetDiffMode(true); //диференциальный режим
p.bufferSize = 1024;
p.frequency = SampleFreq;
p.channels.SetSize(ChannelNumber);
for(uint i=0;i<ChannelNumber;++i){
p.channels[i].SetUsed();
p.channels[i].gain = GainKoef;
}
st = device->Init(&p);
if (st != RSH_API_SUCCESS){
errorEmit("device->Init Error: ");
return;
}
else qDebug() << "Initializate Complete";
RSH_BUFFER_S16 iBuffer( p.bufferSize * ChannelNumber );
st = device->Start();
if (st != RSH_API_SUCCESS){
errorEmit("device->Start() Error: ");
return;
}
st = device->Get(RSH_GET_WAIT_BUFFER_READY_EVENT, &timeToWait);
if (st != RSH_API_SUCCESS){
errorEmit("RSH_GET_WAIT_BUFFER_READY_EVENT Error: ");
return;
}
st = device->GetData(&iBuffer);
if (st != RSH_API_SUCCESS){
errorEmit("device->GetData Error: ");
return;
}
device->Stop();
for(int i=0; i<iBuffer.Size(); i++){
a[i%2]+=iBuffer[i]>>4;
if(a[i%2]!=0) a[i%2]/=2;
}
iBuffer.Clear();
emit gatherBlock(QString("Поджатие первого датчика: " + QVariant(a[0]).toString() + "\n""Поджатие второго датчика: " + QVariant(a[1]).toString()),1);
}
void WorkerThread::run()
{
qDebug() << "Thread start";
// qDebug() << BufferSize;
// qDebug() << SampleFreq;
// qDebug() << ChannelNumber;
// qDebug() << GainKoef;
U32 st = RSH_API_SUCCESS;
uint warningBufferCount = 10000;
uint Loops = 0;
RshInitDMA p;
p.startType = RshInitDMA::Program;
p.dmaMode = RshInitDMA::Persistent;
p.SetDiffMode();
p.bufferSize = BufferSize;
p.frequency = SampleFreq;
p.channels.SetSize(ChannelNumber);
for(uint i=0;i<ChannelNumber;++i){
p.channels[i].SetUsed();
p.channels[i].gain = GainKoef;
}
st = device->Init(&p);
if (st != RSH_API_SUCCESS){
errorEmit("device->Init Error: ");
return;
}
else qDebug() << "Initializate Complete";
arrayBuffer.clear();
arrayBuffer.resize(warningBufferCount);
U32 bBufSize = arrayBuffer[0].TypeSize() * p.bufferSize * p.ActiveChannelsNumber();
RSH_U32 timeToWait = 10000;
st = device->Start();
if (st != RSH_API_SUCCESS){
errorEmit("device->Start() Error: ");
qDebug() << st;
return;
}
do{
arrayBuffer[Loops].Allocate(p.bufferSize * p.ActiveChannelsNumber());
st = device->Get(RSH_GET_WAIT_BUFFER_READY_EVENT, &timeToWait);
if (st != RSH_API_SUCCESS){
errorEmit("RSH_GET_WAIT_BUFFER_READY_EVENT Error: ");
return;
}
st = device->GetData(&arrayBuffer[Loops]);
if (st != RSH_API_SUCCESS){
errorEmit("device->GetData Error: ");
return;
}
qDebug() << Loops << " :: " << arrayBuffer[Loops][0] << arrayBuffer[Loops][2] << arrayBuffer[Loops][4] << arrayBuffer[Loops][6] ;
emit gatherBlock(QVariant(Loops+1).toString(),0);
} while (state && (++Loops != warningBufferCount));
device->Stop();
qDebug() << "Data successfully collected!";
userBuffer1.Allocate( bBufSize * Loops);
for(int i=0; i<Loops; i++){
memcpy(userBuffer1.ptr + i * bBufSize, arrayBuffer[i].ptr, arrayBuffer[i].ByteSize());
}
arrayBuffer.resize(Loops);
}
void WorkerThread::changeState(bool st)
{
state = st;
}
void WorkerThread::setData(QVector<ushort> a)
{
SampleFreq = (double)(a[0]);
BufferSize = a[1];
ChannelNumber = a[2];
GainKoef = a[3];
}
QVector<ushort> WorkerThread::getData()
{
QVector<ushort> ret;
ret.append((ushort)(SampleFreq));
ret.append(BufferSize);
ret.append(ChannelNumber);
ret.append(GainKoef);
return ret;
}
bool WorkerThread::connect(U32 deviceId, QString boardName)
{
U32 st = RSH_API_SUCCESS;
device = 0;
SampleFreq = 25000;
BufferSize = 4096;
ChannelNumber = 2;
GainKoef = 1;
qDebug() << boardName;
RshDllInterfaceKey DevKey(boardName.toUtf8().data(), device);
st = Client.GetDeviceInterface(DevKey);
if (st != RSH_API_SUCCESS){
emit errorEmit("Get Device Interface Error: ");
return false;
}
else emit gatherBlock(QString("Success Get Device Interface"),1);
RSH_S8P libname = 0;
RSH_S8P libVersion = 0;
RSH_S8P libCoreName = 0;
RSH_S8P libCoreVersion = 0;
st = device->Get(RSH_GET_LIBRARY_FILENAME, &libname);
if (st != RSH_API_SUCCESS){
emit errorEmit("Get(RSH_GET_LIBRARY_FILENAME) Error: ");
return false;
}
else qDebug() << "Library Name: "<< libname;
st = device->Get(RSH_GET_LIBRARY_VERSION_STR, &libVersion);
if (st != RSH_API_SUCCESS){
emit errorEmit("Get(RSH_GET_LIBRARY_VERSION_STR) Error: ");
return false;
}
else qDebug() << "Library Version: " << libVersion;
st = device->Get(RSH_GET_CORELIB_FILENAME, &libCoreName);
if (st != RSH_API_SUCCESS){
emit errorEmit("Get(RSH_GET_CORELIB_FILENAME) Error: ");
return false;
}
else qDebug() << "Core Library Name: " << libCoreName;
st = device->Get(RSH_GET_CORELIB_VERSION_STR, &libCoreVersion);
if (st != RSH_API_SUCCESS){
emit errorEmit("Get(RSH_GET_CORELIB_VERSION_STR) Error: ");
return false;
}
else qDebug() << "Core Library Version: " << libCoreVersion;
RSH_U32 caps = RSH_CAPS_SOFT_PGATHERING_IS_AVAILABLE;
st = device->Get(RSH_GET_DEVICE_IS_CAPABLE, &caps);
if (st != RSH_API_SUCCESS){
emit errorEmit("Get(RSH_GET_DEVICE_IS_CAPABLE) Error: ");
return false;
}
else emit gatherBlock(QString("PGATHERING_AVAILABLE"),1);
RshDeviceKey connectKey(deviceId);
emit gatherBlock(QString("Connecting to device (it may take some time)... "),1);
st = device->Connect(&connectKey);
if (st != RSH_API_SUCCESS){
emit errorEmit("Connect Error: ");
return false;
}
else emit gatherBlock(QString("Connect Success"),1);
RSH_S8P fullDeviceName = 0;
st = device->Get(RSH_GET_DEVICE_NAME_VERBOSE, &fullDeviceName);
if (st != RSH_API_SUCCESS){
emit errorEmit("Get(RSH_GET_DEVICE_NAME_VERBOSE) Error: ");
return false;
}
else emit gatherBlock(QString("The name of the connected device: " + QVariant((char*)(fullDeviceName.data)).toString()),1); ;
RSH_U32 serialNumber = 0;
st = device->Get(RSH_GET_DEVICE_SERIAL_NUMBER, &serialNumber);
if (st != RSH_API_SUCCESS){
emit errorEmit("Get(RSH_GET_DEVICE_SERIAL_NUMBER) Error: ");
return false;
}
else emit gatherBlock(QString("Serial number of the connected device: " + QVariant(static_cast<int>(serialNumber.data)).toString()),1);
return true;
}
bool WorkerThread::saveToFile(QString str,bool saveTxt)
{
U32 st = RSH_API_SUCCESS;
std::string fileName = str.toStdString();
st = userBuffer1.WriteBufferToFile(fileName);
if( st == RSH_API_SUCCESS ){
emit gatherBlock(QString("Данные успешно собраны и сохранены в " + str),1);
emit gatherBlock(QString("Размер файла: " +QVariant(userBuffer1.BytePSize()/1024.0).toString() + " KB"),1);
// return true;
}else{
emit gatherBlock(QString("Невозможно записать в файл " + str),1);
emit errorEmit("Ошибка при записи в файл.");
// return false;
}
if(saveTxt){
str.append(".txt");
QFile F1(str);
F1.open(QIODevice::WriteOnly|QIODevice::Text);
if(F1.isOpen()){
QString h;
QString stat("Дата/время сбора: "+QDateTime::currentDateTime().toString("dd-MM-yy hh:mm:ss")+"\n"+\
"Размер буфера (на канал): " + QVariant((uint)BufferSize).toString()+" байт\n"+\
"Количество каналов: " + QVariant((uint)ChannelNumber).toString()+"\n"+\
"Коэфициент усиления: х" + QVariant((uint)GainKoef).toString()+"\n"+\
"Частота: " + QVariant(SampleFreq).toString()+" Гц\n");
F1.write(stat.toUtf8());
for(int i=0; i<ChannelNumber; i++){
h = "Канал"+QVariant(i).toString()+"\t";
F1.write(h.toUtf8());
}
for(int i=0; i<arrayBuffer.size(); i++){
for(int j=0; j<arrayBuffer[i].size(); j+=ChannelNumber){
for(int k=0; k<ChannelNumber; k++){
h = QVariant(arrayBuffer[i][j+k]>>4).toString()+"\t";
F1.write(h.toUtf8());
}
F1.write("\n");
}
emit gatherBlock(QString("txt: " + QVariant(i+1).toString() + " из " + QVariant(arrayBuffer.size()).toString()),2);
}
emit gatherBlock(QString("txt: Завершено"),2);
F1.close();
}
else st = 32;
}
if( st == RSH_API_SUCCESS ){
emit gatherBlock(QString("Данные успешно собраны и сохранены в " + str),1);
emit gatherBlock(QString("Размер файла: " +QVariant(userBuffer1.BytePSize()/1024.0).toString() + " KB"),1);
return true;
}else{
emit gatherBlock(QString("Невозможно записать в файл " + str),1);
emit errorEmit("Ошибка при записи в файл.");
return false;
}
}
SettingsWindow.cpp
#include "setingsdialog.h"
#include "ui_setingsdialog.h"















