I'm working on reading a temperature value from ADC channel 0.
Previously, I directly read it using KMotion and displayed it on the CNC screen by saving the variable as a DRO label. However, due to user requests, I now need to rewrite the code to read the value through Visual Studio, which will run in the background as the KMotionCNC app is loaded by the user, and update the temperature on the screen as well. I've read a few articles about "MainStatus" and the ".NET" namespace, but I'm still a bit confused about how to make this work.
I have a few questions:
1. I'm including the "Mainform.cs" file as a resource in my .sln file. In this case, do I still need to include header files like "Kmotiondef.h" and "PC-DSP.h"?
2. If I'm running this in Visual Studio, do I still save the variable to DRO labels to upload it onto the CNC UI, or do I use another function?
3. When creating the Visual Studio project, I'm currently selecting "Windows Forms App (.NET Framework)". Is this a good option? Also, does it matter if I run it in either C# or C++?
If there is any code snippet for similar projects that can be provided that would be great.
Thank you! Sorry for so many questions. I appreciate your help!
p.s. This is my old code that works fine:
Code: Select all
#include "KMotionDef.h"
#define TMP 10
#include "KflopToKMotionCNCFunctions.c"
// Function to read from ADC and convert to temperature
void readtemp() {
// Access the ADC channel for Kogna ADCs
int adc_value = Kogna_ADC_Buffer[0];
// Use the provided macro to convert ADC value to voltage
float voltage = KOGNA_CONVERT_ADC_TO_VOLTS(adc_value);
// Convert voltage reading to temperature
float temp_range = 100.0 - 0.0;
float voltage_range = 5 - 0.0;
float temp = ((voltage - 0.0) / voltage_range) * temp_range + 0.0;
char s[80];
sprintf(s,"%.2f °C\n", temp);
DROLabel(1000, 162, s);
// Print results
printf("The ADC reading is %d, which corresponds to a voltage of %.2f V.\n", adc_value, voltage);
printf("This voltage corresponds to a temperature of %.2f °C.\n", temp);
}
int main() {
double nextTime = Time_sec(); // Initialize nextTime to the current time
while (1) { // Infinite loop to continuously read temperature
nextTime += 2.0; //read every 2 sec
readtemp();
WaitUntil(nextTime);
}
return 0;
}