Skip to content

Instantly share code, notes, and snippets.

@ouoam
Last active October 27, 2020 06:06
Show Gist options
  • Select an option

  • Save ouoam/fce336da12d53a962e481d91b95cbef0 to your computer and use it in GitHub Desktop.

Select an option

Save ouoam/fce336da12d53a962e481d91b95cbef0 to your computer and use it in GitHub Desktop.
Read MTM-380SD RS-232 data output
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
void setup() {
Serial.begin(115200);
mySerial.begin(9600);
}
uint8_t buffs[3][16];
void loop() { // run over and over
if (mySerial.available() && mySerial.read() == 0x02) {
while (!mySerial.available());
if (mySerial.read() != '4') return;
char buff[16];
for (int j = 13; j >= 0; j--) {
while (!mySerial.available());
buff[j] = mySerial.read();
}
for (int j = 13; j >= 0; j--) {
buffs[buff[13] - '0' - 1][j] = buff[j];
}
}
if (Serial.available()) {
Serial.read();
for (int i = 0; i < 3; i++) {
double val = 0;
for (int j = 8; j >= 1; j--) {
val *= 10;
val += buffs[i][j] - '0';
}
if (buffs[i][10] == '1') { // Polarity
val = -val;
}
for (int i = 0; i < buffs[i][9]- '0'; i++) { // Decimal Point
val /= 10;
}
Serial.print(val, buffs[i][9]- '0');
Serial.print(",");
}
Serial.println();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment