LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Problem: read bytes at serial port

Solved!
Go to solution

Hello, I need to comunicate with a external device with serial port. I have to write a command string to my device and then it should replay with 64 bytes of data. I built a function WRITE-READ and I use it in a for loop (I have to read lot of block of 64 byte)

Sometimes I have the following problem:

1) I send the command string, I wait with "Bytes at serial port" while I have more then 60 bytes then I read.

2) Sometimes happen that the read function can't read all the 64 bytes, so at net for cycle I have in the receiver buffer mor than 64 and the program stop to work.(doesn't work well)

 

 I need to be sure that after writing command I must have 64 bytes, then I have to red all 64 bytes so at the next for loop I have the receiver buffer empty.

I attach my Write read function. Please refer to block diagram (there are some notes).

Thanks

0 Kudos
Message 1 of 23
(7,478 Views)

The best way to read a binary stream is to make a binary parser.

You have first to read the header (sync. byte), than read the length, than read the package, than check the checksum or CRC (if available) and than convert the data from binary string to data using Unflatten From String VI.

Message Edited by Eugen Graf on 11-07-2008 05:21 AM
0 Kudos
Message 2 of 23
(7,465 Views)

Please could you explaine me what does parser mean? I write command, I wait for 64 bytes then I read: sometimes the read function don't read all 64 bytes but less.(though in the input buffer I have  64 bytes) So at the next execution of the funtion (new command....., new wait for 64 byte) in the receive buffer I have the new 64 bytes + the oldest that the previus read didn't read. Then I loss the syncronization. Is it possible to resolve in a easiest way?

Thank you in advance

Maurizio

0 Kudos
Message 3 of 23
(7,427 Views)
Solution
Accepted by topic author maumontesi
In no place in your program do I see the VISA Configure Serial Port. It is alwasy a good idea to include that even if you are using the default settings. I suspect it is one of the default settings that is hurting you. By default, the read is set to terminate when a termination character is seen. That means that if you have a hex 10 (ASCII LF) in your data stream, the read will stop before the specified number of bytes is read. Put in a VISA Configure Serial Port and wire a False constant to the Enable Termination Character input.
Message 4 of 23
(7,425 Views)

You're great!!!!

Thank you very very much.

0 Kudos
Message 5 of 23
(7,417 Views)
It would probably be more appropriate to mark the actual solution instead of your response.Smiley Wink
0 Kudos
Message 6 of 23
(7,410 Views)
Sorry!! Is it possible to change??
0 Kudos
Message 7 of 23
(7,402 Views)
Yes.  Go to the options menu for your repsonse and you can uncheck it.  Then you can go to the options menu for Dennis' response and mark is as accepted.
0 Kudos
Message 8 of 23
(7,398 Views)

I couldn't help but notice what I assume was a slip of the tongue: " hex 10 (ASCII LF)"  should read " hex 0x0A or decimal 10 (ASCII LF) " should it not?

Hex 10 is most definitely not a line-feed!  Call me pedantic... :smileyhappy:

 

 

----------------------------------
LabVIEW 8.0.1; WinDoze XP
aut viam inveniam aut faciam
0 Kudos
Message 9 of 23
(7,140 Views)

Hello,

 

I have a similar problem. Except that I'm new to .NET protocols. I'm normally use Modbus protocols and I get confused when I use .NET. So I go this document that explains how to get data from some charge controllers. I attached the document to see if it makes it easier to explain my problem. 

 

According to this document I use Byte 0 to select my command. Byte 1 would be my source address (address of my device). Byte 2 would be the destination address which apparently I has to use 0xFF. Byte 3 should tell how many bytes are in the packet, but I'm not exactly sure how to know that. Then we have the nth byte which I think it's the one that specifies which variable you want to get (Voltage, current, power, etc.). Finally is the checksum. 

 

My questions are:

 

Should I just send an array with all of the bytes mentioned?

If so, do I chose the number of bytes in my packet or will that vary somehow?

Also, how do I create the checksum?

Do I sum all of the bytes prior to the checksum, negate it and convert it to a byte?

For the nth bytes, are the values on these bytes 4, 5, 6, and so on as seen in the table of my document, or do I leave those bytes in blank so that I'll be reserving space for the returning bytes?

 

If any of you can help me with that I would greatly appreciate it. 

 

Thanks in advance,

 

Jose

 

 

0 Kudos
Message 10 of 23
(6,273 Views)