I have a windows VBasic (gasp!) program that I need to port to C++ within a linux environment. All I need help with is the Serial communication.
I have a device which constantly spits out a long string of bytes which I need to read. I also need to send the device a string of bytes.
This is done through a USB-to-serial converter to an XBEE wireless serial interface. The fact that it is wireless serial shouldn't matter, the VBasic code is very transparent.
I cannot for he life of me find any reasonable example code, tutorial, human readable help file, etc., that helps me develop very simple C++ serial communication code that works under linux. I don't want VisualStudio C++ windows only stuff. Just pure simple GNU C code.
The Visual Basic code is this:
I have a device which constantly spits out a long string of bytes which I need to read. I also need to send the device a string of bytes.
This is done through a USB-to-serial converter to an XBEE wireless serial interface. The fact that it is wireless serial shouldn't matter, the VBasic code is very transparent.
I cannot for he life of me find any reasonable example code, tutorial, human readable help file, etc., that helps me develop very simple C++ serial communication code that works under linux. I don't want VisualStudio C++ windows only stuff. Just pure simple GNU C code.
The Visual Basic code is this:
Code:
'///////////////////////
' ENABLE BUTTON COM PORT
'///////////////////////
Private Sub Enable_Click()
' SET ERROR INTERRUPT
On Error GoTo ErrorMess1
' SET BAUDRATE
MSComm1.Settings = "57600,N,8,1"
' Flow control?
If FlowControl.Value = 1 Then
MSComm1.RTSEnable = True
MSComm1.Handshaking = comRTS
Else
MSComm1.RTSEnable = False
MSComm1.Handshaking = comNone
End If
' ENABLE PORT
MSComm1.PortOpen = True
' CLEAR DISABLE OPTION
Disable.Value = False
' LOCK PORT CHANGE
PortCombo.Locked = True
GoTo finish
' ERROR MESSAGE ROUTINE
ErrorMess1:
MsgBox "Please select an available port ", vbExclamation, " PORT IS UNAVAILABLE!"
Disable.Value = True
finish:
End Sub
'///////////////////////
' RECEIVE BYTE FROM PORT
'///////////////////////
Private Function RXBYTE()
On Error GoTo error
RXDATA = DataIN(Index)
Index = Index + 1
error:
End Function
'//////////////////////////////
'RS-232 MESSAGE RECEIVE HANDLER
'//////////////////////////////
Private Sub MSComm1_OnComm()
Dim i As Integer
Dim DataSave(100) As Byte
Dim CHECKSUM, CHECKSUM_RX As Long
' ERROR CONTROL
'On Error GoTo finish
' RECEIVE MESSAGE HANDLER
If MSComm1.CommEvent = comEvReceive Then
' Read data
DataIN = StrConv(MSComm1.Input, vbFromUnicode)
Index = 0
' CONTINUE IF WRONG CHARACTER RX
CONTINUE:
' READ BYTE
Call RXBYTE
' Header
If RXDATA = 170 Then
' READ BYTE
Call RXBYTE
' Header
If RXDATA = 85 Then
' Read in Data
For i = 1 To 70
' READ BYTE
Call RXBYTE
CHECKSUM = CHECKSUM + RXDATA
' Save Data
DataSave(i) = RXDATA
Next i
' READ BYTE
Call RXBYTE
' End
If RXDATA = 255 Then
' READ BYTE
Call RXBYTE
' End
If RXDATA = 13 Then
' READ BYTE
Call RXBYTE
' Rx Checksum
CHECKSUM_RX = CLng(RXDATA) * 256
' READ BYTE
Call RXBYTE
CHECKSUM_RX = CHECKSUM_RX + RXDATA
' Checksum is valid
If CHECKSUM = CHECKSUM_RX Then
// do some data conversion from bytes to ints etc.
// ..........