 |
|
สอบถามโปรแกรมอ่าน serial port หน่อยครับ พอใช้ไปนานๆ แล้วกดปิดมันค้างครับ |
|
 |
|
|
 |
 |
|
คือผมทำดปรแกรมอ่าน นน. จากเครื่องชั่งผ่านสาย RS232
โค้ดตามด้านล่างครับ
มันอ่านไม่หยุดเลยคับ
อยากจะให้มัน รับค่า + เขียนไฟล์ ทุกๆ 2 วินาทีนี้ต้องทำยังไงครับ
มันอ่านรวดดดดด แล้วพอเปิดใช้งานๆ มากดปิด มันค้างครับ
Code (VB.NET)
Imports System
Imports System.Threading
Imports System.IO.Ports
Imports System.ComponentModel
Imports System.IO
Public Class Form1
'------------------------------------------------
Dim myPort As Array
Dim filePath As String
Delegate Sub SetTextCallback(ByVal [text] As String) 'Added to prevent threading errors during receiveing of data
'------------------------------------------------
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
SerialPort1.PortName = "COM1"
SerialPort1.BaudRate = "9600"
SerialPort1.Open()
'myPort = IO.Ports.SerialPort.GetPortNames()
'ComboBox1.Items.AddRange(myPort)
Timer1.Enabled = True
Timer1.Interval = 1000
Dim Weight As String = RichTextBox2.Text.Length()
TextBox1.Text = Weight
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
SerialPort1.Write(RichTextBox2.Text & vbCr) 'concatenate with \n
'SaveTextToFile(RichTextBox2.Text, "D:\weight.txt")
filePath = System.IO.Path.Combine(
My.Computer.FileSystem.SpecialDirectories.MyDocuments, "weight.txt")
My.Computer.FileSystem.WriteAllText(filePath, RichTextBox2.Text, False)
Dim Weight As String = RichTextBox2.Text.Length()
If Weight > 400 Then
RichTextBox2.Text = ""
End If
End Sub
Private Sub SerialPort1_DataReceived(ByVal sender As System.Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
ReceivedText(SerialPort1.ReadExisting())
End Sub
Private Sub ReceivedText(ByVal [text] As String) 'input from ReadExisting
If Me.RichTextBox2.InvokeRequired Then
Dim x As New SetTextCallback(AddressOf ReceivedText)
Me.Invoke(x, New Object() {(text)})
Else
Me.RichTextBox2.Text &= [text] 'append text
End If
End Sub
Public Function SaveTextToFile(ByVal strData As String, _
ByVal FullPath As String, _
Optional ByVal ErrInfo As String = "") As Boolean
'Dim Contents As String
Dim bAns As Boolean = False
Dim objReader As StreamWriter
Try
objReader = New StreamWriter(FullPath)
objReader.Write(strData)
objReader.Close()
bAns = True
Catch Ex As Exception
ErrInfo = Ex.Message
End Try
Return bAns
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Timer1.Stop()
Timer1.Enabled = False
If SerialPort1.IsOpen Then
SerialPort1.Close()
End If
Application.Exit()
End Sub
End Class
รบกวนหน่อยครับ ใครพอมีแนวทางบ้าง
Tag : .NET, VB.NET
|
|
 |
 |
 |
 |
Date :
2018-06-20 16:36:40 |
By :
ilikeit |
View :
2614 |
Reply :
5 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เข้าใจว่าใช้ Timer แล้วมันน่าจะทำงานเป็น Loop ครับ แล้วน่าจะมีปัญหาเรื่องการเชื่อมต่อ Open -> Close ลองเช็คดูดีๆ ว่ามีการเปิด/ปิด ครบหรือเปล่าครับ 
|
 |
 |
 |
 |
Date :
2018-06-21 13:01:40 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับ ถ้าเปิดโปรแกรมไม่นาน แล้วกดปิด มันจะทำงานได้ครับ ถ้าเปิดไว้นานๆ แล้วกดปุ่มปิด มันจะค้างเลยครับ
|
 |
 |
 |
 |
Date :
2018-06-22 14:45:02 |
By :
ilikeit |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
แนะนำด้วยค่ะ อยากรู้เหมือนกัน
|
 |
 |
 |
 |
Date :
2019-08-09 10:45:26 |
By :
daino_tuckky |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ผมคิดว่า น่าจะเกิดการวนลูปมากเกินไปใน timer แล้วไม่ได้เครียค่า ครับ ลอง .Dispose ดูครับ
|
 |
 |
 |
 |
Date :
2019-08-13 09:03:46 |
By :
2127832830566218 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|