Ahoj,
jsem vb.net vyvojar, vetsinou s pochoponim jednodusich veci v C# problem nemam, ale ted jsem na nej narazil,
chtem bych proto Vas, co v C# delate pozadat o vysvetleni kodu:
public float GetTemperature()
{
// Start conevrt command (0x51)
i2cBus.Write(_slaveConfig, new byte[1] { START_CONVERT }, I2C_TIMEOUT);
// Wait to conversion end
Thread.Sleep(750);
// Read temperature command (0xAA)
i2cBus.Write(_slaveConfig, new byte[1] { READ_TEMPERATURE }, I2C_TIMEOUT);
// Read MSB and LSB of the temperature
byte[] readBuffer = new byte[2];
i2cBus.Write(_slaveConfig, readBuffer, I2C_TIMEOUT);
// Get MSB and LSB together
int temp = readBuffer[0];
temp <<= 8;
temp |= readBuffer[1];
// Negative value test (subzero temperature)
temp -= (readBuffer[0] >= 0x80) ? 65536 : 0;
// Get the temperature in Celsius
return (float)((temp >> 4) * 0.0625);
}
jde mi o radky:
temp <<= 8; // predpokladam, ze se jedna o temp je vetsi, nebo rovne 8, ma tento radek ale vliv na dalsi? pokud ne, tak radek nema funkcni vyznam, ne?
temp |= readBuffer[1]; // jaka je funkce znamenka |= ?
temp -= (readBuffer[0] >= 0x80) ? 65536 : 0; // tu jsem bezradny
return (float)((temp >> 4) * 0.0625); // zde kdyz to dobre chapu, zustane v nejvnorenejsi zavorce boolean?
pri strojovem prekladu me vyslo tohle
Public Function GetTemperature() As Single
' Start conevrt command (0x51)
i2cBus.Write(_slaveConfig, New Byte(0) {START_CONVERT}, I2C_TIMEOUT)
' Wait to conversion end
Thread.Sleep(750)
' Read temperature command (0xAA)
i2cBus.Write(_slaveConfig, New Byte(0) {READ_TEMPERATURE}, I2C_TIMEOUT)
' Read MSB and LSB of the temperature
Dim readBuffer As Byte() = New Byte(1) {}
i2cBus.Write(_slaveConfig, readBuffer, I2C_TIMEOUT)
' Get MSB and LSB together
Dim temp As Integer = readBuffer(0)
temp <<= 8 ' zde je chyba, melo by byt <=
temp = temp Or readBuffer(1) ' smysl tohoto nechapu
' Negative value test (subzero temperature)
temp -= If((readBuffer(0) >= &H80), 65536, 0) 'tohle taky nechapu
' Get the temperature in Celsius
Return CSng((temp >> 4) * 0.0625) 'a zde je podle toho c# spravne, ze z nejvnorenejsi zavorky vyjde boolean, vynasobi se 0.0625 a prevede na single?
End Function
Dekuji za vysvetleni a objasneni
Adrian