111,129
社区成员
发帖
与我相关
我的任务
分享Declare Function OpenCom Lib "S7_200_PPI.dll" (ByVal ComNo As Long) As Long
Declare Function CloseCom Lib "S7_200_PPI.dll" () As Long
Declare Function SetBaudRate Lib "S7_200_PPI.dll" (ByVal BaudRate As Long) As Long
Declare Function bitsetV Lib "S7_200_PPI.dll" (ByVal PLCaddr As Long, ByVal Vaddr As Long, ByVal bitNo As Long) As Long
Declare Function bitResetV Lib "S7_200_PPI.dll" (ByVal PLCaddr As Long, ByVal Vaddr As Long, ByVal bitNo As Long) As Long
Declare Function bit Lib "S7_200_PPI.dll" (ByVal AByte As Byte, ByVal bitNo As Long) As Boolean
Declare Function readVB Lib "S7_200_PPI.dll" (ByVal PLCaddr As Long, ByVal Vaddr As Long, ByVal length As Long, buff As Byte) As Long
Declare Function readVW Lib "S7_200_PPI.dll" (ByVal PLCaddr As Long, ByVal Vaddr As Long, ByVal length As Long, buff As Integer) As Long
Declare Function writeVB Lib "S7_200_PPI.dll" (ByVal PLCaddr As Long, ByVal Vaddr As Long, ByVal value As Byte) As Long
Declare Function writeVW Lib "S7_200_PPI.dll" (ByVal PLCaddr As Long, ByVal Vaddr As Long, ByVal value As Integer) As Long
Declare Function PLCRUN Lib "S7_200_PPI.dll" (ByVal PLCaddr As Long) As Long
Declare Function PLCSTOP Lib "S7_200_PPI.dll" (ByVal PLCaddr As Long) As Long
Declare Sub setReadDelay Lib "S7_200_PPI.dll" (ByVal ADelayTime As Long)
Declare Sub setOKDelay Lib "S7_200_PPI.dll" (ByVal ADelayTime As Long) Dim rxdata(1024) As Integer
Dim tmp As String
Text1 = ""
If readVW(2, Val(Text8), Val(Text10), rxdata(0)) > 0 Then
tmp = ""
For i = 0 To Val(Text10) - 1
tmp = tmp & rxdata(i) & ","
Next
Text1 = tmp
Else
Text1 = "error"
End Ifpublic class Api
{
private Api()
{
}
[DllImport("S7_200_PPI.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int OpenCom (Int32 ComNo);
[DllImport("S7_200_PPI.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int CloseCom ();
[DllImport("S7_200_PPI.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int SetBaudRate (Int32 BaudRate);
[DllImport("S7_200_PPI.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int bitsetV (Int32 PLCaddr, Int32 Vaddr, Int32 bitNo);
[DllImport("S7_200_PPI.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int bitResetV (Int32 PLCaddr, Int32 Vaddr, Int32 bitNo);
[DllImport("S7_200_PPI.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int bit (byte AByte, Int32 bitNo);
[DllImport("S7_200_PPI.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int readVB (Int32 PLCaddr, Int32 Vaddr, Int32 length,Byte buff);
[DllImport("S7_200_PPI.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int readVW(Int32 PLCaddr, Int32 Vaddr, Int32 length, int buff);
[DllImport("S7_200_PPI.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int writeVB (Int32 PLCaddr, Int32 Vaddr, byte value);
[DllImport("S7_200_PPI.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int writeVW (Int32 PLCaddr, Int32 Vaddr, int value);
[DllImport("S7_200_PPI.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int PLCRUN (Int32 PLCaddr);
[DllImport("S7_200_PPI.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int PLCSTOP (Int32 PLCaddr);
[DllImport("S7_200_PPI.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int setReadDelay (Int32 ADelayTime);
[DllImport("S7_200_PPI.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int setOKDelay(Int32 ADelayTime);
}//这句没问题
if (Api.OpenCom(5) > 0){
textBox1.Text = "打开成功";
}
else
{
textBox1.Text = "打开失败";
}//这个报错,由于是调用dll,无法捕获异常,竟然弹出APPCRASH问题(vb下没问题)……
int[] rxdata = new int[1024];
string tmp;
textBox1.Text = "";
if (Api.readVW(2, 40, 10, rxdata[0]) > 0)
{
tmp = "";
for (int i = 0; i < 9; i++)
{
tmp = tmp+rxdata[i].ToString()+",";
}
}
else{
tmp = "读取错误";
}
textBox1.Text =tmp;
。
我刚开始理解为从数组0依次写入,所以直接Api.readVW(2, 40, 10, rxdata[0]),谢谢,问题解决!
谢了,已解决。Declare Function readVW Lib "S7_200_PPI.dll" (ByVal PLCaddr As Long, ByVal Vaddr As Long, ByVal length As Long, buff As Integer) As Long
[DllImport("S7_200_PPI.dll", CallingConvention = CallingConvention.StdCall)]
public static extern short readVW(Int32 PLCaddr, Int32 Vaddr, Int32 length, short[] buff);
我拿这个说明下吧
vb里long对应c#int / Int32
vb里integer对应c#short / Int16
像buff这个参数是byref,对应到c#里直接用数组就行[DllImport("S7_200_PPI.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int readVW(Int32 PLCaddr, Int32 Vaddr, Int32 length, IntPtr buff);
调用:
IntPtr p = Marshal.AllocHGlobal(1024);
if (Api.readVW(2, 40, 10, p) > 0)
{
byte[] data = new byte[1024];
Marshal.Copy(p, data, 0, 1024);
......
}
先这样试试。