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
測試結果 可接收
應用思考題:
1.輪換模擬 ,PLC 回餽給 ARDUINO(PLC) 解析X,Y,M 等BIT 元件狀態2.ARDUINO 上建構 LED 燈 來顯示輸出 Y0~Y7 八個輸出狀態?
3.如果加上LED顯示Y0~Y17如何顯示?PIN腳位是否夠用?是否可用LCD來顯示?用擴充版?
4.如和提供者 動態輸入 想要的X 接點或 Y接點?
5. word 的元件的操作呢?
6.使用FX3U 跟 FX2N 是否一樣? 要如何設定?
7.BAUDRATE 同位元 停止位元 資料位元 等參數如何動態變化來適應不同機台?
沒有留言:
張貼留言