11-07-2008 03:58 AM
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
Solved! Go to Solution.
11-07-2008 05:21 AM - edited 11-07-2008 05:21 AM
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.
11-07-2008 08:38 AM
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
11-07-2008 08:40 AM
11-07-2008 09:08 AM
You're great!!!!
Thank you very very much.
11-07-2008 09:11 AM
11-07-2008 09:21 AM
11-07-2008 09:23 AM
02-10-2009 05:03 PM
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...
03-15-2012 01:17 PM
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