Solved

Communications with device using STX/ETX/LRC

Posted on 2013-01-04
7
3,495 Views
Last Modified: 2013-01-11
Hey guys!!
I am trying to communicate with an Ingenico iSC350 and I need some help with the following string.

STX control character
1 byte
Message Identifier
3 bytes
Message data, consisting of multiple fields
n bytes (0 to 241)
ETX control character
1 byte
LRC check character
1 byte

How do I go about this?

Thanks,

Jerry
0
Comment
Question by:JDL129
  • 4
  • 3
7 Comments
 
LVL 15

Accepted Solution

by:
Minh Võ Công earned 500 total points
ID: 38743820
Do you want parser message?
a line format like: STX(1)MsgID(3)MsgData(0-241)EXT(1)LRC(1)
in C# string is unicode
string strMsg = <STX>midmessagedata<EXT><LRC>;
char STX = (char)0x02;
char ETX = (char)0x03;
strmsgid = strMsg.Substring(1,3); //now strmsgid = "mid"
//convert to byte array
byte [] id = Encoding.ASCII.GetBytes(strmsgid);
int i = strMsg.IndexOf(ETX);
strmsgdata = strMsg.Substring(4,i-4);// strmsgdata  = "messagedata"
charLRC = strMsg[strMsg.Leng-1];
0
 

Author Comment

by:JDL129
ID: 38744587
minhvc!!!!!!!!!!!!!  I am glad to hear from you.  You get the points but I need some clarification.

What you have sent is exactly what I need.  I have some data to send and I would appreciate it if you can put it in the format above.

My open message is 01.00000000
My close message is 00.0000

 STX an ASCII control Character, Type is Constant, and length is 1
Message Identifier is ASCII "01." for open message
Application ID is Decimal 0000
Parameter ID is Decimal 0000
ETX is an ASCII control Character Type is Constant, length is 1
LRC check character is Binary, length is 1

When it is sent it looks like 01.00000000

When I run int i = strMsg.IndexOf(ETX);  i is -1.
I don't think I have items in the proper order.

Again I appreciate the post!!!!

Jerry
0
 

Author Closing Comment

by:JDL129
ID: 38749476
Thanks a bunch!!!!!!!!!!!!!111
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 15

Expert Comment

by:Minh Võ Công
ID: 38749686
How your PC connect to Ingenico iSC350? Com (serial port) or IP (Socket, NET)? Can you post the code you have send/receive data? Do you receive a byte [] or string data?
0
 

Author Comment

by:JDL129
ID: 38751356
minhvc!!!!!!!!!!!!!!!!!!!!!!
Thanks for the post!!!!!!!!!

It connects by usb: RemoteHost is 127.0.0.1 and RemotePort is 5446 and it has a Telnet button beside the Host and Port fields.

When I parse the received data it appears to be the same format as you have suggested we send.

This is the code we're trying to use:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Net.Sockets;
using System.IO;
using System.Threading;
using System.Net;

namespace iSC350
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        TcpListener listener = null;
        private void btnSend_Click(object sender, EventArgs e)
        {  
             string strMsg = "<STX>"+txtMessage.Text+"<FS>"+txtBoxId.Text+"<FS>7699<ETX><LRC>";
            TcpClient tcpClient = new TcpClient();
            tcpClient.Connect("127.0.0.1", 5446);
            NetworkStream Streamtest = tcpClient.GetStream();

            Byte[] outbytes = System.Text.Encoding.Default.GetBytes(strMsg.ToCharArray());
            Streamtest.Write(outbytes, 0, outbytes.Length);//Sned data to ISC350 port
            tcpClient.Close();

        }

      private void AcceptWorkThread()
        {
         
            while (true)
            {
                Socket socket = listener.AcceptSocket();
                byte[] buffer = new byte[1024];
                int receiveCount = socket.Receive(buffer);
                if (receiveCount > 0)
                {
                    string recString = Encoding.UTF8.GetString(buffer, 0, receiveCount);  
                   MessageBox.Show(recString,"ISC350 Port get data.");
                }
                else
                {

                }
            }
        }

      private void Form1_Load(object sender, EventArgs e)
      {
          listener = new TcpListener(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 5446));
          listener.Start();//Listen ISC350 port if get data
          Thread acceptThread = new Thread(new ThreadStart(AcceptWorkThread));
          acceptThread.Start();
      }      
    }
}

Thanks again for the help!!!

Jerry
0
 
LVL 15

Expert Comment

by:Minh Võ Công
ID: 38761920
How recString you received from device?
0
 

Author Comment

by:JDL129
ID: 38766936
minhvc!!!!!!!!!!!

We haven't got to there yet.  Any ideas?

Thanks again,

Jerry
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now