0

In my project I want to create a Windows application using C# to communicate with the Electronic Board Atxmega via Serial Port Using the Modbus protocol. I could send the write command from the PC to board and the Board send the correct response but I couldn't read it in my PC. I don't know why I can't receive the complete packet

it tried this

bytesRead = new byte[Modbus1.BytesToRead];

for (i = 0; i < Modbus1.BytesToRead; i++)
{
    bytesRead[i] = (byte)(Modbus1.ReadByte());
}

and this

Modbus1.Read(bytesRead, 0, Modbus1.BytesToRead);

but I couldn't receive the complete Packet so how can I receive the complete packet from the serial port ? And what's wrong with my code ?

Christoph Fink
  • 22,727
  • 9
  • 68
  • 113
cherev
  • 1
  • 1
  • 1
    You say "couldn't read" and mention "the complete packet". What exactly *does* happen? What do you receive? – Marc Gravell May 15 '14 at 08:37
  • normally the response is 30 byte i receive only the First three byte – cherev May 15 '14 at 09:17
  • so what do you get for the rest? -1? or...? – Marc Gravell May 15 '14 at 09:19
  • Modbus1.BytesToRead give just 3 so nothing for other but i don't know why when i get response ie read it in more then two timed in the first time it read the 3 first byte and then the Other some other and then other ... – cherev May 15 '14 at 09:28
  • So? That number is essentially meaningless; that just tells you what is available *right now*. Data comes in streams; you often need to read in a loop to assemble the original data. – Marc Gravell May 15 '14 at 09:30
  • you often need to read in a loop who can i create the loop if i don't know who it receive at the first time it receive 3 the second some time is 10 and some time 13 ....it don't think that is a good idea for it ...........and what will i do if i will send another request ? – cherev May 15 '14 at 09:39
  • which is why most protocols include a framing mechanism that defines logical messages separate to the physical data; for example, binary protocols often use a length-prefix, where-as text protocols often use a sentinel value (such as a line-feed) to indicate the end of logical messages. You read (and benerally: buffer locally) until you have a complete logical message (frame), then you process *that*. See also: http://tiny.cc/io – Marc Gravell May 15 '14 at 09:41
  • please i use the Modbus Protocol is send this request 01 03 00 00 00 0D 84 0F (to read 13 register ) and the Slave response is 01 03 1A BB 80 00 00 41 CA 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 12 1D 02 14 0E D6 1E but i my code sharp i receive this at the first time 01 03 0A at the second time BB 80 00 00 41 CA 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 12 1D 02 14 0E D6 1E 01 03 1A so (this is for just one example ).... – cherev May 15 '14 at 09:54
  • so if you are expecting 30 bytes; **ask it for 30 bytes**. Again, `BytesToRead` is basically meaningless in your context. – Marc Gravell May 15 '14 at 09:56
  • i did it and I get the same problem at the first i receive 01 03 1A 00 00...........00 and in the second i receive BB 80 00 00 41 CA 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 12 1D 02 14 0E D6 1E 01 03 1A – cherev May 15 '14 at 10:16

0 Answers0