08-15-2006 04:12 PM
08-16-2006 07:28 AM
At the end of this link is the code to do byte swapping for 16 bit values [htons()] and 32 bit values [htonl()]. These are standard definitions used to handle the byte swapping requirements of a network interface, but they will also work any place you need to deal with "endianness".
http://www.netrino.com/Publications/Glossary/Endianness.html
08-16-2006 09:08 AM
Hallo mvr.
Thank you for the link, I will do the data side now and when it is running will report my progress.
Thank you very much for you valueable knowledge,
Best regards Peter.
08-16-2006 03:04 PM
Hallo,
I have changed the data format send to te serial port to 8 bits and reading an AD/value from a potensiometer and then send the data as Hex 0 to FF (0 -255),I can see the data in the array buffer of labwindows and in the Microcontroller debugger windows.In the controller debugger i see consistant numers and logic,in labwindows I see in 1 element the correct value and in the next element a minus number,If i set the potensiometer to read say decimal 150 the 1 element reads 150 and the next read - 50,I am not sure but could this be a syncronization problem as the controller send continuosly and when I activate the ok button the port reads ,How do I synchronize the port read and the controller sending to each other. Is it possible also in labwindows to do a continious read by pressing a button once and then only stop when the button is pressed again,
I apologise for asking so much help ,I really like the looks of labwindows and would like to work with it however by reading the referance manuals and user manual for a beginner is quit difficult to understand,I also apologise for my english I am a German speaker.
Best regards
Peter
08-17-2006 02:59 AM
This sounds like it is being caused by using char as the data type for your buffer, which by default in CVI is signed. So any char value "above" 127 will actually be regarded as negative. Try changing the definition of your buffer to unsigned char instead.
JR
08-17-2006 11:13 AM - edited 08-17-2006 11:13 AM
Message Edited by mvr on 08-17-2006 11:17 AM
08-17-2006 04:54 PM
Hallo mvr ,JR![]()
Thank you for the advice:
Mvr ,
I will preload the buffer in the controller with the data you sugested and send that to CVI ,this is easly done in the microcontroller and then varify the reception in CVI,I also confirmed with microchip today that all their USARTs send little endian as you suspected in an earlier post,therefore using you pointer suggestion in totally valid,and easier for me to start with. I will try all first and work with the data to get myself more clear and then inform about my progress.
Thank you once again for the help .
I wish you all a nice weekend.
Peter
08-19-2006 03:31 PM
Hallo mvr,
Thank you very much my Serial port is running now I included the timer and poll every 0.5 sec for data at the serial port,I am sending chars and they are correct and my controls show the correct data aswell.However by defining the buffer as unsigned char ,the compiler give a warning that an char is expected, by looking at the function prototype for the ComRd() it shows char buff, you can see it as I posted the code and warning below.Now I will start to work on the intergers in buffer and use the integers as the data values ,
I am so happy thank you for your help with out it i would still be in the dark.
#include <cvirte.h>
#include <userint.h>
#include "USART.h"
//*****************************************************************************************************************
static int panelHandle;
int main (int argc, char *argv[])
{
if (InitCVIRTE (0, argv, 0) == 0)
return -1; /* out of memory */
if ((panelHandle = LoadPanel (0, "USART.uir", PANEL)) < 0)
return -1;
DisplayPanel (panelHandle);
OpenComConfig (4, "COM4", 9600, 0, 8, 1, 512, 512);
RunUserInterface ();
DiscardPanel (panelHandle);
return 0;
}
//******************************************************************************************************************
int CVICALLBACK ReadCallback (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
SetCtrlAttribute (panelHandle, PANEL_TIMER, ATTR_ENABLED, 1);
break;
}
return 0;
}
//---------------------------------------------------------------------------------------------------------------------
int CVICALLBACK QuitCallback (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
CloseCom (4);
QuitUserInterface (0);
break;
}
return 0;
}
//______________________________________________________________________________________________________________________
int CVICALLBACK EndreadCallBack (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
unsigned char Battery_V;
switch (event)
{
case EVENT_COMMIT:
Battery_V = 0;
SetCtrlVal (panelHandle, PANEL_Battery, Battery_V);
SetCtrlAttribute (panelHandle, PANEL_TIMER, ATTR_ENABLED, 0);
break;
}
return 0;
}
//_____________________________________________________________________________________________________________________
int CVICALLBACK TimerCallback (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
int n;
unsigned char Battery_V;
unsigned char Rev;
unsigned char Data[10];
switch (event)
{
case EVENT_TIMER_TICK:
FlushInQ (4);
n = ComRd (4, Data, 10);
Rev = (Data[4] * 30)/1000;
Battery_V = (Data[5]*0.0196);
SetCtrlVal (panelHandle, PANEL_NUMERICGAUGE, Rev);
SetCtrlVal (panelHandle, PANEL_Battery, Battery_V);
if (Rev > 4){
DeleteGraphPlot (panelHandle, PANEL_GRAPH, -1, VAL_IMMEDIATE_DRAW);
PlotY (panelHandle, PANEL_GRAPH, Data, 10, VAL_UNSIGNED_CHAR,
VAL_THIN_LINE, VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_RED);
}
break;
}
return 0;
}
//**********************************************************************************************************************
08-21-2006 08:27 AM
Looks like you are well on your way. Getting the data is usually the most frustrating part.
A couple of things to think about:
By "casting" your buffer pointer to a signed char you will eliminate the compiler complaint. n = ComRd (4, (char *)Data, 10); This does not change anything about how the data is handled, it simply tells the compiler to accept the Data array pointer as a signed char pointer. Depending on how your data is used, another solution may be to redefine your Data buffer as a signed char. I dont see a problem in mixing signed and unsigned values for your specific application at this point.
Your timer callback looks good. Here is something to consider as you expand your use of them. Timer callbacks are like hardware interrupts, you want to minimize execution time of the callback as much as possible. The SetCtrlVal() function redraws the screen for each call, which is inefficient when using multiple calls to update controls. An easy way to avoid this is to use SetCtrlAttribute().
SetCtrlVal (panelHandle, PANEL_NUMERICGAUGE, Rev); can be replaced by
SetCtrlAttrbute(panelHandle, PANEL_NUMERICGAUGE, ATTR_CTRL_VAL, Rev);
This updates the control value, but delays the draw event.
Update all your controls this way and at the end of the callback insert a ProcessDrawEvents() call and this will update the display in a single operation without the overhead of multiple draw operations.
08-21-2006 02:30 PM
Hallo mvr,
Thank you very much it is clear to me about the differences between SetCtrlVal(); and SetCtrlAttribute after your explenation, I have so much to learn ,I will change my code and also now I am working on pointers and arrays in in my C study book I am really uncomfortable with the pointers but will give it a good go,
Wish you nice week ,
Peter