Jump to content

problem with serial write & type conversion


oskarbosch

Recommended Posts

Hi guys

I want to send a number for controlling a PWM pulse, via labview to a microcontroller.

i have problems with type conversions. number to send is, 1 byte, 0-255, thus is 3 characters as a string.

as it seems labview just send first character, and then PWM wave didn't change much.

i confused with type conversions, string to number, number to array !!! :wacko:

I attached a picture of my simple VI.

how must i change my VI to send numbers completely?

post-3628-1136704965.jpg?width=400

Link to comment

One byte should be just one character, right? You will want to make the datatype of the number to be U8 (and not double or single as you have on the diagram). Then, typecast this to a string (you will find the Type Cast function under Advanced >> Data Manipulation palette.)

Let us know if you still have issues.

-Khalid

Link to comment

I agree that Khalid's answer may be what you really need to make your application work, but to answer the question as you posed it, to get your simple VI to output a 3-byte ASCII string then you need to use a %03d format string.

FWIW, you used the the "Format Value" function found on the String -> String/Number Conversion palette.

That (and most all of the other conversions on that palette) are somewhat outdated and have been replaced by the more universal "Format into String" function (and its complementary "Scan from String" function) found on the top-level String palette. One of the advantages of the "Format into String" function is that you can right-click on it and select the "Edit format string" option which will bring up a window which will lead you through the various options required to produce the type of output string you desire. For example, the foregoing %03d format string is built using:

Format Decimal Integer + Right Justify + Pad with zeros + minimum width=3

Link to comment

Hi all

My problem didn't solve yet, I tried "Type Cast" but I don't know what I should use as third input of this block and then output is rubbish characters!

and I also used "03d" with format value but it doesn't work.

I write a simple application with VB to test that if problem is from Microcontroller codes, but it work correctly with VB program (I have a pulse with varying width in Oscope) but when I test it with LabView the width is constant (or maybe little variation that I can't see)

I attached my VI, any idea?

Download File:post-3628-1136786737.vi

Link to comment

In your first post you said you wanted to send 1 byte out. This translates to one character. I am attaching the modified VI that uses U8s and type casts it to a character. Try it out.

What are you sending out from your VB program? Can you attach the relevant code snippets? Or maybe some documentation/manual for your PWM device.

-Khalid

Download File:post-311-1136788564.vi

Link to comment

Thank you khalid for your very fast responses, it works, but not as smoothly as VB program do.

these are VB codes:

Dim a As StringOption ExplicitPrivate Sub Form_Load()MSComm1.CommPort = 1MSComm1.Settings = "9600,n,8,1"MSComm1.PortOpen = TrueEnd SubPrivate Sub HScroll1_Change()a = HScroll1.ValueMSComm1.Output = Chr$(a)Debug.Print aText1.Text = aEnd Sub

(my VB app have a little bug, can you find it!?)

and this is Microcontroller codes in BASIC

Do   B = Inkey()   If B > 0 Then	  B = B * 4	  Pwm1a = B   End IfLoop

(Pwm1a must be between 1 and 1024)

VB divide pulse width in 255 section, but as it seems LabView just divide it into 10.

Link to comment

h4med,

It looks like, in your VI, you are constantly opening the serial port. :thumbdown:

Yet in your VB code you open it only when you load the form.

So an easy fix would be to program a little dialog box, with stop button,

that opens the serial port only in the beginning, sends the bytes as needed

(only when the slider changes, use event structure).

that should make it much smoother. :thumbup:

Oskar

Link to comment

H4med,

I would like to know what is the "HScroll1.Value" in your VB program.

From the statement "MSComm1.Output = Chr$(a)" it appears that you are just converting the "HScroll1.Value" to a character and writing to the output. In LabVIEW, as you can see in the VI I attached earlier, we are taking a number and converting it to a character and writing it out. If this doesn't work, then the problem is what numbers are we converting -- in the VB program versus LabVIEW -- before converting them into characters and writing out.

I strongly recommend that you download Portmon (or any other Serial port monitor) and monitor the actual bytes going out from your VB program, and compare it with LabVIEW's output:

http://www.sysinternals.com/utilities/portmon.html

Hope this helps.

-Khalid

Link to comment

khalid,

thank you for introducing Portmon, that is a very useful software.

by monitoring serial port this is the result:

in VB: when I change scroll, on port I have:

..Length 1: 50Length 1: 51Length 1: 52Length 1: 53Length 1: 54..

as I said, and expected output changes smoothly between 0 and 255 (FF)

but in LabView: these are the results:

..Length 4: 00 00 00 19  Length 4: 00 00 00 32 Length 4: 00 00 00 4B Length 4: 00 00 00 64 Length 4: 00 00 00 7D ..

First, output is 4 word, and second it changes with big steps.

(This is VI:)

Download File:post-3628-1137220009.vi

Link to comment

LabVIEW is sending out 4 bytes because you are converting a U32 (a 32-bit number) to characters.

To fix this: right-click on the constant "25," select Representation and then U8. This will then output just one byte, i.e. Length = 1.

Hope this fixes it.

Regards,

-Khalid

Link to comment
Hi guys

I want to send a number for controlling a PWM pulse, via labview to a microcontroller.

i have problems with type conversions. number to send is, 1 byte, 0-255, thus is 3 characters as a string.

as it seems labview just send first character, and then PWM wave didn't change much.

i confused with type conversions, string to number, number to array !!! :wacko:

I attached a picture of my simple VI.

how must i change my VI to send numbers completely?

post-3628-1136704965.jpg?width=400

the sending of the no. or a string is dependent on the time u have taken to send it through serial port

bcz if u put the whole of the cicuit in the while loop or the for loop with time delations u can eeasily see

the strings it can end for that perticular time. if u wish i can send u a circu9it for u also.my email address is

01.ammar@gmail.com

Hi oscar

Problem is the number that Labview send to chip, VB send a number between 0-255, but Labview send a number between 0-10, then PWM pulse width change roughly..

I wrote VB program in less than 10 minute, but after two days I couldn't get same answer from Labview! :headbang:

u can change the type of the number u are sending to serial port, bycliking the search icon in the control palate and writing in the box like,string to decimal conversion,and u will get the idea of how to convert from ascII to other types.

Hi guys

I want to send a number for controlling a PWM pulse, via labview to a microcontroller.

i have problems with type conversions. number to send is, 1 byte, 0-255, thus is 3 characters as a string.

as it seems labview just send first character, and then PWM wave didn't change much.

i confused with type conversions, string to number, number to array !!! :wacko:

I attached a picture of my simple VI.

how must i change my VI to send numbers completely?

post-3628-1136704965.jpg?width=400

[/quote ammar saleem]

use the while loop or the for loop for more strings to be transmitted or u can check the process by clicking run continously icon ie with the run icon

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
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.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.