请教一个类型转化的问题

chenxicheng 2012-08-22 03:17:22
基于OMRON Fins通信的实验,找了好几种方法,结果都遇到了vbstring在C#中类型不匹配的问题。
本人玩C#纯属业余爱好,想请大师们指点迷经。
//================================================================================================================
//VB6.0事件处理 OnReceive()如下,收发数据正常。

//Private Sub Command1_Click()
//FinsMsg1.sendFinsCommand 0, 1, 0, "0101820064000002"
//End Sub

//Private Sub FinsMsg1_OnReceive()
//Dim net As Integer, node As Integer, unit As Integer
//Text1.Text = FinsMsg1.receiveMessage(net, node, unit, vbString)
//End Sub
//================================================================================================================

private void button1_Click(object sender, EventArgs e)
{
axFinsMsg1.sendFinsCommand(0, 1, 0, "0101820064000002");
}
private void axFinsMsg1_OnReceive(object sender, EventArgs e)
{
//public virtual object receiveMessage(ref short netID, ref short nodeID, ref short unitID, object messageType)
//AxFINSMSGCTLLib.AxFinsMsg 的成员
short a=0,b=0,c=0;
object temp=new object();
textBox1.Text = axFinsMsg1.receiveMessage(ref a,ref b,ref c,temp);
//错误 1 无法将类型“object”隐式转换为“string”。存在一个显式转换(是否缺少强制转换?)
}
...全文
184 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
chenxicheng 2012-08-22
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
C# code
textBox1.Text = axFinsMsg1.receiveMessage(ref a,ref b,ref c,temp.toString());
[/Quote]
[Quote=引用 2 楼 的回复:]
receiveMessage是返回什么类型,如果是object,你需要 ToString(),才能赋值给Text
[/Quote]

不可以的,根据Fins软件提供的资料,返回值应该是:Variant类型。
另有另一种操作方法的帮助文档解释如下:
Definition Function readArea(Type As SysmacCS1_DataArea_Constants, Offset As Long, Size As Long, RetValType) 
Explanation Reads the PLC continuous I/O memory area.
Parameters Type Specifies the area type.
Offset Specifies the area offset.
Size Specifies the area size (number of words).
RetValType Specifies the return value type: character string, byte array, or Variant array.

Return Value The readArea method returns the read value in the type specified in the fourth parameter, stored as a Variant.


Example 1 - Read the data from DM 0 to 100, and store it in the Variant variable var, with subtype String:
Dim var As Variant

var = SYSMAC_CS11.readArea(plcAreaDM, 0, 100, vbString)



2 - Read 10 words of data starting from DM 0, and store it in the variable var, with subtype Variant array. Store the data from word 5 of the Variant array in the Long variable dm5:

Dim var As Variant
Dim dm5 As Long

var = SYSMAC_CS11.readArea(plcAreaDM, 0, 10, vbVariant Or vbArray)
dm5 = var(6)



Communication Communication is executed.
See Also FINS command 2301
bdmh 2012-08-22
  • 打赏
  • 举报
回复
receiveMessage是返回什么类型,如果是object,你需要 ToString(),才能赋值给Text
tudiy 2012-08-22
  • 打赏
  • 举报
回复
textBox1.Text = axFinsMsg1.receiveMessage(ref a,ref  b,ref c,temp.toString());

110,501

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧