简体   繁体   中英

Bitwise operation in C#

I'm a student working on a project for my college's chemistry department. Last year I wrote a C++ program to control a picomotor stage and have it communicate with a force pressure sensor, moving the stage up and down until it touched the sensor. I used the DLL the company provided to write this program. It took me a while, but I got everything to work.

This year, I'm incorporating a second stage to the program and updating the entire program into a GUI using C#. The second stage and the force sensor only provide DLLs that work with C#, whereas the old stage was meant for C++. I was able to get all three to work in the new C# GUI program, it's all almost done save this problem:

In the old program, I was able to determine if the motors on the stage were moving via this function:

#define MOTOR_MOVING 0x01
byte isMoving = LdcnGetStat(pico_addr) & MOTOR_MOVING;

byte LdcnGetStat(byte address) returns the last status byte of the module at the given address. According to the instructional manual I have, bit 0 is suppose to be set when the motor is moving. So, if I test it in my C++ program, using a bool variable, isMoving is 0 when the motors aren't moving, and 1 when they are, and it promptly returns to 0 after the motors stop moving. Works fine.

But in C#, I'm having problems incorporating this.

byte isMoving = LdcnGetStat(pico_addr) & MOTOR_MOVING;

doesn't work, I have to do some explicit casting. So I changed it to this:

bool isMoving = Convert.ToBoolean(LdcnGetStat(pico_addr) & 
Convert.ToBoolean(MOTOR_MOVING);

That doesn't give me an error, but LdcnGetStat(pico_addr) will always return "true" because it's never 0. If I just test the values that LdcnGetStat() spits out, when the motor is not moving, it returns 28, when it's moving, it return s 98. If I test again post-move, it returns 23.

Is there a proper way to deal with this in C#? I'm thinking of checking to see if LdcnGetStat returns the right numerical value, but that doesn't seem like the right way to handle it, especially since the values seem to change post-move.

Thanks

Conditionals in C/C++ effectively compile down to comparisons against zero. So the expression LdcnGetStat(pico_addr) & MOTOR_MOVING is equivalent to (LdcnGetStat(pico_addr) & MOTOR_MOVING) != 0 . In C# however, conditionals are done with actual bool s, so you can only use the second expression.

I can explain more if you need me to.

I think what you're looking for is:

bool isMoving = (LdcnGetStat(pico_addr) & MOTOR_MOVING) != 0;

The expression:

byte isMoving = LdcnGetStat(pico_addr) & MOTOR_MOVING;

Probably gives you an error about implicitly converting int to byte . You have to cast it:

byte isMoving = (byte)(LdcnGetStat(pico_addr) & MOTOR_MOVING);

In that case, isMoving will be equal to 0x01 when the motor is moving.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM