Fieroguy19:
So I have a mega2560 connected with a pump that communicated via serial, so I am using Serial for computer and Serial1 for pump. The "PIN_STOP" will trigger the pump to stop and then the "Serial1.println("/1?29");" tells the pump to send back the position of the pump. I am using the MultiSerialMega code as well to see what is going on via Serial Monitor.
This is what is expected to be returned, where 4522 is the value that I am looking for.
/
0
@
4
5
2
2
y
With the current sketch this is what I am actually getting using MultiSerialMega. The DEC value for "/" is 47 so I am guessing that is where that came from. Without the MultiSerialMega code I end up getting "47" for pump_return.
47
0
@
4
5
2
2
y
if (fluid == true){
if (frequency >= 6000){ //Number will change after calibration
Serial.println("stop");
digitalWrite(PIN_STOP, LOW); // Stop syringe
analogWrite(BLUE_LED, 0); //Turn off LED
digitalWrite(SENSOR_ON, LOW); //Turn sensor off
fluid=false;
lcd.clear();
selected = false; //Go to main menu
Serial1.println("/1?29"); // Querry syringe position
if (Serial1.available()){
pump_return = Serial1.read();
Serial.println(pump_return);
What I really need is a way to receive the 4522 value and set it to "pump_return" to be used for other purposes. I am guessing I need to receive some type of string? Maybe use a modified version of this http://arduino.cc/en/Tutorial/SerialEvent
You don't really need to make a string and use a function to turn that into a number.
serial inputs 4-5-2-2
start off with a variable to hold your number and another to hold the serial entry
int value = 0;
char serIn;
1st I get '4' (ASCII 52) into serIn and have already checked it is >= '0' and <= '9'
// serIn == '4'
value = value * 10 + serIn - '0'; // value == 4
// serIn == '5'
value = value * 10 + serIn - '0'; // value == 45
// serIn == '2'
value = value * 10 + serIn - '0'; // value == 452
// serIn == '2'
value = value * 10 + serIn - '0'; // value == 4522
We could pack the whole thing into a loop that waits for each character to arrive but we already have loop() going and may want to do other things at the same time.
There is a method called Finite State Machine where you use a variable to keep track of the process going on and use that to determine which code to respond to changes, in this case the serial data.
Using the pump code doc, the initial state would look for '/' and reject all else. Once it finds '/' the state changes. You write a separate section of state code for each operation you need along the way. The states do not have to be 0-1-2-3-... they can be 10-20-21-22-30-etc so that printing out the state value at the start of loop() can tell you at a glance what your process is doing.
For serial processing, put your state machine inside a switch-case block inside an if block:
if ( Serial.available())
{
switch ( state )
{
case 0 : // looking for message start
{
if ( serIn == '/' ) state = 10;
}
break;
case 10 : // here we go
{
// more code
}
break;
// more cases
default : // all errors go here
{
// print or whatever your error indications
state = 0;
}
}
}
All that fits in loop() and only runs when a serial character arrives.
You can have different processes with different state variables "running at the same time" if your MCU is doing more than just communication with the pump. One thing that I do is have led 13 blink 1 second on, 1 second off as a yup it's still running indicator.
Dealing with the pump codes is moderately difficult. Just be happy it's logical. But do not expect that serial data will all always arrive uncorrupted. Maybe it will but if your code does not deal with errors then every error will be a bad one.
Byte# Description ASCII Hex
1 Start Character / 2F
2 Controller Address 0 30
3 Status Byte @ or '
4 to N Response
N+1 End of Text 03
N+2 Carriage Return 0D
N+3 Line Feed 0A
N+4 End (Blank) FF
Explanation of bytes:
Byte 1: The starting character, 2F hex, which signals the beginning of a new
packet, is the front slash character "/" on a computer keyboard,.
Byte 2: The host address, 30 hex (ASCII 0), is the address number for the
host computer.
Byte 3: The status and error byte describes the device status. This can be either
@ for busy or ' for not busy.
Byte 4: There may or may not be response byte(s) for a command. In
general, all query commands, "read an input value" commands, and
configuration query commands (~A, ~B, ~P, ~V, etc) cause response
bytes. Other commands do not cause a response. This is where the value 4522 is.
Byte 4+n: The end-of-response mark is 03 hex.
Byte 4+n+1: Carriage return is 0D hex.
Byte 4+n+2: End of packet character is the line feed character, 0A hex.
Byte 4+n+3: The extra ending character, FF hex, is an extra character to ensure
the packet is properly sent. This character might not be displayed by
the host terminal.