坚持使用C#中的ASTM代码 [英] Stuck with ASTM code in C#

查看:195
本文介绍了坚持使用C#中的ASTM代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试图找出与基于ASTM协议的设备进行通信的方式。

I am trying to figure out the way to communicate with an equipment that is ASTM protocol based.

通讯组成两个阶段。

阶段1:从设备读取数据   >已完成!

Phase 1 : reading data FROM the equipment    > already done !

第2阶段:向设备发送数据 >坚持 

Phase 2 : sending data TO the equipment  > Stuck with 

阅读我使用此代码的数据 

to read the data I use this code 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ASTM;
using System.IO.Ports;
using System.Threading;
using System.Data.OracleClient;

namespace TestMessage
{
    class Program
    {
        const int wait = 3000;
        const int cihazId = 2;

        static string soh = char.ConvertFromUtf32(1);
        static string stx = char.ConvertFromUtf32(2);
        static string etx = char.ConvertFromUtf32(3);
        static string eot = char.ConvertFromUtf32(4);
        static string enq = char.ConvertFromUtf32(5);
        static string ack = char.ConvertFromUtf32(6);
        static string nack = char.ConvertFromUtf32(21);
        static string etb = char.ConvertFromUtf32(23);
        static string lf = char.ConvertFromUtf32(10);
        static string cr = char.ConvertFromUtf32(13);


        static SerialPort sp;
        static EventWaitHandle wh;
        
        static bool orderFlag = false;
      

        static void Main(string[] args)
        {
            Console.WindowWidth = 120;
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Title = "Host";
            
            //Console.WriteLine(Console.WindowWidth.ToString());

            sp = new SerialPort("COM1");
            sp.DataReceived+=new SerialDataReceivedEventHandler(sp_DataReceived);
            sp.Open();
            Console.WriteLine("[port opened]");

            Console.ReadKey();
           
        }

        static void sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            string str = sp.ReadExisting();
            Eval(str);
            Console.WriteLine("[received]" + Display(str));
        }


        private static void Eval(string str)
        {
            string received;
            
            Thread thread;

            received = null;

            received = str;
            Console.WriteLine("[received]" + Display(received));

            if (received == ack)
            {
                wh.Set();

            }
            else if (received == enq)
            {
                ACK();
            }

            else if (received == eot)
            {
                if (orderFlag)
                {
                    thread = new Thread(new ThreadStart(SerialWrite));
                    thread.Start();
                    orderFlag = false;
                    //Console.WriteLine("[Host ~]$ Analyzer Queries {0}", _barcode);
                    //Console.WriteLine("[Host ~]$ Retrieving {0} tests from LIS", _barcode);
                }
            }
            else
            {
                string recordType = received[2].ToString();

                switch (recordType)
                {
                    case "H":
                        ACK();
                        break;
                    case "Q":
                        string[] fields = received.Split('|');
                        string[] comps = fields[2].Split('^');
                        //string barcode = comps[1];

                        //_barcode = Convert.ToInt32(barcode);
                        
                        orderFlag = true;
                        ACK();
                        break;
                    case "R":
                        string test = "";
                        string result = "";
                        string resultUnit = "";
                        test = received.Split('|').GetValue(2).ToString();
                        result = received.Split('|').GetValue(3).ToString();
                        resultUnit = received.Split('|').GetValue(4).ToString();
                        Update( test, result, resultUnit);
                        //Update(_barcode, test, result, resultUnit);
                        ACK();
                        break;
                    //case "O":
                    //    _barcode = Convert.ToInt32(received.Split('|').GetValue(2).ToString());
                    //    ACK();
                        //break;
                    case "L":
                        ACK();
                        break;
                    default:
                        ACK();
                        break;
                }

            }

        }

我需要知道您如何根据此信息向设备发送数据 

I need to know how do you SEND data to the equipment according to this 

HOST [ENQ]

设备[确认]

主机[STX] 1H | \ ^& | | | ASTM-Host [CR] [ETX] 59 [CR] [LF]

设备[确认]

主机[STX] 2P | 1 [CR] [ETX] 3F [CR] [LF]

设备[确认] 

Equipment[ACK]  

HOST [STX] 3O | 1 | 000083 | 135 ^ 0 ^ 1 ^^ SAMPLE ^ NORMAL \ ^^^ 10 ^ 0 \ ^^^ 150 ^ 0 | R | | | | | | N | | | | | | | | | | | | | | Q [CR] [ETX] F9 [CR] [LF]

HOST [STX] 4L | 1 [CR] [ETX] 3D [CR] [LF]

设备[确认]

主机[EOT]

使用上面的代码我能够以这种方式查看设备中的数据 

using the code above I am able to view data from the equipment in this way 

Equimpent [ ENQ]

Equimpent [ENQ]

主机[确认]

设备[STX] 1H | \ ^& | | | | | | | | | | P | | [CR] [ETX] 05 [CR] [LF]

Equipment[STX]1H|\^&| | | | | | | | | |P| |[CR][ETX]05[CR][LF]

主持人[确认]

设备  [ STX] 2P | 1 | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | [CR] [ETX] 3B [CR] [LF]

Equipment [STX]2P|1| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | |[CR][ETX]3B[CR][LF]

主持人[确认] 

设备[STX] 3O | 1 | 000083 | 135 ^ 0 ^ 1 ^^ SAMPLE ^ NORMAL | ALL | R | 19980403090614 | | | | | X | | | | | | | | | | | | | | O | | | | | [CR] [ETX] 79 [CR] [LF]

Equipment [STX]3O|1|000083|135^0^1^^SAMPLE^ NORMAL|ALL|R |19980403090614| | | | |X| | | | | | | | | | | | | |O| | | | |[CR][ETX]79[CR][LF]

主持人[确认] 

设备[STX] 4R | 1 | ^^^ 10 ^ 0 | 2.73 | ULU / ml的| 0.270 ^ 4.20 | N | | F | | | 19980403090641 | 19980403092503 | [CR] [ETX] B7 [CR]

Equipment [STX]4R|1|^^^10^0| 2.73|ulU/ml|0.270^4.20|N| |F| | |19980403090641|19980403092503|[CR][ETX] B7[CR]

主机[确认]

设备< span style ="font-size:0.75em"> [STX] 6L | 1 [CR] [ETX] 3F [CR] [LF]

HOST [确认]

设备[EOT]

----------------- ----------------------------------

---------------------------------------------------

basicailly我只是回复用[ACK]到每一行,这样我就能得到下一行。但问题是,我该如何回信?

basicailly I am just replying with [ACK] to every line so i can get the next one . but the question is , how do I write back ?

提前致谢。

推荐答案

也可能使用SerialWrite()函数。但是,根据我从标准中读到的内容,您可能需要进行一些修改才能使其正常工作。

Possibly with your SerialWrite() function too. However from what I read from the standard, you may need some more modification to make it work.

利用Encoding.ASCII.GetBytes()发送命令,因为.NET框架默认情况下在UTF16上运行,但ASTM标准明确规定所有命令都必须是ASCII。

Make use of Encoding.ASCII.GetBytes() to send your command because .NET framework defaults to run on UTF16 but the ASTM standard explicitly states all the commands need to be in ASCII.

(或者更具体地说:

允许的字符: 7,9,11,12,13,32-126,128-254
$
不允许使用的字符:0-6,8,10,14-31,127,255)

Allowed characters: 7, 9, 11, 12, 13, 32-126, 128-254
Disallowed characters: 0-6, 8, 10, 14-31, 127, 255)

另请注意,除非标准中另有说明,否则在文本字段中仅允许ASCII字符32-126和未定义字符128-254。您可能希望在发送之前编写要检查的命令的验证函数。

Also note the unless otherwise stated in the standard, only ASCII characters 32-126 and the undefined characters 128-254 are permitted on text fields. You may want to write validation functions of the commands to check before sending it out.

在验证命令字节序列是否正确后,您现在可以使用$ b $发送它b
SerialPort.Write()重载使用字节数组代替
。 (这是您对SerialWrite功能所需的修改)

After you've verified the command byte sequence is correct, you may now send it out with SerialPort.Write() overload that uses byte array instead. (This is the modification you'll need for SerialWrite function)


这篇关于坚持使用C#中的ASTM代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆