Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > .NET Framework > Forum > ติดปัญหา Convert ข้อมูลเป็น Int จาก Serial Port ครับ



 

ติดปัญหา Convert ข้อมูลเป็น Int จาก Serial Port ครับ

 



Topic : 130493



โพสกระทู้ ( 5 )
บทความ ( 0 )



สถานะออฟไลน์
Facebook



พอดีผมรับค่าจาก Serial Port จากฝั่ง Arduino แล้วเกิดปัญหา Convert ไม่ได้ครับ บางครั้งยอมให้รันโปรแกรมและแปลงได้ได้ บางครั้งไม่ยอมให้รันโปรแกรม เกิดจากสาเหตุอะไรครับ พอมีวิธีแก้ไหมครับ ขอบคุณครับ




Code
Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

CheckForIllegalCrossThreadCalls = False
Dim lists As String() = IO.Ports.SerialPort.GetPortNames
For i = 0 To lists.Count - 1
ComboBox1.Items.Add(lists(i))

Next
End Sub


Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

Dim line As String
Dim sensor(6) As Integer
Dim myLabel = New Label(5) {Label0, Label1, Label2, Label3, Label4, Label5}

If SerialPort1.BytesToRead > 0 Then
line = SerialPort1.ReadLine
' Labeltest.Text = line
line = line.Trim

If line = "ARDUINO_SEND" Then
For i As Integer = 0 To 5
line = SerialPort1.ReadLine
line.Trim()
sensor(i) = Convert.ToInt32(line)
If sensor(i) > 25 Then
myLabel(i).Text = "ว่าง"
myLabel(i).ForeColor = Color.Green
Else
myLabel(i).Text = "ไม่ว่าง"
myLabel(i).ForeColor = Color.Red
End If

If sensor(0) > 25 Then
PictureBox1.ImageLocation = "C:\Users\Jame\Desktop\Project\pic\p1.png"
Else
PictureBox1.ImageLocation = "C:\Users\Jame\Desktop\Project\pic\p1car.png"
End If
' If sensor(1) > 25 Then
'PictureBox2.ImageLocation = "C:\Users\Jame\Desktop\Project\pic\p2.png"
'Else
'PictureBox2.ImageLocation = "C:\Users\Jame\Desktop\Project\pic\p2car.png"
'End If
Next
End If

End If
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If SerialPort1.IsOpen Then SerialPort1.Close()
If Button1.Text = "Connect" Then
SerialPort1.BaudRate = 9600
SerialPort1.PortName = ComboBox1.SelectedItem
Try
Timer1.Enabled = True
Timer1.Interval = 1000
SerialPort1.Open()
Button1.Text = "Disconnect"
ComboBox1.Enabled = False
Catch ex As Exception
MsgBox(ex.Message)
End Try
Else
Button1.Text = "Connect"
ComboBox1.Enabled = True
End If
End Sub
End Class




Tag : .NET, VB.NET, VS 2010 (.NET 4.x)







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2018-03-07 12:46:07 By : 10204639104093711 View : 1469 Reply : 2
 

 

No. 1



โพสกระทู้ ( 4,434 )
บทความ ( 23 )



สถานะออฟไลน์
Facebook

ดูค่าที่ได้จาก SerialPort1 ดูครับ ว่ามันได้ค่าเป็นอะไร

ดึงออกมาลงใน textbox เลยโดยไม่ต้องแปลงครับ






แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2018-03-07 15:19:49 By : lamaka.tor
 


 

No. 2

Guest


ไม่แน่ใจว่าเป็นเพราะ port นั้นอยู่ระหว่างทำงานอยู่หรือเปล่าครับ ควรจะ Delegate Event เพื่อมารับค่าต่างหาก (ไม่ค่อยรู้เรื่องเท่าไรนะครับ)
อันนี้แปรงมาจาก C# ไม่แน่ใจว่ามี Error ตรงไหนหรือเปล่านะครับ

Code (VB.NET)
Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
            Try
                Dim sp As SerialPort = CType(sender, SerialPort)
                Dim RecievedData As String               
                RecievedData = sp.ReadExisting()
                If Not(RecievedData = "") Then
                    Me.BeginInvoke(New LineReceivedEvent(AddressOf LineReceived), RecievedData)
                End If
            Catch __unusedException1__ As Exception
                Console.WriteLine("Error !!!")
            End Try
        End Sub

        Private Delegate Sub LineReceivedEvent(ByVal line As String)

        Private Sub LineReceived(ByVal line As String)
            MessageBox.Show(line)
            Thread.Sleep(1000)
        End Sub

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2018-03-07 16:01:23 By : บัญดิษฐ
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : ติดปัญหา Convert ข้อมูลเป็น Int จาก Serial Port ครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 04
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่