I have a PHD ULTRA syringe pump from Harvard Apparatus. I want to establish communication between an Arduino and the syringe pump. The company provided me with a Python code, but Arduino does not support Python. Therefore, I converted this code to C++ to make it compatible with the Arduino. I am using an RS-232 to TTL converter between the Arduino and the syringe pump.
However, when I attempted to use this code to establish communication, I encountered issues. Could someone please help me amend this code if there are any errors? I have attached a diagram of my setup and will also attach the Python code provided by the company.
Note: The syringe pump successfully responds and turns ON and OFF when using the pump terminal software provided by the company.
The C++ Code is:
However, when I attempted to use this code to establish communication, I encountered issues. Could someone please help me amend this code if there are any errors? I have attached a diagram of my setup and will also attach the Python code provided by the company.
Note: The syringe pump successfully responds and turns ON and OFF when using the pump terminal software provided by the company.
The C++ Code is:
Arduino Code:
#include <SoftwareSerial.h>
// Define the pins for RX and TX
#define RX_PIN 2
#define TX_PIN 3
SoftwareSerial pumpSerial(RX_PIN, TX_PIN);
void setup() {
// Initialize hardware serial for debugging
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect
}
// Initialize software serial for pump communication
pumpSerial.begin(9600);
Serial.println("Arduino ready to communicate with syringe pump.");
}
void loop() {
// Command to send
String command = "run\r\n";
// Send the command
Serial.print("Sending command: ");
Serial.print(command);
pumpSerial.print(command);
// Wait for 1 second
delay(1000);
// Read and print the response
String response = "";
while (pumpSerial.available() > 0) {
char inChar = (char)pumpSerial.read();
response += inChar;
}
// Print the response if not empty
if (response != "") {
Serial.print("Response received: ");
Serial.println(response);
} else {
Serial.println("No response received.");
}
// Wait before sending the next command (adjust as needed)
delay(5000);
}
The diagram of the setup is:
[ATTACH type="full"]327932[/ATTACH]
The Python code that the company provide is:
[CODE lang="python" title="Python Code"]
# Assuming that PySerial is installed:
import serial
import time
# Configure serial connection
ser = serial.Serial(
port='COM1',
baudrate=9600,
parity=serial.PARITY_ODD,
stopbits=serial.STOPBITS_TWO,
bytesize=serial.SEVENBITS
)
ser.isOpen()
input = 'run'
out = ''
# Send the input to the device
# Note the carriage return and line feed characters \r\n will depend on the device
ser.write(input + '\r\n')
# Wait 1 sec before reading output
time.sleep(1)
while ser.inWaiting() > 0:
out += ser.read(1)
# Print the response
if out != '':
print out
Attachments
-
452.5 KB Views: 6
