Jump to content
OMRON Forums

Code example - C# - Websocket client for Microhawk ID Older Version


Omron Forums Support

Recommended Posts

1.  Create a new project of Windows Forms in VisualStudio. (It is not a must to use Windows Form, but for the sake of this example it will be easier. This example is done with VisualStudio2015.)

2.  Add to the project Websocket4Net.dll as reference (download it from this link in case you don’t have it: www.websocket4net.codeplex.com). 


3.  In the source file Form1.cs, add the required reference:

using WebSocket4Net;

4. Add the required controls to your Windows Form (buttons, textbox…). See attached project file for a complete example. 
 
Open websocket connection
In order to open a Websocket connection, you need to create an instance of WebSocket, giving the correct url for opening a Websocket connection with the reader as a server:
websocket = new WebSocket("ws://192.168.188.2:50501/");

In the line above, WebSocket instance is created with the name websocket, with a reader IP address 192.168.188.2 which is the default IP address of the reader. The number 50501 is the Websocket port number.

Define event handlers
The WebSocket class will raise events on specific actions like connection opened, data received… In order to be able to handle such events, add them first, declaring the names of the corresponding event handlers:
Event handler for connection opened:
websocket.Opened += new EventHandler(websocket_Opened);
 
Event handler for connection closed:
 websocket.Closed += new EventHandler(websocket_Closed);
 
Event handler for receiving data:
 websocket.MessageReceived += new EventHandler<MessageReceivedEventArgs>(websocket_MessageReceived);

Handler functions:
Define the handler functions for each one of the above events. The handler function will be called when the corresponding event rises. (Log can be a simple function do display text string as user output).

Handler function for connection opened event:
private void websocket_Opened(object sender, EventArgs e)
{
     Log("Websocket opened \r\n");
}
Handler function for connection closed event:
private void websocket_Closed(object sender, EventArgs e)
{
     Log("Websocket closed \r\n");
}
Handler function for message received event:
private void websocket_MessageReceived(object sender, MessageReceivedEventArgs e)
{
     Log(e.Message);
}
 
After defining the event handler functions, when the Websocket is opened it will launch ‘Websocket opened’ event. When data is received from the reader (=server) over this Websocket, it will launch the event ‘Websocket message received’.

Receive data from the reader
As explained above, when data is received from the reader, the event ‘Websocket message received’ is launched. Define the code inside the handler function websocket_MessageReceived to perform the desired functionality. In the example above, the received text is simply fed into Log  function which displays the message as output.

Send data to the reader
To send data to the reader, use the following method:
websocket.Send("<K231?>");

This will send the given K-command string directly to the reader over the opened Websocket connection.

Closing the Websocket connection
To close the websocket connection with the reader, use the following method:
websocket.Close();
 
As mentioned above, when the Websocket connection is closed, it will launch the ‘Websocket closed’ event.

 

Operating System

Any

Link to comment
Share on other sites

  • Replies 0
  • Created
  • Last Reply

Top Posters In This Topic

Popular Days

Top Posters In This Topic

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...