ARDUINO 模擬 PLC 回應 型式一(FORM1 資料格式)
練習測試 :開始試做 FORM1使用;VB6,ARDUINO UNO board,ARDUINO IDE開發環境,USB line
1.ARDUINO 端程式
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
while (!Serial) { // // Open serial communications and wait for port to open
} //otherwise the data will send and lost before you recieve
}
void loop() {
// put your main code here, to run repeatedly:
//Simulate PLC sned back the data-format as form1 spec.
// below we can get form1 data-format as STX+01FF10100100+ETX+73
char val; //define char,use print(char) will send out a char(like STX) not a number.
val=2;
Serial.print(val);
Serial.print("01FF10100100");
val=3;
Serial.print(val);
Serial.print("73");
while (true) ; // keep loop here, then loop will not send again again
// repeatly will casue com-buffer > form1 format
}
2.VB6 端 程式
注意:1. 請先加入MS comm 元件 專案\設定使用元件\Microsoft comm control 6.0(元件)
2. Comport setting 設定 一致;arduino 預設 9600,n,8,1
4. 元件 commPort 一致 ,從裝置管理員看.
數據解析 副程式=>並將燈號圖示進行顏色導通顯示(綠色)
Private Sub ParserData()Dim RcvBuf As String
Dim SumChkStrRcv As String
Dim SumChkStr As String
Dim H02Pos As Integer
Dim H03Pos As Integer
Dim i As Integer
'PLC 回傳數據處理 之 副程式
RcvBuf = Text9.Text
H02Pos = InStr(RcvBuf, Chr(2))
H03Pos = InStr(RcvBuf, Chr(3))
If H02Pos >= 1 And (H03Pos - H02Pos) >= (TxtRcvLen.Text - 3) Then
SumChkStrRcv = Mid(RcvBuf, H03Pos + 1, 2)
SumChkStr = chksum(Mid(RcvBuf, H02Pos + 1, H03Pos - 1))
If SumChkStrRcv = SumChkStr Then
For i = 0 To (Val(Text7.Text) - 1)
If Mid(RcvBuf, 6 + i, 1) = 1 Then
ShpX(i).FillColor = vbGreen
Else
ShpX(i).FillColor = &HC0C0C0
End If
Next
Text10.Text = 6 'ACK
' Else
' MsgBox "通信錯誤"
' Text10.Text = 21 'NAK
End If
Call Command3_Click
MSComm1.InBufferCount = 0
End If
End Sub
測試結果 可接收
