VS2008 VB.Net
کد:
Imports System.IO.Ports
Public Class Form1
Private Function answer(ByVal sp As SerialPort)
Dim okmsg() As Char = "B"
sp.Write(okmsg, 0, okmsg.Length)
End Function
Private Function DetectSerialPort()
Dim testport As New SerialPort
Dim portnames() As String
Dim bauds() As Integer = {4800, 9600, 19200, 38400, 76800, 115200}
Dim readbuf As Byte
portnames = SerialPort.GetPortNames()
For Each portname In portnames
For Each baud In bauds
testport = New SerialPort(portname, baud)
If testport.IsOpen = False Then
testport.ReadTimeout = 200
testport.Open()
Try
readbuf = testport.ReadByte()
If (readbuf = 65) Then
answer(testport)
'sleep 1 sec
testport.ReadExisting()
'clear buff
Return testport
End If
testport.Close()
Catch ex As TimeoutException 'not our port
testport.Close()
Continue For
End Try
End If
Next
Next
Return Nothing
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim sp As SerialPort = DetectSerialPort()
If sp Is Nothing Then
MessageBox.Show("our device not attached")
End If
End Sub
End Class
AVR-GCC
کد:
while (1)
{
USARTWriteChar(65);
readbuf1 = USARTRead_USARTBuffer();
if(readbuf1 == 'B')
break;
}
*تنظیمات پرت باز شده، امکان override شدن در .net ندارن، پرت باید بست با تنظیمات جدید باز کرد، مگر اینکه buffer و پروتوکل و ... , همه اینها رو خودتون از صفر پیاده سازی کنین.
*این روش تابع سخت افزار ارتباطی هم هست، قبلا هم گفتن با FT ها، بخصوص driver های قدیم مشکل داره، چون اگه buffer شون با باود و تنظیمات اشتباه شروع به پر شدن بکنه، تنها راه ریست کردن بود(درست یادم نیست چی بود مشکل)