Hi,
I am trying to control an external device (Eldex pump) via its RS232 interface using my Arduino Uno with an USB host shield. The USB host shield is connected to the RS232 of the external device via an USB Serial Adapter cable (with a PL2303 chip). Luckily, the Arduino USB host shield (https://www.arduino.cc/en/Main/ArduinoUSBHostShield) supports the PL-2303. The goal of my little Arduino project is that I woulkd like to control the pump from my Arduino by switching pump on/off and controlling pumb speed. The RS232 excepts simple control sequeces, consisting of two bytes (e.g. "RU", "SX",...), uses 9600 baud, no parity. Hamdshaking is required though!
Here is the problem: I have written a simple control program that sends a control sequence ("RU" : switch on pump) from the Arduino USB host shield to the RS232 of the device and is waiting for a feedback from the RS232 ("OK/") when powered on. I am able to send the control sequence to the device, no error message, however, the pump won't respond. Moreover, I won't get a feed back signal in response.
Is there anything to be considered regarding the Handshaking? My sample code is below. I would really really appreciate any comment for trouble shouting. How can I control an RS232 device with my USB host shield properly?
THANKS!!
/* USB Host to RS232 interface of Eldex pump using a PL-2303 converter */
// http://felis.github.io/USB_Host_Shield_2.0/class_a_c_m.html
// http://felis.github.io/USB_Host_Shield_2.0/cdcacm_8cpp_source.html
#include <usbhub.h>
/* CDC support */
#include <cdcacm.h>
#include <cdcprolific.h>
#ifdef dobogusinclude
#include <spi4teensy3.h>
#include <SPI.h>
#endif
class PLAsyncOper : public CDCAsyncOper {
public:
uint8_t OnInit(ACM *pacm);
};
uint8_t PLAsyncOper::OnInit(ACM *pacm) {
uint8_t rcode;
// Set DTR = 1
rcode = pacm->SetControlLineState(1);
if(rcode) {
ErrorMessage<uint8_t>(PSTR("SetControlLineState"), rcode);
return rcode;
}
Serial.println("Error code of OnInit = ");
Serial.println(rcode, HEX);
LINE_CODING lc;
lc.dwDTERate = 9600;
lc.bCharFormat = 0;
lc.bParityType = 0;
lc.bDataBits = 8;
rcode = pacm->SetLineCoding(&lc);
if(rcode)
ErrorMessage<uint8_t>(PSTR("SetLineCoding"), rcode);
return rcode;
}
USB Usb;
USBHub Hub(&Usb);
PLAsyncOper AsyncOper;
PL2303 Pl(&Usb, &AsyncOper);
uint32_t read_delay;
#define READ_DELAY 100
void setup() {
Serial.begin(9600);
Serial.println("Start");
if(Usb.Init() == -1)
Serial.println("OSCOKIRQ failed to assert");
delay(100);
}
void loop() {
uint8_t rcode;
uint8_t buf[64];
uint16_t rcvd = 64;
char strbuf[] = "RU";
uint8_t LengthOfBuffer;
Usb.Task();
if( Usb.getUsbTaskState() == USB_STATE_RUNNING ){
Serial.println(F("\n"));
Serial.println("USB is up and running!");
Serial.println(F("\n"));
}else{
Serial.println(F("\n"));
Serial.println("USB is not running!");
Serial.println(F("\n"));
}
if(Pl.isReady()) {
/////////////////////////////////////////////////////////
//
// write
//
/////////////////////////////////////////////////////////
Serial.print("Write buffer to USB port: ");
Serial.print(strbuf);
Serial.print(" and in integer: ");
Serial.print((uint8_t)strbuf[0]);
Serial.print(" ");
Serial.println((uint8_t)strbuf[1]);
LengthOfBuffer = strlen(strbuf);
Serial.print("Length of buffer [bytes]: ");
Serial.println(LengthOfBuffer);
rcode = Pl.SndData(LengthOfBuffer, (uint8_t*)strbuf);
Serial.print("Error code of buffer write = ");
Serial.println(rcode);
if (rcode)
ErrorMessage<uint8_t>(PSTR("SndData"), rcode);
delay(100);
/////////////////////////////////////////////////////////
//
// read
//
/////////////////////////////////////////////////////////
Serial.println("Read from USB port");
for (uint8_t i=0; i<64; i++)
buf[i] = 0;
rcode = Pl.RcvData(&rcvd, buf);
Serial.print("Error code of buffer read = ");
Serial.println(rcode);
if (rcode && rcode != hrNAK)
ErrorMessage<uint8_t>(PSTR("Ret"), rcode);
Serial.println((char*)(buf));
delay(100);
//////////////////////////////////////////////////////
//
// another way of reading
//
//////////////////////////////////////////////////////
Serial.println("Read from USB port again");
if((long)(millis() - read_delay) >= 0L) {
read_delay += READ_DELAY;
rcode = Pl.RcvData(&rcvd, buf);
if(rcode && rcode != hrNAK)
ErrorMessage<uint8_t>(PSTR("Ret"), rcode);
if(rcvd) { //more than zero bytes received
for(uint16_t i = 0; i < rcvd; i++) {
Serial.println("Bytes received:");
Serial.print((char)buf[i]); //printing on the screen
}
}
}
}
}