我试图用C++/CLI应用程序向西门子PLC写入内容。Snap7写入S7-1200 PLC
阅读是可以的(除了第一次读取它给出奇数值)。
但写作正在做一些完全不同于我想要的东西。
下面你可以找到代码:
private: void WriteSiemensDB()
{
byte* buffer;
if (ConnectToSiemensPLC()) //Check if you are connected to PLC
{
String^ msg;
int DBNumber = 2;
bool NDR;
//Getting the values 1 time so buffer has a value
buffer = sPLC->ReadDB(DBNumber);
//give variables a value to write it to the PLC
NDR = true;
sPLC->SetBitAt(buffer, 0, 0, NDR); //Convert a bool to a bit
msg = sPLC->WriteDB(DBNumber, buffer); //write to the Datablock in Siemens
MessageBox::Show(msg); //Show if it worked or not
}
}
的sPLC-> SetBitAt方法:
void SiemensPLC::SetBitAt(byte buffer[], int Pos, int Bit, bool Value)
{
byte Mask[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
if (Bit < 0) Bit = 0;
if (Bit > 7) Bit = 7;
if (Value)
{
buffer[Pos] = (byte)(buffer[Pos] | Mask[Bit]);
}
else
{
buffer[Pos] = (byte)(buffer[Pos] & ~Mask[Bit]);
}
}
WriteDB方法:
System::String^ SiemensPLC::WriteDB(int DBnumber, byte buffer[])
{
int Result;
String^ msg;
Result = MyClient->DBWrite(DBnumber, 0, 80, buffer);
if (Result == 0)
{
msg = "Gelukt!"; //success
}
else
{
msg = "Mislukt, error:" + Result; //failed
}
return msg;
}
其实我得到的消息 “Gelukt” ,但它仍然写入rwong值。所以填写我的buffer
时出了点问题。我在缓冲区中做错了什么?
在C#我有同样的应用程序,除了缓冲器是byte buffer[];
我的问题是:
- 请告诉我在C
byte* buffer;
++和C#中的byte buffer[];
之间的区别? - 当我在调试时将鼠标悬停在缓冲区上时,它说
buffer* = 0 ''
。这是否意味着它是空的?如果是这样,为什么它仍然发送随机数到我的PLC?
烨我发现我有我的缓冲更改为阵,我已经做到这一点,它现在解决了。谢谢(你的)信息! :) – Bart