Hello All,
I am trying to interface with an Alicat Mass Flow Controller using an Arduino Mega Serial port. The mass flow controller serial port appears to send out TTL voltage levels (0-5V, I checked via my scope) so I just connected its serial pins to the Arduino serial port directly. I am able to read something from the MFC, however, the characters are garbled. I am able to read the data just fine via a PC serial port, however. I made sure that the Baud rates are matched between the devices. I tried to switch baud rates, but no luck.
Via PC I am getting repeated streams that look like this:
A +014.66 +028.07 +0000.0 +0000.0 0000.0 Air
On the arduino I am reading (note I read 64 bytes regardless and the MFC constantly sends updated data so I do not know when the frame begins)
¿¿¿}- yjÖv6¿©£¿©£¿©£¿£¿¿¿¿¿}- yjÖv6¿©}
Any thoughts would be appreciated!
Here is my code for the MFC object I am using to communicate:
#ifndef AlicatMFC_h
#define AlicatMFC_h
#include "Arduino.h"
#define ALI_MULTIPLIER 64000
#define SERIAL_TIMEOUT 500
class AlicatMFC
{
public:
AlicatMFC();
void initSerial();
void sendCommand(char* in, char* out);
private:
};
#endif
#include "Arduino.h"
#include "AlicatMFC.h"
AlicatMFC::AlicatMFC()
{
// NOTES: No line ends with line feeds
// There will be two devices A and B
// To address a given device type: A $$XX or B $$XX where XX is a command
// NOTE: Units should be first set to polled mode (not streaming), this must be done for each unit separately by sending: *@=A and *@=B respectively
}
void AlicatMFC::initSerial()
{
Serial3.begin(9600); // Baud for Alicat is 19200 default
}
void AlicatMFC::sendCommand(char* in, char* out)
{
int i = 0;
for (i=0;i<64;i++)
out[i] = '\0';
unsigned long t;
// send the message to MFC
Serial3.println(in);
// read response (if any)
i = 0;
t = millis();
// read until carriage return or serial timeout
do {
while (Serial3.available()==0)
if ((millis()-t) > SERIAL_TIMEOUT) { out[0] = 'R'; return; }
out[i++] = Serial3.read();
} while ((out[i-1]!=13) & (i<64));
}