Serial string to control another button.


Recommended Posts

Good day

i have a serial string coming into a channel, and need the string to control another channel but cant seem to figure it out.

a single word, being "Temp" comes into the serial channel (test2) and is supposed to control another channel by changing the value to 1 when it arrives or 0 when anything else arrives.

can you shed some light on this and how it can be done if possible.

thanks

mike

image.png.e94da5073781a37849cba55afdf02830.png

Link to comment
Share on other sites

A few problems here:

1) watch your indentation: that will show you that your catch()/endcatch don't have a matching try

2) the problem point is listed in the error message, line 7.  The issue is the a single = is assignment, while a double == is comparison. if(), while() and other statements that expect a value will never have a single =. 

That all said, you really should do this in the Event for test2.  Then the script simplifies to just:

button = test2[0] == "Temp"

You don't need (or want) the while() in the event because the Event is called every time test2 is updated.  You don't need a catch() because if there is an error, you are going to want to know about it.  And you don't need an if() because there are only two choices, and you want button to one of those two choices.  You can thus use Boolean math. 

test2[0] == "Temp" 

is an expression, much like 3*4, that compares (double == ) test2[0] with "Temp" and returns 1 if they are the same, and 0 if not.  We can then assign (using a single = ) to put that 0 or 1 into button.

Link to comment
Share on other sites

Thank you for the quick reply.

so i did what you suggested and placed 

 button = test2[0] == "Temp"

into the event of the test2 channel and stopped the sequence i was running and it still does'nt change the button state to 1 from a 0.

Any other recommendations?

 

mike

 

Link to comment
Share on other sites

Hi there.

i got a M2M GSM modem and trying to switch a button from 0 to 1 for the moment, then once i get that sorted, i can control others.

For some or other reason the code doesnt change the button state from 0 to 1.

attached is the test2 channel input, and the button input, as well as the .ctl.

 

mike

 

Button timer.jpg

test2 channel.jpg

M2M(2).ctl

Link to comment
Share on other sites

OK, that is because you are using readuntil(13) and there is both a carriage return (13) and a line feed (10) in your data stream.  So, after DAQFactory reads the first line, up to the 13, the next time you call readuntil(13) it reads the 10, then the ASCII values.  So your test2 channel doesn't hold "Temp", but rather a line feed + "Temp".  But you can't see the line feed, so you don't realize it.  When you go to compare that channel to just "Temp" it is always false. 

So, it is in general best to always read until the very end of the line, i.e. do readuntil(10) instead of readuntil(13).  That way you have a 13 at the end of the data instead of 10 at the beginning.  You can then parse off that 13 before putting it in the channel with just:

datain = left(datain, getlength(datain) - 1)

Alternatively, of course, you could use mid() to trim off the 10 at the beginning of the line, but I prefer to have extraneous stuff at the end of the line instead of the beginning, for various reasons.

 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.