简体   繁体   English

如何连接多个gsm调制解调器并获得所有调制解调器的响应

[英]how to connect multiple gsm modem and get response of all modem

hi i have following code to connect gsm modem using com port this code works fine on single modem i have more than 10 modems.i want to connect all modems and get response of every modem SerialDataReceiveEvent Serprately question is i need to paste this code 10 times or any other easy way to connect all modems at same time and get every modem response seprately. 您好,我有以下代码使用com端口连接gsm调制解调器,该代码在单个调制解调器上可以正常工作,我有10个以上的modems。我想连接所有调制解调器,并获取每个调制解调器的响应SerialDataReceiveEvent问题是我需要粘贴此代码10次或同时连接所有调制解调器并单独获得每个调制解调器响应的任何其他简便方法。

Public WithEvents DataPort As New IO.Ports.SerialPort
 Public Function ConnectPort(ByVal PrtName As String)
    Try

        DataPort.PortName = PrtName
        DataPort.BaudRate = 115200
        DataPort.Parity = IO.Ports.Parity.None
        DataPort.StopBits = IO.Ports.StopBits.One
        DataPort.DataBits = 8
        DataPort.Open()
        AddHandler Me.DataPort.DataReceived, New  SerialDataReceivedEventHandler(AddressOf comPort_DataReceived)

        Catch ex As Exception
        MsgBox(ex.ToString)
    End Try

   Public Sub comPort_DataReceived(ByVal sender As Object, ByVal e As  SerialDataReceivedEventArgs)
    Dim RecievedMessage As String = DataPort.ReadExisting()
    ModemResponse(RecievedMessage)
    End Sub

I believe you can connect multiple modem, for that you can make your application multithreaded. 我相信您可以连接多个调制解调器,因此可以使您的应用程序成为多线程。 In this case each thread will represent to one modem object but will share the same code also. 在这种情况下,每个线程将代表一个调制解调器对象,但也将共享相同的代码。

You can copy and paste the code 10 times (changing the port number/name each time), but a better approach would be to use a for loop to create the 10 ports. 您可以复制和粘贴代码10次(每次更改端口号/名称),但是更好的方法是使用for循环创建10个端口。 You then need your event handler to know which port the data has arrived from (because your single handler will be called by allthe ports) - you can use sender or possibly may be able to get the port number from the event args. 然后,您需要事件处理程序知道数据从哪个端口到达(因为所有端口都将调用您的单个处理程序)-您可以使用sender或可能能够从事件args获取端口号。

Note that there is no need to deliberately make your code 'multi threaded' because the serial port will do this for you - the event handler will be called back on different thread(s) as data arrives. 请注意,无需故意将代码设置为“多线程”,因为串行端口将为您执行此操作-事件处理程序将在数据到达时在不同线程上被回调。 As long as your event handler doesn't do anything non-thread-safe (such as storing data from different ports in the same place) you won't need to do anything specific to make it multithreaded. 只要事件处理程序不执行任何非线程安全的操作(例如,将来自不同端口的数据存储在同一位置),您就不需要执行任何特定操作来使其成为多线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM