[VB.NET Compact] SerialPort.write问题 [英] [VB.NET Compact] SerialPort.write problem

查看:63
本文介绍了[VB.NET Compact] SerialPort.write问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在使用visual studio 2008开发/迁移VB6中的应用程序到Windows CE 5 Pro设备,我正试图解决这个问题问题大约4天没有运气。

I'm developing/migrating an application from VB6 to a Windows CE 5 Pro device with visual studio 2008 and i'm trying to solve this serial problem for about 4 days without any luck.

我们走了:

我需要通过串口发送一个特定的字节数组来得到一个来自自定义设备的响应。

I need to send a specific byte array via the serial port to get a response from a custom device.

字节数组是:  dim bytearray(6)as byte = {83,49,3,62,212,13} 十六进制为[53,31,03,40,D4,0D]

The byte array is this:  dim bytearray(6) as byte = {83, 49, 3, 64, 212, 13} that in hex is [53,31,03,40,D4,0D]

我使用标准serialport发送它: serialport1.write(bytearray,0,6)。

I send it using the standard serialport: serialport1.write(bytearray,0,6).

不幸的是,使用串口监视器,我检查了transmmision,发现它正在发送:53 FF 31 FF 03 40 FF 3F 0D。

Unfortunately, using a serial port monitor, i checked the transimmision and find that it's sending this: 53 FF 31 FF 03 40 FF 3F 0D.

尝试多次,并发现FF字节不固定并显示显然是随机的。

Tryed multipple times and also discovered that the FF byte aren't fixed and shows up at random apparently.

经过一些搜索,我设法让它在正常的PC上更改代码页:

After a bit of search i managed to make it working on a normal pc changing the codepage:

serialport.Encoding = System.Text.Encoding.GetEncoding(28591)

不幸的是,紧凑框架不支持编码,我已经尝试了所有的编码但没有人工作。

Unfortunately that encoding isn't supported by the compact framework, I'have tryed all the encodings but no one is working.

有人有解决方案或建议吗?我认为我已经死路一条,因为我不知道如何解决这个问题...

Someone have a solution or a suggestion? I think i'm a dead end since i have no idea on how to solve this...

如果这可以帮助我使用Visual Studio 2008中的意大利语使用windows xp sp3用于开发和windows ce设备的意大利语是英语。

If this can help i'm using windows xp sp3 in italian with visual studio 2008 also in italian for the developing and the windows ce device is in english.

提前感谢谁将帮助我。

推荐答案

使用字节数组发送二进制数据时不使用编码。 

Encoding is not used when sending binary data using a byte array. 

但是,您必须为二进制数据设置serialport.DataBits = 8.  例如,我写了这个小型演示。 它正确发送数据。

You must, however, set serialport.DataBits = 8 for binary data.  For example, I wrote this small demo.  It sends data correctly.

Public Class Form1

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object,ByVal e As System.EventArgs)Handles MyBase.Load

       使用SerialPort1

            .PortName =" Com1"  '使用您的申请所需的任何名称

            .RtsEnable = True

            .DataBits = 8            
'这是二进制数据所必需的

          ;   .BaudRate = 9600      '使用所需的任何速度

            .Parity = IO.Ports.Parity.None   
'这是二进制数据所必需的

          ;   .Open()

       结束与$
   结束次

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        With SerialPort1
            .PortName = "Com1"  'use whatever name you need for your application
            .RtsEnable = True
            .DataBits = 8             'this is required for binary data
            .BaudRate = 9600      'use whatever speed is required
            .Parity = IO.Ports.Parity.None    'this is required for binary data
            .Open()
        End With
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)Handles Button1.Click

        Dim bytearray()By Byte = {83,49,3,62,212,13}   '或  {& H53,& H31,& H03,& H40,& HD4,& H0D}

        SerialPort1.Write(bytearray,0,bytearray.Length)

   结束分

结束分类

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim bytearray() As Byte = {83, 49, 3, 64, 212, 13}   'or {&H53, &H31, &H03, &H40, &HD4, &H0D}
        SerialPort1.Write(bytearray, 0, bytearray.Length)
    End Sub
End Class

迪克


这篇关于[VB.NET Compact] SerialPort.write问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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