• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2385
  • Last Modified:

Serial port rs232 comunication bi-directional mode maby php and visual basic

Hi, I need some Best ideas for a project that I am building,

I need create a standart way software solution to comunicate with many medicaL instruments,
each instruments have their own standart data code comunication,

the protocol comunication to all instruments is the same:
RS232 NULL MODEM db9 or db25 PINOUT, I have not ptoblems with the phical layer.

how this work:
intrument ask to pc for a worklist, pc send worklist data
when the instrument has finished all worklist task send the results back to pc.

the solutions that I am thinking is:

1.- Develop all in php, but can php listen in com1 in windows enviroment?
2.- create an visual basic script, for manage the serial comunication data as a proxy, and find the best way to integrate this application to php( that php processe requested data, making conection to database.
in this case is an hibrid solution

  • 5
  • 2
1 Solution

seach for com1 and you will see some code that shows you how to write to COM1.  This may help you get started.
I have written many apps using RS232 back in the old days way back in 76. Under Windows the problem with RS232 was a physical limit to the devices that you could connect to. But using USB and some kind of driver this limit may have been overcome.

What type of physical connection system are you going to use? Does it have any limits?
cyberlabAuthor Commented:
the application will be installed for manage one unique serial port this ou focos for simplicity.
that for each serial port we need only clone the aplication and install, normally each pc have 3 instruments not more, but not have limitation.

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

cyberlabAuthor Commented:

yes I  have starter dear, before post here, the question is more conceptual, that what is more better solution, stable,etc.
The objective are:

* to control lots of devices simply
* to be able to update the software for all devices in one simple hit; even if the systems are all currently running.

I have had a chance to think about a really good solution. In summary I would write a server application that starts up on a PC or server somewhere on the same LAN the server waits for devices to start up and register themselves to the server.

The Devices control software starts up and reads a control file that defines the device type, location, communication settings and communication mode.  The Server knows which clients are attached and how to talk to each device.  The Server sends commands to each client and the client echos the serial data back to the server. So the server is talking to each device sening RS232 commands via TCPIP which are received by the device control clients which then repeat the commands to the devices via RS232 and echo the RS232 responses back to the server via TCPIP.

You are going to need 3 EXE files; 2 clients exe and one server app.

--------------Exe 1 DevControl.EXE----------------------

Device control starter, lets give this a name like DevControl.exe

The DevControl.exe  will start and read a control file in the same folder DevControl.txt.  The DevControl.txt will contain the name of the latest version of the device control client. For example the DevControl.txt will look something like this:


For each device attached to the PC there will be a link in the start menu-programs-startup so it will start automatically when the pc is rebooted. The link will look something like this:

\\ServerName\DevData\DevControl.exe DeviceXYZ.txt

Devcontrol will start to run and then simple read the devcontrol.txt file find out what the latest exe version name is set it running passing the start parameters.

Dim sLatest As String

sLatest =ReadFile( App.Path & "\DevControl.txt")

Dim lPID as long

lPID = Shell(sLatest, vbNormalWindow)


Because this is a very simple program it would take only about  15 minutes to complete.

---------------------Exe 2 - Device Controller

Each time the Device controller is changed it is given a new name like DevControlV10.exe, the next version will be DevControlV11.exe, then DevControlV12.exe etc.  In this way the device control softwatre can be updated even while the systems are in operations. And also throughout the entire organisation there will only be one current exe making support and upgrade simple.

When the DevControlVxx starts running it reads the parameter file passed to it in the command line.

The parameter file could look like this:


The device controller reads the control file to see which device it has attached.  It connects connects to the Server using the name and port specified. When the connection is established the device controller registers itself with the server it will send a message like.

Server.SendMessage "LOCATION=" + sDeviceLocation ;"DEVICETYPE=" + sDeviceType
The client then waits for a message from the server like "OK;ESCAPE=$%^;"

The client would store the escape sequence as this would be used in future control messages.

Further control messages start and end with the escape sequence that the server proposed. This is to simplefy the code that gets fired when a message from the server arrives. Normally data from the server will be echoed to the device. But if a message starts with an escape sequence it signifies that the message is a control message directed to the client rather than the device.  In this way the server can send control statements to the device control software. Allowing commands like EnterDebugMode (the device controller displays a monitor screen showing data send between server and the device) or Restart (telling the device controller to End and run the DeviceControl.exe so the latest version can be reloaded without or even commands like Reboot system could be actioned.

This program is just 3 small sections.

Startup Phase
  Read Device Control Ini information file.
  Connect to server

Wait for Server Commands
   Decode incoming server commands; look for escape sequence, if found action the command.
When the server sends the "Open Device" escape sequence the RS232 communication to the device is initiated.

Wait for RS232 activity, echo activity back to the server.

The code for this exe would be very simple to create and would only take about 2 to 4 hours to complete.

-------------------------Exe 3 DevServer.exe

This is the major part of the system would need the most thought but still quite simple.

Server Listens for new incoming TCP/IP connections.

New connections arrives, server receives location and device type and it stores the connection in a collection of connected clients and tells the client to Open the device and start data echo. All subsequent data form the client will be data echoed from the RS232 port.

Incoming data from a client, the server knows what type of device is connected to the client so it would decode the data according to the device type.

You could just simply hard code the handler for each device type in the main exe but a really cool way you be to create a with events object to handle the device type activity.  In this way you could add new objects to the server without having to recompile it.  Each handler object would have standard methods & properties and generate standard events. So when a new device connects to the server you would create a new instance of a client class.  The client class would contain the device type, the incoming socket,  a new instance of the object handler for device and other information like time of last activity, etc.

The key thing would be the device hander based on the device type sent from the client, so the class would look a bit like this:

Class DeviceClient()

Public WithEvents Sock As Winsock
Public DeviceType As String
Public DeviceLocation As String
Public LastMessage As Date
Public DeviceHandler As Object
Public Status As Long
End Class

So when the new connection arrives the TCPIP servers receives a connection request event where the server creates a new instance of a device client class and accepts the connection into the socket defined in the DeviceClient class. The server then adds the DeviceClient to a collection of Clients.

As soon as the server receives a new connection it would send the client a message so the client knows that it connected ok. Perhaps the server could send a message like "OK;" + sEscapeCode+";"

The client would send the server the locations and device type. The server would then create a handler object for the client and set the status of the DeviceClient to 1 to indicate client is connected and registered and the object is ready to process echo data.

Again this code would be quite easy to create and should only take a small number of hours.

------------------------------------Handler Classes

For each of your device types you would need to create a handler class with some methods and events.

Class SampleHandler()

Public Sub DataFromDevice(psMessage As String)

' Process in coming data
' you would need to decode the data and then raise events to feed the conclusion to the server so the server can display on a monitor

End Sub

The handler would depend on the messages that the device input protocol and the device output protocol.

Hope this helps, let me know if you would like any code samples :~}

One final comment; it would be better to make the server .Net or a language that can handle multiple threads.




cyberlabAuthor Commented:
nice model, this is very pointting in the rigth way, in this midle time, I have aready made same decision:

-Clients will have an small exe, acting as a port gateway serial/tcp in bi-directional mode, listen for connection started from any side. I have writed 90%

-Server willbe an php socket server runing in linux as a backgound process. 10% only for test.
and here we are posting this asks for this:

-At server side we are searching a create a standard way for normatize all direntes fiels coming from all devices, if you wan I have opened another post about this here:


in general, what is the best for manage rs232 basic data comunication?
in the small exe, (like ACK ENQ commands response interacting for data transmition) or at the tcp server we do all control data as the ACK ENQ and all data adquisition/tramsmition.

cyberlabAuthor Commented:
inthedark have partially helped in the solution
cyberlabAuthor Commented:
B: The Expert(s) provided an acceptable solution, or a link to an acceptable solution. May assign if you needed a bit more information to complete the task.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now