Reputation: 31
I am trying to communicate with a CBC Autoanaylyser machine which sends data across RS232 serial port. These are the device settings
I am connecting it to com4 port using a serial to usb adapter
ON COM4 port I am using the following VB code to read the data coming through.
Imports System
Imports System.Threading
Imports System.IO.Ports
Imports System.ComponentModel
Public Class ComReadWrite
Dim myPorts As Array
Dim txtline As String
Dim txtchar As String
Dim txtbyte As String
Dim txtexisting As String
Delegate Sub setTextCallBack(ByVal txt As String)
Private Sub ComReadWrite_Load(sender As Object, e As EventArgs) Handles MyBase.Load
myPorts = IO.Ports.SerialPort.GetPortNames()
portnamecombo.Items.AddRange(myPorts)
WriteButton.Enabled = False
CloseButton.Enabled = False
End Sub
Private Sub Start_Click(sender As Object, e As EventArgs) Handles Start.Click
SerialPort1.PortName = portnamecombo.Text
SerialPort1.BaudRate = BaudRateBox.Text
SerialPort1.ReadTimeout = 500
SerialPort1.Parity = Parity.None
SerialPort1.DataBits = 8
SerialPort1.StopBits = StopBits.One
SerialPort1.Open()
Start.Enabled = False
WriteButton.Enabled = True
CloseButton.Enabled = True
End Sub
Private Sub WriteButton_Click(sender As Object, e As EventArgs) Handles WriteButton.Click
SerialPort1.Write(WriteBox.Text)
End Sub
Private Sub CloseButton_Click(sender As Object, e As EventArgs) Handles CloseButton.Click
SerialPort1.Close()
Start.Enabled = True
WriteButton.Enabled = False
CloseButton.Enabled = False
End Sub
Private Sub SerialPort1_DataReceived(sender As Object, e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
RecievedText(SerialPort1.ReadExisting())
End Sub
Private Sub RecievedText(ByVal txt As String)
If Me.ReadBox.InvokeRequired Then
Dim x As New setTextCallBack(AddressOf RecievedText)
Me.Invoke(x, New Object() {(txt)})
Else
Me.ReadBox.Text &= txt
End If
End Sub
Private Sub portnamecombo_SelectedIndexChanged(sender As Object, e As EventArgs) Handles portnamecombo.SelectedIndexChanged
End Sub
Private Sub Label5_Click(sender As Object, e As EventArgs)
End Sub
Private Sub BindingSource1_CurrentChanged(sender As Object, e As EventArgs) Handles BindingSource1.CurrentChanged
End Sub
End Class
On running the program the form only reads weird unreadable characters like OOOOOOOO but not any thing readable as shown in pic
The documentation which came with the device has the following pages which seem to be relevant.
Upvotes: 1
Views: 1582
Reputation: 31
We need to communicate the commands in ASCII code. Enq is Chr(5) ack is Chr(6). These were the unreadable characters.
Hey Thanks to the Stack overflow community for the help. I found the solution it is as follows
The machine is supposed to say enq to which host is supposed to reply ack. But when I read the serial port machine seems to be sending an unreadable character.
Well the enq is equal to ASCII code 5 which does not have any character associated with it so it's unreadable character. So if instead of serialport1.readexisting() I write serialport1.readchar() I get that the machine is saying 5. That is machine is actually sending the enq.
Now we need to send the whose ASCII value is 6
If I say serialport1.write('6') it's not get to work. What will work is serialport1.write(Chr(6))
And using this I got the machine to send the data.
Upvotes: 1