从Shell脚本检查是否返回非标准字符 [英] Checking for return of non-standard character from shell script

查看:64
本文介绍了从Shell脚本检查是否返回非标准字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用C#编写一个函数,该函数通过shell脚本与相机进行通信.该函数接受一个字符串命令并将其写入相机.如果识别出该命令,则相机会返回一些特定于该命令的信息,然后 在特定行中,它返回一个铲形符号(以unicode为\ u2660).如果无法识别/无法执行该命令等,则相机会发送新的分区符号(认为是\ u00A7).我希望我的函数使用它来返回true或false 将用于确定读取/写入是否成功.

I'm trying to write a function in C# that communicates via a shell script with a camera. The function takes a string command and writes it to the camera. If the command is recognised then the camera returns some information specific to the command and then in a particular line it returns a spade symbol (think it is \u2660 in unicode). If the command is not recognised/impossible etc the camera sends the new section symbol (think it is \u00A7). I want my function to use this to return either a true or false which will be used to determine if the read/write was successful. 

如果我在cmd窗口中手动执行此操作,则可以看到一切正常,因此我知道相机等正在以这种方式进行响应.我的代码不完整如下.它成功地将命令写入外壳程序,然后将所有行一一读取到 字符串数组.可以,但是符号丢失了,如果我尝试显示它们,它们都被写成正方形.

If I perform this in a cmd window manually I can see everything is behaving as expected so I know camera etc is responding in this way. My incomplete code is below. It writes a command to shell successfully and then reads all the lines one by one into a string array. This works but the symbols are missing and if I try and display them they are both written as a square. 

任何人都可以建议:一种区分这两个命令的方法,即我可以以字节的形式读取它们吗(我尝试过这样做,有点混乱!)?是ReadLine()操作正在读取Unicode字符,而我正在努力显示它们还是它的初始字符? 读取操作失败了吗?任何帮助都是最有用的,因为我正在努力解决这个问题.

Can anyone suggest: a way to distinguish between the two commands, ie can I read them in as bytes (I tried this and got into a bit of a mess!)? Is the ReadLine() operation reading the unicode characters and I am struggling to display them or is it the initial read operation that is failing? Any help would be most useful as I am struggling to get round this.

  //This function takes a command runs the shellscript to update the camera settings. It returns
        //a value of false if there is an error, 1 if successful.
        private bool shellCommand(string CommandString)
        {
            const char SpadeValue = '\u2660';
            string args = ("-a");//clshell.exe is launched with this additional command
            string[] output = new string[25];//string array to hold the returned strings
            bool noError;//return value to determine if write /read was successful

            
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            
            // The following commands are needed to redirect the standard output.
            proc.StartInfo.RedirectStandardOutput = true;
            proc.StartInfo.UseShellExecute = false;
            proc.StartInfo.CreateNoWindow = true;
            proc.StartInfo.RedirectStandardInput = true;
                    

            proc.EnableRaisingEvents = false;
            proc.StartInfo.FileName = "C:\\Program Files\\SiliconSoftware\\Runtime5.1\\bin\\clshell.exe";
            proc.StartInfo.Arguments = args;
                     
            proc.Start();
            //write command string to camera via clshell.exe
            proc.StandardInput.WriteLine(CommandString);
            proc.StandardInput.Close();
          
            //Read in all the output line by line into a string array
            int i = 0;           
                do
                {
                    output[i] = proc.StandardOutput.ReadLine();
                    i++;
                } while (output[i-1] != null);      
     
                //Set return value depending on whether it was successful
                if (output[5].IndexOf(SpadeValue) >= 0)
                {
                    noError = true;
                }               
                else
                {
                   noError = false;
                }
             
            proc.WaitForExit();
            proc.Close();
            
            return noError;                      
        }

推荐答案

正方形字符通常表示字体没有可用于显示的字符.

The square character normally means that the font does not have the character available to it to display.

您将需要

运行外壳程序-设置字体以使用unicode字体...然后您将看到正确的返回字符

Run the shell - set up the font to use a unicode font... then you will see the correct return character


这篇关于从Shell脚本检查是否返回非标准字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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