c# - 将微型 C# 控制台应用程序迁移到在 Ubuntu Server 9.04 上运行的应用程序

标签 c# php linux console migration

我是一个 linux 菜鸟,想迁移这段 C# 代码:

using System;
using System.IO;
using System.IO.Ports;

public class LoadCell
{
    private static string configFile = Directory.GetCurrentDirectory() + "\\LoadCell.config";
    private static string errorLog = Directory.GetCurrentDirectory() + "\\LoadCell.log";
    private static string puerto = "COM1";

    public static void Main (string[] args)
    {
        if(File.Exists(configFile))
        {
            TextReader tr = new StreamReader(configFile);
            puerto = tr.ReadToEnd();
            tr.Close();
        }

        if(args.Length > 0)
        {
            switch(args[0])
            {
                case "-ayuda":
                    Console.WriteLine("\r\nEste programa esta diseñado para capturar el peso medido actualmente por la báscula a través de un puerto (por defecto es el COM1). Recuerde que el indicador debe estar configurado para:");
                    Console.WriteLine("\r\npuerto: " + puerto);
                    Console.WriteLine("baud rate: 4800");
                    Console.WriteLine("parity: none");
                    Console.WriteLine("data bits: 8");
                    Console.WriteLine("stop bit: one");
                    Console.WriteLine("\r\nEn caso de ocurrir un error recuerde revisar el log de errores (" + errorLog + ").");
                    Console.WriteLine("\r\nLos posibles argumentos son:");
                    Console.WriteLine("\r\n-ayuda: este ya lo sabes usar...");
                    Console.WriteLine("\r\n-puerto <nombre>: cambia el puerto al nuevo valor creando un archivo de configuración (" + configFile + ")");
                    Console.WriteLine("\r\n-default: elimina el archivo de configuración para retomar la configuración inicial");
                    break;

                case "-default":
                    File.Delete(configFile);
                    Console.WriteLine("\r\narchivo de configuración eliminado");
                    break;

                case "-puerto":
                    if(args.Length > 1)
                    {
                        puerto = args[1];
                        TextWriter tw = new StreamWriter(configFile);
                        tw.Write(puerto);
                        tw.Close();
                        Console.WriteLine("\r\npuerto cambiado a " + puerto);
                    }
                    else
                    {
                        Console.WriteLine("\r\nse esperaba un nombre de puerto");
                    }
                    break;
            }
        }
        else
        {
            new LoadCell();
        }
    }

    private static void log(string text)
    {
        Console.Write("ha ocurrido un error...");
        TextWriter sw = File.AppendText(errorLog);
        sw.WriteLine("[" + System.DateTime.Now.ToString() + "] " + text);
        sw.Close();
    }

    public LoadCell()
    {
        try
        {
            SerialPort port = new SerialPort(puerto, 4800, Parity.None, 8,  StopBits.One);
            port.NewLine = "\r";
            port.ReadTimeout = 10000;
            port.Open();
            port.WriteLine("RA:");
            port.DiscardInBuffer();
            Console.Write(port.ReadLine());
        }
        catch(Exception e)
        {
            log("Error: " + e.ToString());
        }
    }
}

对于其他方面,任何建议将不胜感激!

顺便说一句,您如何看待直接在 PHP 中执行此操作?因为结果在 PHP 文件中使用,例如:

function peso() {

    $resultado = utf8_encode(exec('loadcell'));

    if(preg_match('/^RA:\s*([0-9]{1,8})$/i', $resultado, $m) > 0) {

        json_exit(array('peso' => $m[1]));

    } else {

        json_exit(array('error' => $resultado));

    }

}

谢谢!

最佳答案

请记住,Linux 使用正斜杠表示路径,而 Windows 使用反斜杠,因此您需要执行以下操作:

private static string configFile = Path.Combine(Directory.GetCurrentDirectory(), "LoadCell.config");
// Or...
private static string configFile = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + "LoadCell.config";

除此之外,当您编译为 mono 时还有什么问题?

此外,通过 MoMA 运行您的应用程序.

关于c# - 将微型 C# 控制台应用程序迁移到在 Ubuntu Server 9.04 上运行的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/973952/

相关文章:

c# - 使用 MongoDB 的官方 C# 驱动程序的按位枚举(标志)查询

java - 可以执行 JavaScript 的服务器端浏览器

php - 图片无法显示,因为它在 php gd 中包含错误

linux - Linux文件系统中3个时间状态有什么区别

linux - 检查 fork() 是否安全

c# - 使用与绑定(bind)不同的数据源将列动态添加到 Gridview

javascript - 在嵌入式 JavaScript 中插入 AssemblyInfo(例如 DLL 版本号)

c# - MongoDB C# 驱动程序中的部分更新 - 字典问题

php - Codeigniter Active Record - 以特定 ID 开始查询?

regex - Apache 元字符