Sign in to follow this  
dh53

Get Serial / RS232 Data

Recommended Posts

Hello,

Complete newbie here.  I have been using DaqFactory to acquire, view, and log analog data but now want to log data from a weighing scale with an RS-232 interface.  I am able to add the new serial device using Quick->Device Configuration->New Serial(RS232...  After some messing with the settings I am able to communicate in DaqFactory so that  I can send the command in Comm Monitor "N\013\010"  which this particular scale interprets as "send net weight- carriage return".  I get an ascii response with the correct weight in Comm Monitor.  Also I can set the weighing scale to output data continuously and when I look at Comm Monitor it is showing me the correct weight and time stamp every second or so, so I know DaqFactory can read the RS232 data out of the scale.

The output from the scale is a string of " + x.xx lbs\013\010".  I am obviously most interested in the "x.xx" as numerical data, but can postprocess that later if I can capture the string.

My question is how do I get that RS232 serial data into a variable/ data point I can look at in a graph or output to a data file?  Does this require programming / sequences?  I have so far had great success with DaqFactory, but have not been able to make the leap between the windows based drop down options and programing sequences.  If I want to either:

1) set the scale to only output when prompted, then using DaqFactory ask the scale every 30 seconds for the current weight and display it in DaqFactory and save it to a file

or

2) set the scale to output data continuously but wait 30 seconds for DaqFactory to display and record the data point

 

Does this require a programming Sequence or can it be done in some other way?  I only ask because I (newbie!) have so far had no luck getting any sequence to work.  If I create a sequence called x7 that consists only of "x=7" and then in the command window I type "?x" I always get a C1000 error.  No combination of file save, apply, apply & compile, run sequence, etc seems to get DaqFactory to know I want X to be equal to 7 so obviously I am not even slightly good at sequences.  

 

Thanks!

Share this post


Link to post
Share on other sites

I spent some time with the user manual and am slightly better at sequences, then found an earlier thread about reading data from a scale over RS-232.  The sequence I tried based on the earlier thread here in the support forum and the fact I only want data every 30 seconds was:

private string datain
private data

while(1)
   try
      wait(30)
      datain = device.myDevice.readUntil(10)
      data = strToDouble(mid(datain,6,6))     
      weight.addValue(data)
  catch()
      ? strLastError
      delay(0.1)
   endcatch
endwhile

 

This works, sort of.  Something is reading the serial RS-232 data and keeping it around in memory which I'm not understanding.  The result is my sequence gives me the output of the scale from some time ago, if I change the weight on the scale the value in my variable "weight" does not change for a few minutes.  My next attempt was the same except I added

device.myDevice.Purge()

right after the wait(30) thinking the data in memory would be flushed out.  The result was a semi-success, the value written to the variable "weight" was now the current weight from the scale.  I started logging data and then found the value of "weight" in the Variable-Value display was correct 80-90% of the time, but occasionally was "NaN".  After an hour and 10 minutes of logging 8 data points (7 analog inputs and this one serial input) Daqfactory crashed.  The logged data file shows the value of "weight", the one data point from the serial connection, is missing completely from the data file about every 5th time it is written.  The data from the serial connection has its own time stamp and its own line in the data file separate from the A to D channels, I think this is as expected. 

 

The data file also shows the last write as it crashed it was trying to write this serial data.

 

In looking at the code from the earlier thread it occurs to me I am not reading the serial data starting from the same point in the RS232 string so I tried this:

device.myDevice.readuntil(10) // clear half frame

In the example code in the earlier thread this is outside the While loop but it seems to me this should be happening with every read, no?  I have put it inside my While loop so now I am running the following:

private string datain
private data

while(1)
   try
      wait(30)

      device.myDevice.Purge()

      device.myDevice.readuntil(10) // clear half frame


      datain = device.myDevice.readUntil(10)
      data = strToDouble(mid(datain,6,6))     
      weight.addValue(data)
  catch()
      ? strLastError
      delay(0.1)
   endcatch
endwhile

 

I ran it for an hour with no more "NaN" values.  It has just started logging so crossing my fingers it doesn't crash again.

Question 1) was there something in my earlier code, like trying to write "NaN to a data file, that is obvious why it crashed?

Question 2) is there anything obviously wrong with my current code?  It has been logging now for 2 hours so I am feeling better about it by the hour.

Question 3) if DaqFactory crashes again is there a logfile or any way for me to investigate why it crashed?

 

Thanks.

Share this post


Link to post
Share on other sites

OK, first the reason your x7 sequence didn't work is because you didn't declare x as a variable.  You have to declare it as a global variable:

global x = 7

or:

global x

x = 7

Next, use the query method, not the stream method.  Using stream requires you to constantly service the buffer, otherwise you get old data (like you saw), or you get out of sync (like you saw the second attempt with purge()).  Instead create a sequence that looks something like this:

device.myDevice.purge()
private string datain
while(1)
   try
      device.myDevice.write("send net weight" + chr(13))
      datain = device.myDevice.readUntil(10)
      weight.addValue(strToDouble(mid(datain,6,6)))
   catch()
      ? strLastError
   endcatch
   delay(30)
endwhile

I'm assuming you have the mid() correct, though if the output string is just + xx.x lbs, then you don't need the mid() at all, just:

weight.addValue(strToDouble(data))

 

   

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this