get_uart.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include "get_uart.h"
  2. #include <QDebug>
  3. Get_Uart::Get_Uart(QString s) : name(s), theGraphData(N_M_GRAPH_DATA_SIZE, N_P_GRAPH_DATA_SIZE)
  4. {
  5. }
  6. void Get_Uart::doWork()
  7. {
  8. //ADC1_y_val = QVector<double>(1200);
  9. //ADC2_y_val = QVector<double>(1200);
  10. //ADC_Count = QVector<double>(1200);
  11. emit send_graph_pointer(&theGraphData);
  12. graph_delay = 0;
  13. graph_count = 10;
  14. graph_offset = 0;
  15. f_delta_g = 0;
  16. m_pos_g = 0;
  17. f_delta_old = -1;
  18. m_pos_old = -1;
  19. theGraphData.g_cur_pos = 0;
  20. out_data.setFileName("./out_data.txt");
  21. out_data.open(QIODevice::WriteOnly);
  22. out_data.write("A - Sensor position \r\nB - POTENCIAL\r\nC - Force delta\r\nD - FORCE\r\n\r\nA\tB\tC\tD\r\n");
  23. this->serial = new QSerialPort(this);
  24. // this-> OpenSerial();
  25. }
  26. void Get_Uart::OpenSerial(QString port)
  27. {
  28. this->serial->setPortName(port);
  29. // this->serial->setPortName("/dev/pts/16");
  30. this->serial->setBaudRate(QSerialPort::Baud115200);
  31. this->serial->setDataBits(QSerialPort::Data8);
  32. this->serial->setFlowControl(QSerialPort::NoFlowControl);
  33. this->serial->setParity(QSerialPort::NoParity);
  34. this->serial->setStopBits(QSerialPort::OneStop);
  35. if (serial->open(QIODevice::ReadWrite)) {
  36. emit send_err("Connected.");
  37. qDebug() << "yes";
  38. connect(this->serial, SIGNAL(readyRead()), this, SLOT(ReadSerial()));
  39. } else {
  40. emit send_err("Can not open serial port!");
  41. qDebug() << "no";
  42. }
  43. }
  44. void Get_Uart::ReadSerial()
  45. {
  46. if (serial->canReadLine()){
  47. data = serial->readLine();
  48. qDebug() << data << " " << f_delta_g << graph_offset;
  49. QRegExp rx("[ ]+");
  50. QStringList stringlist = data.split(rx);
  51. QString ende = stringlist[10];
  52. QString starte = stringlist[0];
  53. if (starte[0] == 'B' && ende[0] == 'E'){ //проверка что строка не битая
  54. ADC1_y = stringlist[5].toInt() - 8388810; //8388810 - это "ноль"
  55. ADC2_y = stringlist[6].toInt() - 8388810;
  56. QByteArray data1 = stringlist[7].toUtf8() + "\t" + QByteArray::number(ADC1_y) + "\t" + stringlist[8].toUtf8() + "\t" + QByteArray::number(ADC2_y) + "\r\n";
  57. out_data.write(data1);
  58. // qDebug() << ADC1_y << ADC2_y;
  59. f_delta = stringlist[8].toInt();
  60. if (f_delta < 0){
  61. f_delta = 0;
  62. }
  63. m_position = stringlist[7].toInt();
  64. if (f_delta_g >= N_P_GRAPH_DATA_SIZE - 1){
  65. graph_offset += N_P_GRAPH_DATA_SIZE - 1;
  66. theGraphData.g_cur_pos = 1;
  67. theGraphData.g_offset = graph_offset;
  68. theGraphData.ADC2_y_val.fill(0);
  69. theGraphData.ADC2_Count.fill(0);
  70. theGraphData.ADC2_y_val.begin();
  71. theGraphData.ADC2_Count.begin();
  72. theGraphData.g_cur_pos = 2;
  73. }
  74. f_delta_g = f_delta/graph_count - graph_offset;
  75. if (f_delta_g < 0){
  76. graph_offset -= N_P_GRAPH_DATA_SIZE;
  77. f_delta_g = graph_offset;
  78. theGraphData.g_cur_pos = 1;
  79. theGraphData.g_offset = graph_offset;
  80. theGraphData.ADC2_y_val.fill(0);
  81. theGraphData.ADC2_Count.fill(0);
  82. theGraphData.ADC2_y_val.begin();
  83. theGraphData.ADC2_Count.begin();
  84. theGraphData.g_cur_pos = 2;
  85. }
  86. if (theGraphData.g_cur_pos == 3){
  87. f_delta_g = 0;
  88. graph_offset = 0;
  89. theGraphData.g_cur_pos = 1;
  90. theGraphData.ADC2_y_val.fill(0);
  91. theGraphData.ADC2_Count.fill(0);
  92. theGraphData.ADC2_y_val.begin();
  93. theGraphData.ADC2_Count.begin();
  94. theGraphData.g_cur_pos = 2;
  95. }
  96. if (f_delta_old != f_delta_g){
  97. if (f_delta_g < 0){
  98. f_delta_g = 0;
  99. }
  100. theGraphData.ADC2_y_val[f_delta_g] = ADC2_y;
  101. theGraphData.ADC2_Count[f_delta_g] = f_delta_g;
  102. theGraphData.f_delta_g = f_delta_g;
  103. theGraphData.f_curr = ADC2_y;
  104. f_delta_old = f_delta_g;
  105. }
  106. m_pos_g = m_position/100;
  107. if (m_pos_g != m_pos_old){
  108. m_pos_old = m_pos_g;
  109. if (m_pos_g < 0){
  110. m_pos_g = 0;
  111. }
  112. if (m_pos_g > N_M_GRAPH_DATA_SIZE){
  113. m_pos_g = N_M_GRAPH_DATA_SIZE;
  114. }
  115. theGraphData.ADC1_Count[m_pos_g] = m_pos_g;
  116. theGraphData.ADC1_y_val[m_pos_g] = ADC1_y/10;
  117. theGraphData.m_position_g = m_pos_g;
  118. }
  119. }
  120. }
  121. }
  122. void Get_Uart::recv_command(QString command){
  123. this->serial->write(command.toUtf8());
  124. }
  125. Get_Uart::~Get_Uart(){
  126. out_data.close();
  127. }