|
If these bits are confusing you, then you need to go back and start with the C# basics - there is nothing there I would not expect anyone halfway trained in C# to have problems with.
private const uint COMMAND_LOAD_RAM_AND_RUN = 1;
private const uint COMMAND_LOAD_EEPROM_AND_RUN = 3; Just declares two constant values so that you can use names in your code instead of relying on magic numbers.
lfsrData[0] = 0xF9;
for (int i = 1; i < 251; ++i)
lfsrData[i] = (byte)(0xFE | (IterateLFSR() ? 1 : 0)); Presets a buffer of 251 bytes with a specific sequence: the first byte will be 0xF9, all teh others will be 0xFE or 0xFF, depending on how the IterateLFSR method works. Which is dependant on the value of _LFSR, which depends on when you last called ResetLFSR - and I'm not going to try to follow your code to work that out!
byte[] buffer = new byte[258];
for (int i = 0; i < 258; ++i)
buffer[i] = 0xF9;
Just declares a byte array and fills it with a simple hex value.
for (int i = 0; i < 250; ++i)
{
if (ReceiveBit(false, 100) != IterateLFSR())
throw new ApplicationException("Receiving LFSR data failed!");
}
byte version = 0;
for (int i = 0; i < 8; ++i)
{
version >>= 1;
version += (byte)(ReceiveBit(false, 100) ? 128 : 0);
} Just seems to be a way of clocking in eight bit data on a serial line, with a varying prefix bit that I can't be bothered to work out. Followed by chucking the data you just received away.
To be honest, it's a bit rubbish. Someone has tried to use mnemonics for special values, and then ignored that and hardwired magic numbers such as 0xF9 in anyway, without trying to explain why.
Go back where you got it, and see if there is any design documentation which might explain why they are doing this!
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
Thank you for the reply.
Being self taught, I have holes in my knowledge. Most of the methods, make sense on a literal reading of them, but I was hoping to hear why this might be necessary to interact with the microchip. I have also not ever seen the >> or ? and : used in c sharp, and that was also throwing me such as in this part
version >>= 1;
version += (byte)(ReceiveBit(false, 100) ? 128 : 0);
Unfortunately there was no documentation, I did message the person, but they have not been active recently and I did not receive a reply.
|
|
|
|
|
>> and ? are standard bits of C#: you can find out about them quite easily if you google for C# Operators, which will take you to MSDN[^] - a good place to start if there is any C# or .NET thing you don't understand.
">>" is a SHIFT operator. It moves a value one bit to the right, effectively dividing it by two. So 20 hex would become 10 hex, 10 hex would become 08 hex, and so forth. The bottom bit is discarded.
"?" is a short hand for an if...then...else... with a value returned. The condition is to the left of the "?", the true value is to the right, and the false value is to the right of the ":". So:
version += (byte)(ReceiveBit(false, 100) ? 128 : 0); could be written as:
if (ReceiveBit(false, 100))
{
version += (byte) 128;
}
else
{
version += (byte) 0;
}
[edit]Typos: I hate this keyboard - OriginalGriff[/edit]
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
Great!
I found this link also before I saw your reply. I did not know that c sharp had built in bit operators like the microchip does.
This should be enough to make me successful, thank you!
http://www.functionx.com/csharp/Lesson05.htm[^]
|
|
|
|
|
You're welcome!
(But you should consider a course or a book on C# and .NET anyway - you will have missed a lot by self learning)
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
Some people self-learn C# by reading the C# spec and following Eric Lipperts blog - obviously not the OP though.
|
|
|
|
|
Self learning from language spec is one thing (and a PITA it can be, I've done it often enough) but you just miss so many things which are implicit in a language, rather than part of the grammar. If you follow a structured program, you get introduced to the reasons behind what you are learning as well as the language elements. Unless you follow one of those "Learn XXX in 7 days!!!" books, in which case the main lesson is "Don't waste your money on books with multiple exclamation marks".
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
Many introduction books contain severe errors though ("value types go on the stack" and such), do you know one that doesn't?
|
|
|
|
|
No - but I suspect they say that as a simplification (lies-to-children[^]) that will do because it is (sort of) correct and (sort of) explains why there is a distinction between value and reference types without potentially confusing people with more than they are ready for at that stage. Hopefully, they later correct that - but I suspect that either most authors forget to do that bit, or most readers don't get that far...
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
I would like to maybe even take a class. This was my first OO language (and really my first real language period), which is why I couldn't read a spec and learn a language. Maybe that'd be possible if this was my 3rd or 4th language that I was learning?
I found out some information by grouping together a bunch of posted information that shows the code is instantiating a protocol, with linear feedback shift register verification.
I'm still trying to get used to this short hand of if/then/else and I can't seem to get the long hand version translation of the first code block? The second code block is my 4th attempt, I'm just not sure how they are setting lfsrData array value to 0xFE (254) or if true 1 else 0. That logic is escaping me?
lfsrData[i] = (byte)(0xFE | (IterateLFSR() ? 1 : 0));
lfsrData[i] = (((byte)(0xFE)) || (
if (IterateLFSR())
{
lfsrData[i] = 1;
}
else
{
lfsrData[i] = 0;
}
))
Quote: First the Propeller Tool seems to open the serial port in 115200-8-N-1 mode. After toggling the DTR line to reset the Propeller, it transmits 0xF9 followed by a sequence of 0xFEs and 0xFFs. This sequence is followed by a large block of 0xF9s. I'm sure all this corresponds to the 250 iterations of the LFSR but I don't see how. When I tried to duplicate the LFSR in Python I couldn't get anything even remotely close. Then the Propeller responds with a similar (but different) block of 0xFEs and 0xFFs. Again I'm
sure this corresponds to the LFSR somehow. The Propeller then sends 2 bytes 0xFE 0xFE which I believe is the version number?
After this the Propeller Tools sends a command byte (RAM or EEPROM) followed immediately by block of data which corresponds to the binary image of the program. I haven't spent alot of time analyzing the format of the this data, but it doesn't seem to directly match up with the bytecodes of the program. Something weird is going on here and I think has to do with how the Propeller is "reading" the serial data.
Finally the Propeller Tool sends 0xF9 periodically (every 50msec seems to work) until the Propeller responds with 0xFE. This acknowledgment protocol can be repeated twice more if the command code was programming to the EEPROM.
The $F9 (249) is the 'calibration pulse'. The start pulse is a short (one) pulse while the two zero bits in the $F9 (249) is a long (zero) pulse. After that, the least significant bit of the LFSR is sent by the Tool, one per character, for 250 bits, with the $FF (255) being a short (one) pulse and the $FE (254) being a long (zero) pulse. Then the Propeller sends back the next 250 bits in the LFSR, also one bit per byte by echoing the $F9 (249) characters, changing them to $F8 (248) to indicate that they were processed properly.
After all the LFSR bits, the Tool and the Propeller shift into a 3 bits per byte mode with each bit cell being 3 bits and the start bit counting as one bit of the first cell (which is always zero). As you might expect, a one is a short (1 zero pulse time) and a zero is a long (2 zero pulse times) and the Propeller echos the data. There's a mix of 8 bit and 32 bit sequences, etc
|
|
|
|
|
The long-hand version is only meant to show what ?: does, it is not the same thing. The problem here is that if/else is a statement (so it has no value, only a side effect) and a?b:c is an expression (it computes a value).
On top of that, the || operator is a short circuiting operator that obviously (once you know what it does) can not work on bytes.
Furthermore, in C# arithmetic operators on bytes produce ints as result, so even if you fixed the other errors the compiler would complain about the type.
But that's all easy to solve, for example:
- by writing (byte)(IterateLFSR() ? 0xFF : 0xFE)
- by writing
byte x;
if (IterateLFSR())
x = 0xFF;
else
x = 0xFE; (and then using x , of course)
- or (byte)(IterateLFSR() | 0xFE) if you change IterateLFSR to this:
private int IterateLFSR()
{
_LFSR = unchecked((byte)((_LFSR << 1) | (_LFSR >> 7 ^ _LFSR >> 5 ^ _LFSR >> 4 ^ _LFSR >> 1) & 1));
return _LFSR >> 1;
}
I moved some stuff around there, but the result is the same (as a bit in an int instead of a bool that has to be converted back to that bit). The other bits in the result should be treated as garbage. Or-ing it with something that has 1 in all the high bits throws them out which is why it isn't a problem in IterateLFSR() | 0xFE .
|
|
|
|
|
Thank you Harold!
I was able to get the method to do what I wanted it to, now I need to get it to write bytes ... I have A LOT to learn!!
|
|
|
|
|
If you're asking why the code does what it does, that answer can be found in the documentation for whatever chip this is driving.
Some parts of that code look rather strange, you might want to go back to the source (metaphorically speaking, not this source code!) and rewrite the protocol code yourself. It would be a good exercise in 'bit-twidding' in C#. Does this code actually work? It doesn't look like it should in a few places unless the underlying protocol is weird.
|
|
|
|
|
Hi,
I need code examples how show number of online users in my site application.
Thanks.
Rick
|
|
|
|
|
0) Count them
1) Display the count
But, personally, I wouldn't bother; I mean, look at the count for Code Project, it's pretty much pointless.
|
|
|
|
|
First of all you should have a flag field on your users table which determines which user are online. On my databases this field I name it as "logged" with the type of bit and then on the time when the user is logged to the application then this field will be updated to true and on the time when he is loged out then will be false. Since you have this flag on your users table it's very easy to know which users are online.
Regards
Qendro
|
|
|
|
|
Hi,
I have observableCollection which contains string types.
How do I sort it?
|
|
|
|
|
Look at this article[^]. It discusses three approaches.
The third option, the extension method option, is really the best one.
|
|
|
|
|
Do you really need to sort the collection itself or just view it as sorted without changing the underlying collection? If the first, follow the previous answers. If the second, then you can just expose an ICollectionView[^] and bind to that rather than to the root collection.
|
|
|
|
|
|
I would suggest you need to compile and link it into an exe. Exactly what problem are you having?
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
|
Why I use directx.capture save as wmv, I can not set to 640x480 wmv files
|
|
|
|
|
Does your video card support low resolutions?
You should be able to do it by using setDisplayMode .
e.g. ddraw.setDisplayMode (640, 480, 8, 0, 0);
|
|
|
|
|
hi frnds im doing a project and in a face of adding a forum in my project so is there anyone who can help me....with code or ideas regd forum in .net c#.....im new here and wish ur help and support dear friends........
|
|
|
|