LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Arduino not recognizing RS232 messages in the serial buffer

Solved!
Go to solution

I'm working on a CVI application to control an Arduino. To test the communication between them, I built a simple CVI app that sends a non-empty string on the push of a button. When the Arduino recognizes a non-empty string in the serial buffer, it flips a relay. Using the Serial Monitor, I've validated that the Arduino responds to non-empty strings properly. However, it isn't responding to messages from the CVI app. Any idea what might be wrong? 

 

CVI button callback:

 

int CVICALLBACK start (int panel, int control, int event,
					   void *callbackData, int eventData1, int eventData2)
{
	switch (event)
	{
		case EVENT_COMMIT:
			OpenComConfig (4, "", 9600, 0, 8, 1, 512, 512);
			char buf[100];
			Fmt(buf,"%s","<010010000>");
			if (ComWrt (4, buf, 13) != 13)
			{
				FmtOut("failed\n");
			}
			else
			{
				FmtOut("sent\n");
			}
			CloseCom (4);
			break;
		case EVENT_RIGHT_CLICK:

			break;
	}
	return 0;
}

 

Arduino code:

char receivedChar;
boolean newData = false;
const int Relay[48] = {49, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
   30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48}; // define pins

int relayVals[48] = {};

const int triggerType = LOW; // your relay type
int loopDelay = 1; // delay in loop

void setup() {
  for(int i=0; i<48; i++)
  {
    pinMode(Relay[i], OUTPUT); // set pin as output
    if (triggerType == LOW) {
      digitalWrite(Relay[i], HIGH); // set initial state OFF for low trigger relay
    }
    else {
      digitalWrite(Relay[i], LOW); // set initial state OFF for high trigger relay
    }
  }
    Serial.begin(9600);
    Serial.println("<Arduino is ready>");
}

void loop() {
    recvOneChar();
    showNewData();
}

void recvOneChar() {
    if (Serial.available() > 0) {
        receivedChar = Serial.read();
        newData = true;
    }
}

void showNewData() {
    if (newData == true) {
      channelControl(32, 1, 10);
      channelControl(32, 0, 10);
        newData = false;
    }
}

 void channelControl(int relayChannel, int action, int t)
 {
  int state = HIGH;
  String statTXT = " OFF";
  if(triggerType == LOW)
  {
    if (action) // if ON requested
    {
      state = LOW;
      statTXT = " ON";
    }
    digitalWrite(Relay[relayChannel], state);
    if (t>0)
    {
      delay(t);
    }
    Serial.print("Relay: ");
    Serial.print(relayChannel);
    Serial.print(statTXT);
    Serial.print(" - ");
    Serial.println(t);
  }
 }

 

0 Kudos
Message 1 of 4
(1,645 Views)

You state to write 13 characters to the port but the buffer is 11 chars long. I do not know whether in this case the buffer is actually sent to the port: have you tested this?



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 4
(1,615 Views)
Solution
Accepted by topic author csulliva

Additionally, I would leave some time for the system to actually write to the port before closing it.

 

As a side note, as you may not know (from the function help, highlight is mine):

outputQueueSize int The size of the output queue for the selected port.

If you specify 0, OpenComConfig uses 512. If you specify a value greater than 0 but less than 30, OpenComConfig uses 30. If you specify a negative value for outputQueueSize, the output queue is not used and the data is written to the port directly. There is no maximum limitation on the queue size. However, some serial drivers have a maximum of 32,767 and give undefined behavior when you use a larger queue size. NI recommends that you use a queue size no greater than 32,767.

 

 



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 3 of 4
(1,597 Views)

Giving it more time worked, thanks.

0 Kudos
Message 4 of 4
(1,576 Views)