![]() |
|
Tài trợ cho PIC Vietnam |
Giao tiếp cổng COM và LPT RS232, RS485 và LPT là những giao tiếp cơ bản và kinh điển khi mới học về vi điều khiển... |
|
Ðiều Chỉnh | Xếp Bài |
![]() |
#1 |
Nhập môn đệ tử
Tham gia ngày: Jun 2011
Bài gửi: 1
: |
Xin giúp chuyển đoạn code VB sau sang C#.
Chả là đoạn code sau viết để nhận dữ liệu từ đầu cân Avery Berkel L126 để hiển thị lên màn hình, nhưng người viết chẳng phải là em, híc máy em bị virus thế là file biên dịch cũng tèo. Bi giờ phần mềm chẳng chạy được. Chỉ còn lại đoạn code này mà em thì chẳng hiểu tí gì về VB.
Bác nào làm ơn giúp em chuyển đoạn code này sang C# với vì cái đó thì em biết. Cảm ơn các bác rất nhiều hic. Đoạn code như sau: VERSION 5.00 Object = "{648A5603-2C6E-101B-82B6-000000000014}#1.1#0"; "MSCOMM32.OCX" Begin VB.Form Form1 ClientHeight = 1155 ClientLeft = 60 ClientTop = 60 ClientWidth = 4395 ControlBox = 0 'False LinkTopic = "Form1" ScaleHeight = 1155 ScaleWidth = 4395 StartUpPosition = 2 'CenterScreen WindowState = 1 'Minimized Begin VB.Timer Timer1 Interval = 220 Left = 360 Top = 240 End Begin MSCommLib.MSComm MSComm1 Left = 1080 Top = 240 _ExtentX = 1005 _ExtentY = 1005 _Version = 393216 DTREnable = 0 'False BaudRate = 4800 ParitySetting = 2 DataBits = 7 End Begin MSCommLib.MSComm MSComm2 Left = 3120 Top = 0 _ExtentX = 1005 _ExtentY = 1005 _Version = 393216 DTREnable = 0 'False InBufferSize = 40 ParityReplace = 0 BaudRate = 2400 ParitySetting = 2 DataBits = 7 End Begin VB.Label Label4 Caption = "Toledo" Height = 255 Left = 720 TabIndex = 3 Top = 0 Width = 495 End Begin VB.Label Label3 Caption = "L126" Height = 255 Left = 2520 TabIndex = 2 Top = 0 Width = 495 End Begin VB.Label Label2 Alignment = 2 'Center BackStyle = 0 'Transparent BorderStyle = 1 'Fixed Single Caption = "12345" BeginProperty Font Name = "VNI-Times" Size = 36 Charset = 0 Weight = 700 Underline = 0 'False Italic = 0 'False Strikethrough = 0 'False EndProperty ForeColor = &H00FF0000& Height = 915 Left = 2280 TabIndex = 1 Top = 240 Width = 1995 End Begin VB.Label Label1 Alignment = 2 'Center BackStyle = 0 'Transparent BorderStyle = 1 'Fixed Single Caption = "12345" BeginProperty Font Name = "VNI-Times" Size = 36 Charset = 0 Weight = 700 Underline = 0 'False Italic = 0 'False Strikethrough = 0 'False EndProperty ForeColor = &H00FF0000& Height = 915 Left = 120 TabIndex = 0 Top = 240 Width = 1995 End End Attribute VB_Name = "Form1" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = False Public chuoi As String Public kt_com1, kt_com2 As Integer Dim chuoi_t2, chuoi_tt2, sub_string2 As String Dim filename1 As String Dim filename2 As String Dim port1, port2 As Boolean Private Sub Command1_Click() End End Sub Private Sub Form_Load() On Error GoTo loi Dim thefile As Integer Dim chuoi_tt2 As String chuoi_tt2 = Space(20) port1 = True port2 = True thefile = 1 Open App.Path & "\COMM.TXT" For Input As #thefile Line Input #thefile, chuoi Close #thefile MSComm1.Settings = Trim(Left(chuoi, 10)) MSComm2.Settings = Trim(Left(chuoi, 10)) MSComm1.CommPort = Trim(Mid(chuoi, 12, 1)) kt_com1 = Val(Mid(chuoi, 14, 1)) MSComm2.CommPort = Trim(Mid(chuoi, 16, 1)) kt_com2 = Val(Mid(chuoi, 18, 1)) chuoi = Trim(Mid(chuoi, 20)) If MSComm1.PortOpen = False Then MSComm1.PortOpen = True End If If MSComm2.PortOpen = False Then MSComm2.PortOpen = True End If loi: If Err.Number = 8005 Then MsgBox "Port ready", vbInformation, "Thong bao" End End If End Sub Private Sub MSComm2_OnComm() On Error GoTo loighi Static receive_str2 As String ' If MSComm2.PortOpen = False Then ' MSComm2.PortOpen = True ' End If If MSComm2.InBufferCount > 1 Then chuoi_tt2 = MSComm2.Input Do While (Len(chuoi_tt2) > 1 And Asc(chuoi_tt2) <> Asc("G")) chuoi_tt2 = Mid((chuoi_tt2), 2, Len(chuoi_tt2) - 1) Loop ' enddo If Len(Trim(chuoi_tt2)) >= 10 Then ''And Mid(Trim(chuoi_tt2), 10, 1) = "k" Then receive_str2 = Mid((Trim(chuoi_tt2)), kt_com2, 5) Label2.Caption = Trim(receive_str2) Open filename2 For Output As #1 Print #1, Trim(receive_str2) Close #1 ' chuoi_tt2 = Mid(chuoi_tt2, 36) End If End If loighi: Exit Sub End Sub Private Sub Timer1_Timer() On Error GoTo loighi Static receive_str1 As String Dim chuoi_t1, chuoi_tt1, sub_string1 As String filename1 = chuoi & "\Data1.txt" filename2 = chuoi & "\Data2.txt" If MSComm1.PortOpen = False Then MSComm1.PortOpen = True End If receive_str1 = "" receive_str1 = Left(MSComm1.Input, 36) 'If receive_str1 = "" And port1 = True Then ' MsgBox "cong 1 khong nhan duoc so can", vbInformation, "Thong bao" ' Label1.Caption = "error" ' Open filename1 For Output As #1 ' Print #1, 0 ' Close #1 'port1 = False 'Else ' If receive_str1 <> "" Then ' port1 = True ' End If 'End If If receive_str1 <> "" Then For i = 1 To 16 chuoi_t1 = Mid(receive_str1, i, 1) If chuoi_t1 = Chr(2) Then sub_string1 = Mid(receive_str1, i) If Len(sub_string1) >= 13 Then chuoi_tt1 = Mid(sub_string1, kt_com1, 5) Label1.Caption = chuoi_tt1 Open filename1 For Output As #1 Print #1, chuoi_tt1 Close #1 End If Exit Sub Else i = i + 1 End If Next i End If ''''' loighi: Exit Sub End Sub Private Sub Timer2_Timer() End Sub |
![]() |
![]() |
|
|