Hey Guys, I'm new to Arduino and having a great time fiddling with the units - a really awesome initiative! Now here comes my first question - I'm trying to get two Arduino Mega's to communicate with one another using the SoftwareSerial (NewSoftSerial) library and I'm running Arduino 0023. The first Mega is being used as a data acquisition device from various sensors then it sends a string of data to the other Mega which has a 3.5' OLED display that displays all those variables. The first Mega is sending this to the other. Code: while( Serial.available( ) >= 60 ) This is a really bad way to read the data. What will happen is that it will not enter the while loop to read the data until there are at least 60 characters in the buffer. It will then enter the while statement and, if the first char is STX (presumably it is), read *at least* 60 characters. But you *aren't* guaranteed that there are any more than 60 so the rest of them may not be there yet. So, presumably, the last few variables are read as -1 (at least, that's what Serial.read returns if there's no data). Then you exit loop() and come back in and some of the remaining characters might be in the buffer. But the buffer has less than 60 chars so nothing happens after that (unless you send another batch). Code: while( Serial.available( ) >= 60 ) This is a really bad way to read the data. What will happen is that it will not enter the while loop to read the data until there are at least 60 characters in the buffer. It will then enter the while statement and, if the first char is STX (presumably it is), read *at least* 60 characters. But you *aren't* guaranteed that there are any more than 60 so the rest of them may not be there yet. This C# Tutorial on Serial Port Communication should be able to help. I'm not sure if there is an appropriatei intrinsic caste between Hex and Byte through assignment in C#. I'm not sure if there is an appropriatei intrinsic caste between Hex and Byte through assignment in C#. So, presumably, the last few variables are read as -1 (at least, that's what Serial.read returns if there's no data). Then you exit loop() and come back in and some of the remaining characters might be in the buffer. But the buffer has less than 60 chars so nothing happens after that (unless you send another batch). Of course there is. You should not wait for all the data to be in the buffer before you read any data. If there are supposed to be a fixed number of bytes sent, you are screwed. Serial data is not guaranteed to be delivered. There are any number of reasons why a byte could be lost. Photoshop cs6 deutsche sprachdatei download google free. Relying on a specific number of bytes is not a good idea. A combination of start and end markers, a length value in the packet, and a check sum is required to assure that a packet is received as sent. However, if you want to assume that nothing will ever get lost, you have two choices. Either set up a while loop that blocks until that number of bytes has arrived, reading them as fast as they arrive, or read whatever is available on each pass through loop, and do something with the data only when the expected number of bytes has arrived. In the snippet of code you posted for the sender, you are not expecting to send a fixed number of bytes. You are sending start and end markers (though what they are is hard to tell). If you were sending ASCII data, you could use code like this to receive it. That's not the problem. As I mentioned, it is reading the first 63 bytes or so. While you're processing those 63, the rest of them arrive. When you come back into loop() you have 9 characters in the buffer but the 'if' statement won't process less than 60 at a time. The really quick and dirty fix is to change the number in the if statement to exactly the number of characters in a packet which, if my math is correct, is 72 including the ETX at the end. IIRC on a MEGA this won't be a problem because the buffer is 128 bytes. This download was checked by our built-in antivirus and was rated as clean. This PC software can be installed on 32-bit versions of Windows 7. A much better solution is to handle the bytes for each variable as they arrive using a table to describe the number of bytes in the variable and its address. The following code is for the receiving end. You have to complete the missing info from the data_tab table. Binary Synchronous Communication ( BSC or Bisync) is an character-oriented, half-duplex, announced in 1967 after the introduction of. It replaced the (STR) protocol used with second generation computers. The intent was that common link management rules could be used with three different character encodings for messages. Six-bit looked backwards to older systems; with 128 characters and with 256 characters looked forward. Transcode disappeared very quickly but the EBCDIC and USASCII dialects of Bisync continued in use. At one time Bisync was the most widely used communications protocol and is still in limited use in 2013. Contents • • • • • • • • • • Framing [ ] Bisync differs from protocols that succeeded it in the complexity of message framing.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |