LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

How to add specific data that can be sent to a board through a COM Port

Solved!
Go to solution

Hello everyone, I am new to the CVI program. I am currently learning how to create a panel und send data from the panel through a COM Port to a microcontorller board.

I have already programmed these commands on another microcontroller software:

START:READ_TAG:END

START:GET_UID:END

START:WRITE_B_xxxx:END

START:READ_BLOCK_:END

START:READ_MULTI_:END

START:SET_PRO_PAGE_X_X:END

START:PW_INPUT_xxxx:END

For the first 2 commands I already got an example code (see the example code below).

Now for the other commands, I am looking for a method or tutorial how to write specific characters/numbers, that I can send to the COM PORT.

For example:

 

START:WRITE_B_xxxx:END -> Choose a number for B (Block) and write 4 characters in the Block.

START:READ_BLOCK_:END -> Choose a Block number after BLOCK_ and show the written data inside.

START:READ_MULTI_:END  -> Choose many Blocks (from the first block until a specific block)

START:SET_PRO_PAGE_X_Y:END -> the board has a specific number of pages X, and 4 types of protection that I can choose.

START:PW_INPUT_xxxx:END -> Change the password that the board contains by adding 4 new characters

 

How can I apply this with CVI by adding data into a Panel? I would be so helpful for any tips or help.

Thank you so much in advance.

 

See the CVI example Code for the first commands below:

 

#include <cviauto.h>
#include <formatio.h>
#include <rs232.h>
#include "asynctmr.h"
#include <userint.h>
#include <utility.h>
#include "DEFINITIONS.h"
#include "UIR_GLAS_TAG.h"


int main (int argc, char* argv[])
{
int thereIsAnother = -1;
int iResult = -1;

CA_InitActiveXThreadStyleForCurrentThread(0,COINIT_APARTMENTTHREADED);

if (InitCVIRTE(0,argv,0) == 0)
return -1; // out of memory

// Überprüfen ob Anwendung bereits geöffnet ist um doppeltes öffnen zu vermeiden
if (CheckForDuplicateAppInstance (ACTIVATE_OTHER_INSTANCE, &thereIsAnother) < 0)
return -1;

if (thereIsAnother)
return 0;

iResult = OpenComConfig (5, "COM5", 9600, 0, 8, 1, 512, 512);

iTimer_Handle = NewAsyncTimer (0.9, -1, 1, CALLBACK_MAIN_Find_Glastag, 0);

sPanels.iHauptmenue = LoadPanel (0, "UIR_GLAS_TAG.uir", P_MAIN);

DisplayPanel(sPanels.iHauptmenue);

RunUserInterface();

return 0;
}

 

// ************************************************************************************************************************
int CVICALLBACK CALLBACK_MAIN_Find_Glastag (int reserved, int timerId, int event, void *callbackData, int eventData1, int eventData2)
{
char cAnswer[100] = {0};
char cUID[20] = {0};
int iPos = -1;
int iDA = -1;
int iNICHT_DA = -1;

GetCtrlAttribute(sPanels.iHauptmenue, P_MAIN_DA, ATTR_CTRL_VAL, &iDA);
GetCtrlAttribute(sPanels.iHauptmenue, P_MAIN_NICHT_DA, ATTR_CTRL_VAL, &iNICHT_DA);

ComWrt(5, "START:READ_TAG:ENDE", 19);

Sleep(200);

ComRdTerm (5, cAnswer, 99, 10);

if(FindPattern(cAnswer,0,-1,"START:AW_",1,0) >= 0)
{
iPos = FindPattern (cAnswer, 0, -1, "_1", 0, 0);
if(iPos >= 0)
{
SetCtrlAttribute(sPanels.iHauptmenue, P_MAIN_L_GLASTAG_VORHANDEN, ATTR_CTRL_VAL, TRUE);
iDA++;
}
else
{
SetCtrlAttribute(sPanels.iHauptmenue, P_MAIN_L_GLASTAG_VORHANDEN, ATTR_CTRL_VAL, FALSE);
SetCtrlAttribute(sPanels.iHauptmenue, P_MAIN_UID, ATTR_CTRL_VAL, "0000000000000000");
iNICHT_DA++;
}
}

SetCtrlAttribute(sPanels.iHauptmenue, P_MAIN_DA, ATTR_CTRL_VAL, iDA);
SetCtrlAttribute(sPanels.iHauptmenue, P_MAIN_NICHT_DA, ATTR_CTRL_VAL, iNICHT_DA);

FlushInQ(5);
FlushOutQ(5);
memset(cAnswer,0,sizeof(cAnswer));

if(iPos >= 0)
{
ComWrt(5, "START:GET_UID:ENDE", 19);

Sleep(200);

ComRdTerm (5, cAnswer, 99, 10);

if(FindPattern(cAnswer,0,-1,"START:AW_",1,0) >= 0)
{
iPos = FindPattern (cAnswer, 0, -1, "_1", 0, 0);
if(iPos >= 0)
{
CopyBytes (cUID, 0, cAnswer, 13, 15);
SetCtrlAttribute(sPanels.iHauptmenue, P_MAIN_UID, ATTR_CTRL_VAL, cUID);
}
else
{
SetCtrlAttribute(sPanels.iHauptmenue, P_MAIN_UID, ATTR_CTRL_VAL, "0000000000000000");
}
}
FlushInQ(5);
FlushOutQ(5);
memset(cAnswer,0,sizeof(cAnswer));
memset(cUID,0,sizeof(cUID));
}



return 0;
}


// ************************************************************************************************************************
int CVICALLBACK CALLBACK_MAIN_Panel (int panel, int control, int event, void *callbackData, int eventData1, int eventData2)
{
int iLED_AN_AUS = -1;

if(event == EVENT_COMMIT)
{
switch(control)
{
case P_MAIN_B_OK: GetCtrlAttribute(sPanels.iHauptmenue, P_MAIN_L_BEISPIEL, ATTR_CTRL_VAL, &iLED_AN_AUS);
if(iLED_AN_AUS == 0)
SetCtrlAttribute(sPanels.iHauptmenue, P_MAIN_L_BEISPIEL, ATTR_CTRL_VAL, TRUE);
else
SetCtrlAttribute(sPanels.iHauptmenue, P_MAIN_L_BEISPIEL, ATTR_CTRL_VAL, FALSE);

break;

case P_MAIN_B_EXIT: QuitUserInterface(0); break;
}
}
return 0;
}

0 Kudos
Message 1 of 2
(750 Views)
Solution
Accepted by Ramles97

Well this is more a C question, since it seems you already have planned how to move on the CVI side.

 

I suppose you are using an ASCII protocol, i.e. only printable characters are used. If this is true then you can format  the command for your device with appropriate sprintf () instructions: any good C reference manual will help you in formatting the string.

 

Just as an example (supposing the number must be a fixed-lenght string padded with zeros):

GetCtrlVal (panelHandle, cotrolID,&block);
sprintf (commandString, "START:WRITE_B_%04d:END",blok);
ComWrt (port, commandString, strlen (commandString));

 

Your string may need to be formatted other way: the device documentation must specify exactly what the microcontroller is expecting.



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?
Message 2 of 2
(708 Views)