locked
A problem in using serialport RRS feed

  • Question

  • how to change the fallowwing vb6 code into vb.net 2005  

             Do While MSComm1.InBufferCount > 16


                        getstring1 = MSComm1.Input  '接收对应站点发送的数据


              Loop

     

    Sunday, September 10, 2006 4:37 AM

Answers

  • Maybe I can help.  First off, I'm not sure what you mean by a  "hardware-AD module".  What does the AD stand for?  Secondly, if you're working with a virtual serial port, .NET Compact Framework doesn't support those, so you may or may not be able to accomplish what you want.

    BUT, that disclaimer notwithstanding, you may yet be able to get it done.  I've heard of a possible bug with SerialPort.BytesToRead on virtual serial ports.  If instead of looping through each character according to .BytesToRead as you're doing (which is a pretty non-performant way to go about it anyway), I suggest you try using SerialPort.ReadExisting to get everything off at once.  This will be faster, and (I suspect) will work with your virtual serial port.

    Let me know if it works.  I hope it helps.

    Monday, September 25, 2006 5:37 PM

All replies

  • Hi my friend! You can change your component to the SerialPort(System.IO.Ports, native in framework 2.0). Ex; using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; using System.IO.Ports ; namespace Test { public partial class test { void readtest(object sender, System.EventArgs e) { SerialPort port = new SerialPort(); byte[] bytes = new byte[port.ReadBufferSize]; try { port.Open(); if (port.IsOpen == true) { port.Read(bytes, 0, bytes.Length); } } catch (Exception ex) { MessageBox.Show(ex.Message,"Erro"); } } } }
    Saturday, September 16, 2006 2:55 AM
  • Hi my friend! You can change your component to the SerialPort(System.IO.Ports, native in
    framework 2.0). Ex;

    using System;
    using System.IO.Ports;

    namespace Test
    {
        public partial class Test1
        {

            void testread(object sender, System.EventArgs e)
            {
                SerialPort port = new SerialPort();
                byte[] bytes = new byte[port.ReadBufferSize];
                try
                {
                    port.Open();
                    if (port.IsOpen == true)
                    {
                        port.Read(bytes, 0, bytes.Length);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message,"Err");
                }
            }
        }
    }
    Saturday, September 16, 2006 2:57 AM
  • Thanks, my friend ,when I checked my program,another question raised.So I am puzzled. I wonder if the transmittion part is valid.Now  I will show you my whole program as follows:

    Imports System.IO.Ports

    Public Class Form1
        Dim commport As New SerialPort

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

            With commport
                If Not .IsOpen Then .Open()
                .DtrEnable = True
                .RtsEnable = True
                .Write(":010300000001BB" & Chr(13) & Chr(10))       'the string I wanna send to the serialport
            End With
        End Sub

        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Timer1.Enabled = True
            Timer1.Interval = 1
        End Sub

     Private Sub Bttn_Clear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            Txt_Send.Text = String.Empty
            Txt_Receive.Text = String.Empty
        End Sub

        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            With commport
                If Not .IsOpen Then .Open()
                .DtrEnable = True
                .RtsEnable = True


                Dim i As Integer
               For i = 1 To .BytesToRead
                    str = .ReadChar
                    TextBox2.Text = TextBox2.Text + str
                Next
            End With

        End Sub

    I can use the above program  to send and receive string between tow serialports,but when I send the  string 'Chr(58) &"010300000001BB" & Chr(13) & Chr(10)'  to a hardware-AD module,a problem raised.The AD module  return nothing to the computer.But  the program which programmed with vb6 can receive  message  from the AD module.Here is part of my vb6 program.

     With AXMSComm1
                Dim send_text As String '该变量存储将要向串口发送的字符串
                send_text = Chr(58) & " 010300000001BB"  & Chr(13) & Chr(10)
                If Not .PortOpen Then .PortOpen = True
                .RTSEnable = True
                .DTREnable = True
                .OutBufferCount = 0
                '清空接收缓冲区
                .InBufferCount = 0
                .Output = send_text
            End With

            With AxMSComm1
                If Not .PortOpen Then .PortOpen = True
                .RTSEnable = True
                .DTREnable = True
                .OutBufferCount = 0
        '清空接收缓冲区
                .InBufferCount = 0
                Txt_Receive.Text =.Input
            End With

    Now I am puzzled,could you tell me why? I am looking forward to your reply. Thanks.

     

    Sunday, September 17, 2006 9:28 AM
  • Maybe I can help.  First off, I'm not sure what you mean by a  "hardware-AD module".  What does the AD stand for?  Secondly, if you're working with a virtual serial port, .NET Compact Framework doesn't support those, so you may or may not be able to accomplish what you want.

    BUT, that disclaimer notwithstanding, you may yet be able to get it done.  I've heard of a possible bug with SerialPort.BytesToRead on virtual serial ports.  If instead of looping through each character according to .BytesToRead as you're doing (which is a pretty non-performant way to go about it anyway), I suggest you try using SerialPort.ReadExisting to get everything off at once.  This will be faster, and (I suspect) will work with your virtual serial port.

    Let me know if it works.  I hope it helps.

    Monday, September 25, 2006 5:37 PM