Omron Forums Support Posted March 14, 2022 Share Posted March 14, 2022 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 connectionIn 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 handlersThe 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 readerAs 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 readerTo 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 connectionTo 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 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Top Posters In This Topic
1
Popular Days
Mar 14
1
Top Posters In This Topic
Omron Forums Support 1 post
Popular Days
Mar 14 2022
1 post