06-24-2021 10:34 AM
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);
}
}
Solved! Go to Solution.
06-24-2021 03:29 PM - edited 06-24-2021 03:34 PM
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?
06-25-2021 01:55 AM
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. |
06-28-2021 11:05 AM
Giving it more time worked, thanks.