Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 基于HT16K33的十四段控制并不总是在Windows 10 IoT Core上显示给定位掩码的正确字符_C#_Uwp_Raspberry Pi_Windows 10 Iot Core - Fatal编程技术网

C# 基于HT16K33的十四段控制并不总是在Windows 10 IoT Core上显示给定位掩码的正确字符

C# 基于HT16K33的十四段控制并不总是在Windows 10 IoT Core上显示给定位掩码的正确字符,c#,uwp,raspberry-pi,windows-10-iot-core,C#,Uwp,Raspberry Pi,Windows 10 Iot Core,我想为运行Microsoft Windows 10 IoT Core的Raspberry在我的屏幕上的14段控制显示(4并排)上显示一个字符 对于一些像“HELL”(来自“Hello”)这样的消息,我的源代码是否如预期工作,但对于其他像“DEMO”这样的消息,它显示的是垃圾。这对我来说很奇怪,因为这两条消息都包含字母“E”。但它只工作一次 我的最后一个想法是,位掩码[1]&0xFF;与位移位(位掩码[0]>>8)和&0xFF不同,如果我们要创建一个不拆分的位掩码 示例消息(输入->显示在屏幕上)

我想为运行Microsoft Windows 10 IoT Core的Raspberry在我的屏幕上的14段控制显示(4并排)上显示一个字符

对于一些像“HELL”(来自“Hello”)这样的消息,我的源代码是否如预期工作,但对于其他像“DEMO”这样的消息,它显示的是垃圾。这对我来说很奇怪,因为这两条消息都包含字母“E”。但它只工作一次

我的最后一个想法是,
位掩码[1]&0xFF;
与位移位
(位掩码[0]>>8)和&0xFF
不同,如果我们要创建一个不拆分的位掩码

示例消息(输入->显示在屏幕上)

  • “地狱”->地狱
  • “演示”->,“(3个空格,逗号)
源示例(完整源:)

将写入设备的段缓冲区:

private readonly byte[] segmentBuffer = Enumerable.Repeat(Convert.ToByte(0b00000000), BUFFER_SIZE).ToArray();
字母D的位掩码:

private static readonly Dictionary<char, byte[]> BITMASK_DICTIONARY = new Dictionary<char, byte[]>{   
    { 'D',  new byte[]{0b00010010, 0b00001111},
      .... 
    }
};
将更改写入设备(message=“DEMO”):

for(int i=0;i
链接

  • 我的C#on GitHub
  • python源代码

根据您帖子中的代码,设置分段缓冲区似乎不正确。请尝试以下代码

        for (int i = 0; i < message.Length; i++)
        {
            var bitmask = ConvertCharToBitmask(message[i]);
            Logger.Log(this, $"Writing char: '{message[i]}' at index: {i}.");
            segmentBuffer[i * 2] = Convert.ToByte(bitmask[1] & 0xFF);
            segmentBuffer[i * 2 + 1] = Convert.ToByte(bitmask[0] & 0xFF);
        }

解决方案

这是一个多步骤的错误修正

  • @michael xu msft的提示直接指向右侧。我更新了源代码,使用默认的位移位,而不是拆分的数组

  • 我忘记将缓冲区偏移量
    0x00
    添加到
    Show()
    方法中


  • 更新后的源代码现已发布到GitHub。

    您的意思是只将[0]切换到[1],反之亦然?此更改没有任何效果。另一位用户的意思是,字节交换与位移位不同。我必须深入研究它。感谢您的帮助!我更新了代码,这是我的最后一次响应。它似乎与python代码匹配。谢谢!总是很有帮助的。
    for (int i = 0; i < message.Length; i++)
    {
        var bitmask = ConvertCharToBitmask(message[i]);
        segmentBuffer[i * 2] = Convert.ToByte(bitmask[0] & 0xFF);
        segmentBuffer[i * 2 + 1] = Convert.ToByte(bitmask[1] & 0xFF);
    }
    
    // Write buffer to device.
    ht16k33.Write(segmentBuffer);
    
            for (int i = 0; i < message.Length; i++)
            {
                var bitmask = ConvertCharToBitmask(message[i]);
                Logger.Log(this, $"Writing char: '{message[i]}' at index: {i}.");
                segmentBuffer[i * 2] = Convert.ToByte(bitmask[1] & 0xFF);
                segmentBuffer[i * 2 + 1] = Convert.ToByte(bitmask[0] & 0xFF);
            }
    
        private static readonly Dictionary<char, int> BITMASK_DICTIONARY = new Dictionary<char, int>
        {
            { ' ',  0b0000000000000000  },
            { 'D',  0b0001001000001111  },
        };
    
        private int ConvertCharToBitmask(char character)
        {
            // Check if char is available.
            if (BITMASK_DICTIONARY.Keys.Contains(character))
            {
                return BITMASK_DICTIONARY[character];
            }
    
            // If not, return default.
            return GetDefaultCharBitmask();
        }
    
        private int GetDefaultCharBitmask()
        {
            return BITMASK_DICTIONARY[DEFAULT_BITMASK_CHAR];
        }
    
    
        public void Show(string message)
        {
            // Ensure message has valid length.
            if(message.Length > NUMBER_OF_SEGMENTS)
            {
                throw new OperationCanceledException($"Maximum message length is {NUMBER_OF_SEGMENTS} characters.");
            }
    
            // Update buffer
            Logger.Log(this, $"Show for {message}");
            for (int i = 0; i < message.Length; i++)
            {
                var bitmask = ConvertCharToBitmask(message[i]);
                Logger.Log(this, $"Writing char: '{message[i]}' at index: {i}.");
                segmentBuffer[i * 2] = Convert.ToByte(bitmask & 0xFF);
                segmentBuffer[i * 2 + 1] = Convert.ToByte(bitmask >> 8 & 0xFF);
            }
    
            // Write buffer to device.
            ht16k33.Write(segmentBuffer);
        }