我正在开发一个从串口读取数据的简单项目。事实上,我可以正确地获取visual basic(VB)中的数据。现在我尝试使用c#创建它,但问题是,我无法读取数据,就像我在VB中读到的那样。如何将我的VB代码转换为C sharp?
Dim buf As Variant
Dim data(5) As Variant
Dim data_dorost(3) As Variant
Dim counter As Variant
Dim cs As Variant
Dim sending As Boolean
Private Sub Form_Load()
MSComm1.Settings = "9600,N,8,1"
MSComm1.RThreshold = 1
MSComm1.SThreshold = 1
MSComm1.InputLen = 1
MSComm1.PortOpen = True
End Sub
Private Sub MSComm1_OnComm()
Select Case MSComm1.CommEvent
Case comEvReceive
buf = MSComm1.Input
buf = Asc()
Debug.Print buf
If buf = &HEB And counter = 0 Then
counter = 1
data(counter) = buf
cs = buf
Exit Sub
End If
If counter <> 0 Then
counter = counter + 1
data(counter) = buf
If counter = 2 Then
Select Case data(2)
Case 1:
r1.BackColor = RGB(0, 255, 0)
Text1.Text = "ON"
Case 2:
r1.BackColor = RGB(255, 0, 0)
Text1.Text = "OFF"
Case 3:
r2.BackColor = RGB(0, 255, 0)
Text2.Text = "ON"
Case 4:
r2.BackColor = RGB(255, 0, 0)
Text2.Text = "OFF"
Case 5:
r3.BackColor = RGB(0, 255, 0)
Text3.Text = "ON"
Case 6:
r3.BackColor = RGB(255, 0, 0)
Text3.Text = "OFF"
Case 7:
r4.BackColor = RGB(0, 255, 0)
Text4.Text = "ON"
Case 8:
r4.BackColor = RGB(255, 0, 0)
Text4.Text = "OFF"
Case 9:
r5.BackColor = RGB(0, 255, 0)
Text5.Text = "ON"
Case 10:
r5.BackColor = RGB(255, 0, 0)
Text5.Text = "OFF"
Case 11:
r6.BackColor = RGB(0, 255, 0)
Text6.Text = "ON"
Case 12:
r6.BackColor = RGB(255, 0, 0)
Text6.Text = "OFF"
Case 13:
r7.BackColor = RGB(0, 255, 0)
Text7.Text = "ON"
Case 14:
r7.BackColor = RGB(255, 0, 0)
Text7.Text = "OFF"
Case 15:
r8.BackColor = RGB(0, 255, 0)
Text8.Text = "ON"
Case 16:
r8.BackColor = RGB(255, 0, 0)
Text8.Text = "OFF"
Case 17:
r9.BackColor = RGB(0, 255, 0)
Text9.Text = "ON"
Case 18:
r9.BackColor = RGB(255, 0, 0)
Text9.Text = "OFF"
Case 19:
r10.BackColor = RGB(0, 255, 0)
Text10.Text = "ON"
Case 20:
r10.BackColor = RGB(255, 0, 0)
Text10.Text = "OFF"
Case 21:
r11.BackColor = RGB(0, 255, 0)
Text11.Text = "ON"
Case 22:
r11.BackColor = RGB(255, 0, 0)
Text11.Text = "OFF"
Case 23:
r12.BackColor = RGB(0, 255, 0)
Text12.Text = "ON"
Case 24:
r12.BackColor = RGB(255, 0, 0)
Text12.Text = "OFF"
Case 25:
r13.BackColor = RGB(0, 255, 0)
Text13.Text = "ON"
Case 26:
r13.BackColor = RGB(255, 0, 0)
Text13.Text = "OFF"
Case 27:
r14.BackColor = RGB(0, 255, 0)
Text14.Text = "ON"
Case 28:
r14.BackColor = RGB(255, 0, 0)
Text14.Text = "OFF"
Case 29:
r15.BackColor = RGB(0, 255, 0)
Text15.Text = "ON"
Case 30:
r15.BackColor = RGB(255, 0, 0)
Text15.Text = "OFF"
Bobi, [07.08.17 11:47]
Case 31:
r16.BackColor = RGB(0, 255, 0)
Text16.Text = "ON"
Case 32:
r16.BackColor = RGB(255, 0, 0)
Text16.Text = "OFF"
End Select
counter = 0
If sending = True Then
sending = False
F_BYTE = &HAA
cs = F_BYTE
MSComm1.Output = Chr$(F_BYTE)
MSComm1.Output = Chr$(setpoint_change.Text)
cs = F_BYTE + setpoint_change.Text
cs = cs And 255
MSComm1.Output = Chr$(cs)
End If
End If
cs = cs + buf
End If
End Select
End Sub
Private Sub setpoint_change_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then sending = True
End Sub
这是我的c#代码的一部分:
void _serialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int dataLength = _serialPort.BytesToRead;
byte[] data = new byte[dataLength];
int nbrDataRead = _serialPort.Read(data, 0, dataLength);
if (nbrDataRead == 0)
return;
// Send data to whom ever interested
if (NewSerialDataRecieved != null)
NewSerialDataRecieved(this, new SerialDataEventArgs(data));
}
这里我在文本框中显示数据:
int maxTextLength = 1000; // maximum text length in text box
if (tbData.TextLength > maxTextLength)
tbData.Text = tbData.Text.Remove(0, tbData.TextLength - maxTextLength);
// This application is connected to a GPS sending ASCCI characters, so data is converted to text
string str = Encoding.ASCII.GetString(e.Data);
//end new
//tbData.AppendText(e.Data[0].ToString());
String strTemp0 = e.Data[0].ToString();
tbData.AppendText(strTemp0);
什么是正确的等效C#代码?
答案 0 :(得分:1)
您没有在C#中提供COM初始化的源代码,但如果它是正确的,则问题可能出在您的接收函数中。你假设你总是收到完整的数据字符串,但事实并非如此。
如果您的数据不止一个字符,那么您的回调将按部分接收数据。