LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Write in to serial communication from hex text

Hello every one:

 

  I have made up a simple code to demonstrate my problem more clearly and I deeply appreciate it if some one could guide me through.

I have a device receiving commands via serial communication. I have succeeded in making the connection and sending and receiving commands. The device shows response whenever received a correct command.

I have a status check command which works instantaneously and receive response in milliseconds. The status check command is in hex mode and while sending the command I have to set the "display mode" of the controller to hex to be able to successfully send the command and if I send the command in diagram, it will not be sent in hex mode and will not be correct and no response will be received.

I would be pleased if you could guide me on how to convert a "hex string" in to "string" for this situation.

 

data:     "7e 30 34 30 30 30 30 30 30 46 45 37 43 0d"   this is my hex string prepared to send

this data only works fine when i set the representation mode to hex, what i need is to send my previously prepared text in hex format without converting it again

Download All
0 Kudos
Message 1 of 13
(4,361 Views)

So your command is actually "~04000000.-%+" with a Carriage Return.  Looks like a normal ASCII string to me.

 

Perhaps you can enlighten us to what the command protocol actual is?  A manual for the device might have that information.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 2 of 13
(4,350 Views)

Thanks for the soon reply, appreciated.

Yes. You have mentioned the converted data correctly, the problem is I need to input the data in hex format as I only have hex format after a very massive calculations, 

The sample I provided is the format of my command in hex but written in text format. All I need is to send my hex data in string mode.

If I copy the '7E . . . . . . 0D' into my control and send, it doesn't work unless I set the view format of the control to 'hex format' . That is fine but I can't just send my text data in hex inside diagram. 

0 Kudos
Message 3 of 13
(4,339 Views)

@Lotfee wrote:

Thanks for the soon reply, appreciated.

Yes. You have mentioned the converted data correctly, the problem is I need to input the data in hex format as I only have hex format after a very massive calculations, 

The sample I provided is the format of my command in hex but written in text format. All I need is to send my hex data in string mode.

If I copy the '7E . . . . . . 0D' into my control and send, it doesn't work unless I set the view format of the control to 'hex format' . That is fine but I can't just send my text data in hex inside diagram. 


That last part just doesn't make any sense.

 

If you set the view format to hex and it works,  why don't you just do that?

 

The concept of hex, and bytes and ASCII can be very confusing because people use the terminology in different ways to mean different things.

 

If you need to send a carriage return to this device, do you need to send it has a single byte that has a value of 0D in hex display, or do you need to send to bytes, a literal "0" and a "D" which is what would appear in a string control or constant with normal display set?

 

Remember, what you see on the screen in a string control is just set to different formats to make it easier for the programmer and/or the program user to see what is being sent.  The setting of the display format does NOT affect what the underlying data is.

 

If you need to change data from a literal "0D" (2 bytes, hexadecimal characters of 0 and D) to a single byte with a value of 0D hex (13 decimal), there are conversion functions in the string palette, to help you with that.  And vice versa.

0 Kudos
Message 4 of 13
(4,331 Views)

Dear knight of NI:

 For the part you mentioned that is not making sense, you have used a very good word "underlying data" which is actually being transmitted. That data needs to be hex in my case. The data which is finally connected to a "write visa function" via string. Now my data for sending can be considered as a .txt file. The containing is hex but saved in .txt file. My data is a string while the connecting line to it is pink in NI, but the ingredients are hex. Consider a simple .txt file on the desktop while the containing is as follow:

"7e 30 34 30 30 30 30 30 30 46 45 37 43 0d"

If I copy the text manually inside the controller of my VI and send the data, it will not work, now if I first change the display type to hex and then send my data, it will work cause my data is hex. I exactly need this display transition inside my diagram to send my results via serial, NOT a control page to change it's display type and then type these data into it and send.

0 Kudos
Message 5 of 13
(4,319 Views)

So are you saying you are copying and pasting that text into the control AFTER you have set the display format?

 

The problem with the terminology is that "hex data" doesn't mean much unless you put it into context.

 

When the  display is set for normal mode, and you copy and paste in "7e 30 34 30 30 30 30 30 30 46 45 37 43 0d" (without the quotes), the underlying data is 41 bytes.  Each byte represents the ASCII value of 7, of e, of space, of 3, of 0, ....

 

When the display is set for hex display mode, and you copy and past in "7e 30 34 30 30 30 30 30 30 46 45 37 43 0d" (without the quotes), it will wind up looking like

7e30 3430 3030 3030 3046 4537 430d  . That is 14 bytes of data.  the ASCII characters for those bytes are ~04000000FE7C carriage return.

 

So, the question is, what are you really trying to do here?

Where is this data coming from?  From a file?  Are you just typing it in?

 

You say you have found a way to make it work.  So just do it that way!

 

I'm going to quote your original message:

"

The status check command is in hex mode and while sending the command I have to set the "display mode" of the controller to hex to be able to successfully send the command and if I send the command in diagram, it will not be sent in hex mode and will not be correct and no response will be received.

I would be pleased if you could guide me on how to convert a "hex string" in to "string" for this situation.

 

data: "7e 30 34 30 30 30 30 30 30 46 45 37 43 0d" this is my hex string prepared to send

this data only works fine when i set the representation mode to hex, what i need is to send my previously prepared text in hex format without converting it again"

 

What do you mean by display mode of the controller to hex?  By controller do you mean the device you are talking to?  Or are you actually talking about the CONTROL on the front panel of your LabVIEW VI?

 

And by "if I send the command in diagram, it will not be sent in hex mode", what do you mean?  By "send the command in diagram" are you talking about the block diagram of your VI and putting the command in a string constant?   If so, do you know that you can take a string constant on the block diagram, make the display mode visible, and you can change it to hex display, normal display, \code display, or password display.

 

If you have a string in a control that works, you can also right click on that controls terminal on the block diagram, and pick Create Constant, or Change to Constant.

0 Kudos
Message 6 of 13
(4,302 Views)

Dear Ravensfan:

 

   thanks for your help first.

So, the question is, what are you really trying to do here?

Where is this data coming from? From a file? Are you just typing it in?

Lets consider it this way:

I have data received from machine A in string format (normal text), as follow:

"7e 30 34 30 30 30 30 30 32 42 46 30 33 35 38 31 39 41 39 34 36 39 39 30 30 30 46 45 37 43 30 33 31 30 33 3 0d"

have done a massive checksum calculation on it and after all this is what I have in string format (normal text):

"7e 30 34 30 30 30 30 30 30 46 45 37 43 0d"

I still have it in normal string format and need to send it to machine B within diagram and as hex and without typing into CONTROL of the front panel of labview.

Now what happens is that I have my result in hex string but labview identifies it as normal text, in diagram. If I change the string to hex using programming functions, labview converts it again and as an example: 30 turns into 3330 which would be a mess.

All I need now is to send the calculated data to machine B without messing it up and as real hex. Labview looks at my results as a normal string but I need them to be considered as hex.

Hope I could clarify my matter

0 Kudos
Message 7 of 13
(4,256 Views)

Ho Lotfee,

 


@Lotfee wrote:

Lets consider it this way: …

Hope I could clarify my matter


Instead of so much text you could provide an example VI containing:

- typical input data, set as default value in a control

- the current resulting data, set as default value in an indicator (and also available by running this VI)

- the expected resulting data, set as default value in another indicator (or a string constant)

 


@Lotfee wrote:

I have in string format (normal text):

"7e 30 34 30 30 30 30 30 30 46 45 37 43 0d"

I still have it in normal string format and need to send it to machine B within diagram and as hex and without typing into CONTROL of the front panel of labview.

Now what happens is that I have my result in hex string but labview identifies it as normal text, in diagram.


LabVIEW considers strings as strings, it doesn't care about any display mode you are using in indicators. When you need a certain result you need to program your VI to get this result.

Again: attach an example VI!

Best regards,
GerdW


using LV2016/2019/2021 on Win10/11+cRIO, TestStand2016/2019
0 Kudos
Message 8 of 13
(4,243 Views)

That explanation did help.

 

As I said, you will need to use some conversion functions from the string palette.

 

I also searched the forum and started with an example I found.  (Which can be difficult because as I said before, the terminology gets so confused that you have to read a lot before you find an example that truly reflects what you are doing.)

 

Attached is a VI.  Also a snippet of it to visualize it in the thread.

 

Download All
0 Kudos
Message 9 of 13
(4,230 Views)

@RavensFan wrote:

Attached is a VI.  Also a snippet of it to visualize it in the thread.

 


Way too complicated.  Spreadsheet String To Array.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
Message 10 of 13
(4,222 Views)