02-02-2022 12:55 PM - edited 02-02-2022 01:36 PM
#include <ansi_c.h>
#include <rs232.h>
#include <formatio.h>
#include <utility.h>
#include <cvirte.h>
Here is my code in C, the equipment is a RH-USB Temp Sensor from Omega. The cvi program builds and runs but reading from sensor is always zero. If I use a HyperTerminal same com port 1 it reads correctly, by sending the same syntax F. The program is supposed to communicate with 4 different RH-USB's, one for temp, humidity etc. But for now just need one of the sensors to read. Please help me out, new to C and starting a class in 101.
1. I tried using different com ports.
2. The program works using HyperTerminal.
I think the my syntax on the "H" = humidity command is not being sent correctly
Thank you
#include <userint.h>
//#define COM1 1
//#define COM2 2
//#define COM3 3
#define COM4 4
void DRV_RH_USB (void);
struct
{
float Temp_Ambient;
// float Temp_InLetWater;
// float Temp_OutLetWater;
float Humidity;
} Measure;
/*****************************************************************************[]*/
// Main for stand alone exexcution
main ()
{
DRV_RH_USB ();
}
/*****************************************************************************[]*/
// Read temperature and humidity from RH_USB sensors and assign to variables.
// Also display measurement on STD-IO
//
void DRV_RH_USB (void)
{
char buffer[80] = "";
char command[80] = "";
printf ("%s","RH-USB Temperature Measurement");
// Configure COM ports
//OpenComConfig (COM1, "COM1", 9600, 0, 8, 1, 0, 0); // 0=512 for last two parameters
//OpenComConfig (COM2, "COM2", 9600, 0, 8, 1, 0, 0); // 0=512 for last two parameters
//OpenComConfig (COM3, "COM3", 9600, 0, 8, 1, 0, 0); // 0=512 for last two parameters
OpenComConfig (COM4, "COM4", 9600, 0, 8, 1, 0, 0); // 0=512 for last two parameters
// Open COM ports
//OpenCom(COM1, "COM1"); // Ambient Temperature
//OpenCom(COM2, "COM2"); // Inlet Water
//OpenCom(COM3, "COM3"); // Outlet Water
OpenCom(COM4, "COM4"); // Humidity
// Send command to read temperatures & humidity
Fmt ("H\n","%s", "C\n");
// Ambient
// ComWrt(COM1,command,80);
//Delay (0.5);
//ComRd (COM1, buffer, 80);
//printf(buffer);
// Delay (0.5);
// Measure.Temp_Ambient = atof (buffer);
// printf ("\n%s%f","Ambient Temperature = ", Measure. Temp_Ambient);
// Delay (5);
// Inlet water
// ComWrt (COM2, command, 80);
// Delay (0.5);
// ComRd (COM2, buffer, 80);
// Measure.Temp_InLetWater = atof (buffer);
// printf ("\n%s%f","Inlet Water Temperature = ", Measure.Temp_Ambient);
// Outlet Water
// ComWrt (COM3, command, 80);
// Delay (0.5);
//ComRd (COM3, buffer, 80);
//Measure.Temp_OutLetWater = atof (buffer);
//printf ("\n%s%f","Outlet Water Temperature = ", Measure.Temp_Ambient);
//Humidity
ComWrt (COM4,0x02, 80);
Delay (0.5);
ComRd (COM4, buffer, 80);
Measure.Humidity = atof (buffer);
printf ("\n%s%f","Humidity Temperature = ", Measure.Temp_Ambient);
Delay (5);
}
//******************************************************************* EOF : RH_USB
02-03-2022 12:50 AM
Hi,
as a first step I would suggest adding and checking return values for all the function calls, e.g.,
returnValue=OpenComConfig(...)
It doesn't harm and may help to locate the problem. RS232 error codes can be found here
02-04-2022 03:29 AM - edited 02-04-2022 03:53 AM
Hello original-iceman,
the code you posted is really nothing that can be looked at, I seriously doubt that you can even compile it!
First of all, after calling OpenComConfig you cannot call OpenCom on the same port! Trapping the error codes as Wolfgang suggested you would have got this error.
Other hints: Fmt syntax is wrong, the function expects a variable in its first parameter, ComWrt syntax is wrong, a buffer is expected in its second parameter. Finally, you get the temperature out from the buffer and print the humidity!
Please go and revise your code, read the function help if you don't know how to use them and retry with a working code: you'll probably get the results you expect.
Last but not least: in the future try not to post a source with 95% of the code commented out: it will be easier for the readers to have only the relevant code to look at and possibly comment to try helping you.
05-26-2022 04:20 PM - edited 05-26-2022 05:11 PM
Still struggling with this program, I am new to C, I can program in LabVIEW, but C is giving me headaches. So if you can just fix it for me that would be great. I learn much better this way. Just need to get this program working and then I start trying other programs
I changed the following two commands to the following:
Note: I am only using Com1 for now.
Fmt (Buffer, "C\n");
ComWrt (COM1, buffer, 80);
This is the output I get:
RH-USB Temperature Measurement
Ambient Temperature = 0.000000
05-27-2022 02:06 AM - edited 05-27-2022 02:30 AM
Did you read the responses? Wolfgang told you to check each and every return value of those functions. Once any of those functions has returned an error it makes usually no sense to continue and you should abort the execution and print an according error message with the code somewhere. This will tell you if the OpenComDonfig() succeeded. If it didn't you can keep doing ComRd() and ComWr() until the universe dies, but you never will receive any response
And once you do proper error handling you not only know which function starts to fail but also get an error code that helps you pinpoint the possible causes of the error quite a bit from "It doesn't work!" to "The port isn't recognized" or "The device did not respond in timely manner".
void DRV_RH_USB (void)
{
char buffer[80] = "";
char command[80] = "";
int retval;
printf ("RH-USB Temperature Measurement");
// Configure COM ports
retval = OpenComConfig(COM1, "COM1", 9600, 0, 8, 1, 0, 0); // 0=512 for last two parameters
if (retval < 0)
{
printf("OpenComConfig returned error %d", retval);
return;
}
// Send command to read temperatures & humidity
Fmt(command, "C\n"); // First parameter must be the buffer into which to format the string into.
retval = ComWrt(COM1, command, 80);
if (retval < 0)
{
printf("ComWrt returned error %d", retval);
CloseCom(COM1);
return;
}
....
....
It's a huge difference and absolutely necessary if you expect others in this forum to give you more specific recommendations than wild guesses into the blue.
One pretty obvious error is the Fmt("H\n", "%s", "C\n"); line. Please check your manual for the description of the Fmt() function! Your use is totally wrong.
And yes, it helps to regularly clean up your code. All those commented out code lines mainly help you to confuse yourself even more.
05-27-2022 07:29 AM
Another hint, based on the document linked here: the newline '\n' is not the correct message terminator!
06-08-2022 05:56 PM
Still struggling with this code. Because I am new to C its hard to determine what is wrong in the code. I have followed your instructions the best I can, I don't see any errors when I run it, it complies and runs, but the output is still always zero.
I notice in the HyperTerminal where it works that flow control needs to be set to Xon/Xoff and also under properties ACCII setup needs to have ASCII send and Receive selected. Not sure if I have to do the same in my C code.
Not sure if I can set the Xon/Xoff bit in the code below. What is the orientation or the flow 9600, 0, 8, 1, 0, 0 versus bits set. Iike can see the 9600 is the baud rate, but what about the rest of the following numbers?
Configure COM ports
//OpenComConfig (COM1, "COM1", 9600, 0, 8, 1, 0, 0); // 0=512 for last two parameters
I got the code below from another code, it seemed it would work, but I cant find any info of the FMT, only that it organizes the code flow I think. Can you please explain this part code to me. Confused....
// Send command to read temperatures & humidity
Fmt (command,"H\n","%s", "C\n");
I already got it working in LabVIEW but would like to get it working in C. Any info would be great, thanks for all help so far.
06-09-2022 12:27 AM
@orginal-iceman wrote:
I got the code below from another code, it seemed it would work, but I cant find any info of the FMT, only that it organizes the code flow I think. Can you please explain this part code to me. Confused....
// Send command to read temperatures & humidity
Fmt (command,"H\n","%s", "C\n");
documentation may help....
06-09-2022 08:14 AM
CVI offers you a variety of helpful resources to help you understand what you are doing
HINT #1 Select a command (e.g. OpenComConfig) and press Ctrl+P (or right-click on it and select Recall Function panel or execute View >> Recall Function Panel menu function, which are the same): you will be prompted with this window that lists all parameters needed for the function
HINT #2 On the function panel, right-click on the free area (i.e. NOT on a parameter field): you will be prompted with the help for the function. This is valid for every function of CVI and ANSI-C libraries
HINT #3 On the function panel above right-click on any of the parameters field shown: you will be prompted with the help specific for the selected parameter
HINT #4 As I already told you,it seems to me that '\n' (newline, ASCII10) is not the correct message terminator: use '\r' instead (carriage return, ASCII 13)
Finally, revise and correct the code according to our suggestions, check that Run >> Break On >> Library Errors menu option is checked and test the code, next post it here together with all relevant informations (e.g. return value for the functions)