【发布时间】:2016-02-28 10:14:25
【问题描述】:
我正在尝试使用 C++/CLI 应用程序向 Siemens 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 发送随机数?
【问题讨论】:
标签: c++-cli plc s7-1200 siemens