我在VB中有源代码,我想用C#转换它来发送命令&字符串到COM1。这样做的C#代码是什么?
VB6中的代码:
Comm1.Output = Chr$(&H5)
Comm1.Output = Chr$(&H2) + Chr$(&H10) + Chr$(&H31) + msg + Chr$(&H10) + Chr$(&H32) + msg2 + Chr$(&H3)
答案 0 :(得分:1)
char char2 = (char)int.Parse("2", NumberStyles.AllowHexSpecifier);
char char3 = (char)int.Parse("3", NumberStyles.AllowHexSpecifier);
char char5 = (char)int.Parse("2", NumberStyles.AllowHexSpecifier);
char char10 = (char)int.Parse("10", NumberStyles.AllowHexSpecifier);
char char31 = (char)int.Parse("31", NumberStyles.AllowHexSpecifier);
char char32 = (char)int.Parse("32", NumberStyles.AllowHexSpecifier);
Comm1.Write(char5);
Comm1.Write(string.Format("{0}{1}{2}{3}{4}{5}{6}{7}", char2, char10, char31, msg, char10, char32, msg2, char3));
没有提到如何初始化串口,我的假设是已经正确。如果没有,则有articles to read on the subject.