Typica is a free program for professional coffee roasters. https://typica.us
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

scale.cpp 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*1093:*/
  2. #line 135 "./scales.w"
  3. #include "scale.h"
  4. #include <QStringList>
  5. SerialScale::SerialScale(const QString&port):
  6. QextSerialPort(port,QextSerialPort::EventDriven)
  7. {
  8. connect(this,SIGNAL(readyRead()),this,SLOT(dataAvailable()));
  9. }
  10. /*:1093*//*1094:*/
  11. #line 153 "./scales.w"
  12. void SerialScale::dataAvailable()
  13. {
  14. responseBuffer.append(readAll());
  15. if(responseBuffer.contains("\x0D"))
  16. {
  17. if(responseBuffer.contains("!"))
  18. {
  19. responseBuffer.clear();
  20. }
  21. else
  22. {
  23. /*1095:*/
  24. #line 193 "./scales.w"
  25. QStringList responseParts= QString(responseBuffer.simplified()).split(' ');
  26. if(responseParts.size()> 2)
  27. {
  28. responseParts.removeFirst();
  29. responseParts.replace(0,QString("-%1").arg(responseParts[0]));
  30. }
  31. double weight= responseParts[0].toDouble();
  32. Units::Unit unit= Units::Unitless;
  33. if(responseParts[1].compare("lb",Qt::CaseInsensitive)==0)
  34. {
  35. unit= Units::Pound;
  36. }
  37. else if(responseParts[1].compare("kg",Qt::CaseInsensitive)==0)
  38. {
  39. unit= Units::Kilogram;
  40. }
  41. else if(responseParts[1].compare("g",Qt::CaseInsensitive)==0)
  42. {
  43. unit= Units::Gram;
  44. }
  45. else if(responseParts[1].compare("oz",Qt::CaseInsensitive)==0)
  46. {
  47. unit= Units::Ounce;
  48. }
  49. emit newMeasurement(weight,unit);
  50. /*:1095*/
  51. #line 165 "./scales.w"
  52. responseBuffer.clear();
  53. }
  54. }
  55. }
  56. /*:1094*//*1096:*/
  57. #line 224 "./scales.w"
  58. void SerialScale::tare()
  59. {
  60. write("!KT\x0D");
  61. }
  62. void SerialScale::weigh()
  63. {
  64. write(weighCommand+commandTerminator);
  65. }
  66. void SerialScale::setWeighCommand(const QString&command)
  67. {
  68. weighCommand= command.toAscii();
  69. }
  70. void SerialScale::setCommandTerminator(const QString&terminator)
  71. {
  72. if(terminator=="CRLF")
  73. {
  74. commandTerminator= "\x0D\x0A";
  75. }
  76. else if(terminator=="CR")
  77. {
  78. commandTerminator= "\x0D";
  79. }
  80. else if(terminator=="LF")
  81. {
  82. commandTerminator= "\x0A";
  83. }
  84. }
  85. /*:1096*/