|
-
Nov 18th, 2011, 12:12 PM
#1
Thread Starter
New Member
SerialPort.ReadChar() takes hex returns decimal
Hi there,
I am working on a simple vb project to read data from a PC serial port and write that data to a file.
The data is ascii sent in Hex format. I used MySerialPort.ReadChar() to read data and MyStreamWriter.Write(MySerialPort.ReadChar()) to write it to a file.
I copied my code below, sorry about the format.
The trouble is: MySerialPort.ReadChar() acts the same as MySerialPort.ReadByte() , both takes the Hex data input and returns data in decimal format.
I tried to use Convert.ToChar(MySerialPort.ReadChar()) to covert decimal to char, but it's too slow.
I tried decimal.ToChar(MySerialPort.ReadChar()), but VB doesn't recognize decimal.ToChar, even after I installed .NET4.
Did I do something wrong? how to make MySerialPort.ReadChar() to read Hex data and return char?
Please help..
nx2ms
Entire code is here: sorry about the format.
******************************************************
Imports System
Imports System.IO
Imports System.IO.Ports
Imports System.Text
Public Class Form2
Dim WithEvents ComPort As SerialPort =
New SerialPort("COM3", 9600, Parity.Even, 8, StopBits.One)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If ComPort.IsOpen = False Then ComPort.Open()
MessageBox.Show(ComPort.BaudRate & ComPort.PortName & ComPort.Parity & ComPort.StopBits)
End Sub
Private Sub ComPort_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles ComPort.DataReceived
Dim path As String = "C:\Users\blu\My Documents\work\Projects\VB_Uart\Uart_Test\test8.txt"
Dim objWriter As New System.IO.StreamWriter(path, True)
'objWriter.WriteLine(Hex(ComPort.ReadByte()))
objWriter.WriteLine(ComPort.ReadChar())
'objWriter.WriteLine(ComPort.ReadByte())
'objWriter.Write(Convert.ToChar(ComPort.ReadByte()))
objWriter.Close()
End Sub
End Class
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|