PC的COM4连三菱PLC,COM7连扫描枪,单独打开一个串口能正常工作,两个串口无法同时打开。与三菱PLC通信的程序集成在.DLL文件里,命名空间为FXPLC_COMM。
怎么样才能两个串口同时打开。要用两个线程吗?
Option Strict On
Imports System
Imports System.IO.Ports
Imports FXPLC_COMM
Public Class Form1
Dim FxPLC As New FX_PLC
Dim FxData As FX_DATA
Dim IsPLCComOpen As Boolean
Dim IsPLCComClose As Boolean
Const Output_On As OnOffFlags = OnOffFlags.fxOnStatus
Const Output_OFF As OnOffFlags = OnOffFlags.fxOffStatus
Dim SelectBarcode As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'获取计算机有效串口
Dim ports As String() = SerialPort.GetPortNames() '必须用命名空间,用SerialPort,获取计算机的有效串口
Dim port As String
For Each port In ports
cmbPortName.Items.Add(port) '向combobox中添加项
Next port
'初始化界面
cmbBaudrate.SelectedIndex = 2
cmbPortName.SelectedIndex = 0
Serial_Port1() '初始化串口
Label3.Text = SerialPort1.IsOpen.ToString
lblStatusOfSerialPort.Text = "串口未连接"
lblStatusOfSerialPort.ForeColor = Color.Red
' baudratebox.Text = baudratebox.Items(0) 注释和不注释的地方可以替换
'portnamebox.Text = portnamebox.Items(0)
End Sub
Private Sub Serial_Port1() '设置串口参数
SerialPort1.BaudRate = CInt(Val(cmbBaudrate.Text)) '波特率 'Val()以适当类型的数值形式返回包含于字符串内的数字。
SerialPort1.PortName = cmbPortName.Text '串口名称
SerialPort1.DataBits = 7 '数据位
SerialPort1.StopBits = IO.Ports.StopBits.One '停止位
SerialPort1.Parity = IO.Ports.Parity.None '校验位
End Sub
'关闭串口连接
Private Sub closebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles closebtn.Click
Try
SerialPort1.Close() '关闭串口
Label3.Text = SerialPort1.IsOpen.ToString
If SerialPort1.IsOpen = False Then
lblStatusOfSerialPort.Text = "串口未连接"
lblStatusOfSerialPort.ForeColor = Color.Red
receivebox.Text = ""
receivebytes.Text = ""
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
'打开串口连接
Private Sub openbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles openbtn.Click
Try
SerialPort1.Open() '打开串口
Label3.Text = SerialPort1.IsOpen.ToString
If SerialPort1.IsOpen = True Then
lblStatusOfSerialPort.Text = "串口已连接"
lblStatusOfSerialPort.ForeColor = Color.Green
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
'发送数据,不需要用到
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles closebtn.Click
Try
'SerialPort1.Write(txtDataSendBox.Text)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
'触发接收事件
Public Sub Sp_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Me.Invoke(New EventHandler(AddressOf Sp_Receiving)) '调用接收数据函数
End Sub
'接收数据
Private Sub Sp_Receiving(ByVal sender As Object, ByVal e As EventArgs)
Dim strIncoming As String
Try
receivebytes.Text = Str(Val(receivebytes.Text) + SerialPort1.BytesToRead)
If SerialPort1.BytesToRead > 0 Then
Threading.Thread.Sleep(100) '添加的延时
strIncoming = SerialPort1.ReadExisting.ToString '读取缓冲区中的数据
SerialPort1.DiscardInBuffer()
receivebox.Text = strIncoming
listbox1.Items.Add(strIncoming)
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
'处理PLC比对()
'Select Case SelectBarcode
' Case 0
' If receivebox.Text = txtBarcode1.Text Then
' '输出Y0=1
' SetOutput(Output_On, DeviceName.Y, 0)
' Else
' 'y0=0
' SetOutput(Output_OFF, DeviceName.Y, 0)
' End If
' Case 1
' If receivebox.Text = txtBarcode2.Text Then
' '输出Y0=1
' SetOutput(Output_On, DeviceName.Y, 0)
' Else
' 'Y0=0
' SetOutput(Output_OFF, DeviceName.Y, 0)
' End If
'End Select
End Sub
Private Sub btnLearnBarcode1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLearnBarcode1.Click
txtBarcode1.Text = receivebox.Text
End Sub
Private Sub btnLearnBarcode2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLearnBarcode2.Click
txtBarcode2.Text = receivebox.Text
End Sub
'Open PLC COM Port
Private Sub btnPLCComPortOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPLCComPortOpen.Click
Try
IsPLCComOpen = FxPLC.ComOpen(Com_Number.COM4)
If IsPLCComOpen = True Then
lblIsPLCComPortOpen.Text = "串口已打开"
lblIsPLCComPortOpen.ForeColor = Color.LightBlue
End If
Catch ex As Exception
MessageBox.Show("串口打开时出错," & ex.Message)
End Try
End Sub
'Close PLC Com port
Private Sub btnPLCComPortClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPLCComPortClose.Click
Try
IsPLCComClose = FxPLC.ComClose()
If IsPLCComClose = True Then
lblIsPLCComPortOpen.Text = "串口已关闭"
lblIsPLCComPortOpen.ForeColor = Color.Red
End If
Catch ex As Exception
MessageBox.Show("串口关闭时出错," & ex.Message)
End Try
End Sub
Private Sub radBarCode1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radBarCode1.CheckedChanged
SelectBarcode = 0
End Sub
Private Sub radBarcode2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles radBarcode2.CheckedChanged
SelectBarcode = 0
End Sub
'set PLC output
Public Sub SetOutput(ByVal On_Off As OnOffFlags, ByVal a As DeviceName, ByVal b As Short)
Dim back As Boolean
Try
back = FxPLC.Device_SingleForce(On_Off, a, b)
Catch ex As Exception
MessageBox.Show("SET Output error" & ex.Message)
End Try
End Sub
End Class
FORM
------解决思路----------------------
要打开2个 就用2个SerialPort对象。
下面那些没关系的,重新生成一下也许就没有了。不影响
------解决思路----------------------
Why里,有答案了吗?