Serial port API: Difference between revisions

From wizarPOS
No edit summary
No edit summary
Line 1: Line 1:
== Functions ==
== API Overview ==
The calling sequence is [[#open|open]]>[[#set_baudrate|set_baudrate]]>[[#read|read]]/[[#write|write]]>[[#fulsh_io|fulsh_io]]>[[#close|close ]]
The calling sequence is [[#open|open]]>[[#set_baudrate|set_baudrate]]>[[#read|read]]/[[#write|write]]>[[#fulsh_io|fulsh_io]]>[[#close|close ]]
=== open ===
=== <big>open</big> ===
  int esp_open(char* pDeviceName)
  <syntaxhighlight lang="c">int esp_open(char* pDeviceName)</syntaxhighlight >
Open the serial port by the specified device name.
Open the serial port by the specified device name.
This operation should be used before other operations.
This operation should be used before other operations.
 
{|class="wizarpostable"
'''Parameters'''
|-
{|
! scope="row" colspan="2" | Parameters
|-
|-
| ''pDeviceName'' || char* || Device name
| ''pDeviceName'' || '''char*:''' Device name
|}
|}
'''pDeviceName'''
'''pDeviceName'''
Line 26: Line 26:
** SLAVE MODE: USB_SLAVE_SERIAL
** SLAVE MODE: USB_SLAVE_SERIAL
** HOST_MODE: USB_HOST_SERIAL or USB_SERIAL
** HOST_MODE: USB_HOST_SERIAL or USB_SERIAL
'''Returns'''
{|
|-
|}
{|class="wizarpostable"
|-
!  scope="row" colspan="2" | Returns
|-
|  int || The result code, >= 0, handle of this device; <0 [[Error_code|error code]].
|}
 


The result code, >= 0, handle of this device; <0 [[Error_code|error code]].


=== close ===
=== <big>close</big> ===
   int esp_close(int nHandle)
   <syntaxhighlight lang="c">int esp_close(int nHandle)</syntaxhighlight >
Close the serial port opened before.
Close the serial port opened before.
The open and close apis are pair operations. If you don’t want to use this device, you should call the close api to release this device.
The open and close apis are pair operations. If you don’t want to use this device, you should call the close api to release this device.
 
{|class="wizarpostable"
'''Parameters'''
|-
! scope="row" colspan="2" | Parameters
|-
| nHandle || '''int:''' Handle of this device, returned from open
|}
{|
{|
|-
|-
| ''nHandle'' || int || Handle of this device, returned from open
|  
|}
{|class="wizarpostable"
|-
!  scope="row" colspan="2" | Returns
|-
int || The result code, >= 0, success; <0 [[Error_code|error code]].
|}
|}
'''Returns'''


The result code, >= 0, success; <0 [[Error_code|error code]].
 
=== <big>set_baudrate</big>===
=== set_baudrate===
   <syntaxhighlight lang="c">int esp_set_baudrate(int nHandle, unsigned int nBaudrate)</syntaxhighlight >
   int esp_set_baudrate(int nHandle, unsigned int nBaudrate)
Set the baud rate of the serial port so that this device can read and write in the same baud rate.
Set the baud rate of the serial port so that this device can read and write in the same baud rate.
This api should be used before read and write.
This api should be used before read and write.
 
{|class="wizarpostable"
'''Parameters'''
|-
! scope="row" colspan="2" | Parameters
|-
| nHandle || '''int:''' Handle of this device, returned from open
|-
| nBaudrate || '''int:''' Baud rate
|}
{|
{|
|-
|-
| ''nHandle'' || int || Handle of this device, returned from open
|  
|}
{|class="wizarpostable"
|-
!  scope="row" colspan="2" | Returns
|-
|-
| ''nBaudrate'' || int || Baud rate
| int || The result code, >= 0, success; <0 [[Error_code|error code]].
|}
|}
'''Returns'''


The result code, >= 0, success; <0 [[Error_code|error code]].
 
=== <big>read</big>===
=== read===
   <syntaxhighlight lang="c">int esp_read(int nHandle, unsigned char* pDataBuffer, int nExpectedDataLength, int nTimeout_MS)</syntaxhighlight >
   int esp_read(int nHandle, unsigned char* pDataBuffer, int nExpectedDataLength, int nTimeout_MS)
Get information from the serial port.
Get information from the serial port.
 
{|class="wizarpostable"
 
|-
'''Parameters'''
! scope="row" colspan="2" | Parameters
|-
| nHandle || '''int:''' Handle of this device, returned from open
|-
| pDataBuffer || '''unsigned char*:''' Data buffer
|-
| nExpectedDataLength || '''int:''' Data length to read
|-
| nTimeout_MS || '''int:''' Time in milliseconds. 0 : read and return immediately; <0: read until got data.
|}
{|
{|
|-
|-
| ''nHandle'' || int || Handle of this device, returned from open
|  
|-
|}
| ''pDataBuffer'' || unsigned char* || Data buffer
{|class="wizarpostable"
|-
|-
| ''nExpectedDataLength'' || int || Data length to read
!  scope="row" colspan="2" | Returns
|-
|-
| ''nTimeout_MS'' || int || Time in milliseconds. 0 : read and return immediately; <0: read until got data.
| int || The result code, >= 0, success; <0 [[Error_code|error code]].
|}
|}
'''Returns'''


The result code, >= 0, success; <0 [[Error_code|error code]].


=== write===
=== <big>write</big>===
   int esp_write(int nHandle, unsigned char* pDataBuffer, int nDataLength)
   <syntaxhighlight lang="c">int esp_write(int nHandle, unsigned char* pDataBuffer, int nDataLength)</syntaxhighlight >
Send information from the serial port.
Send information from the serial port.
 
{|class="wizarpostable"
 
|-
'''Parameters'''
! scope="row" colspan="2" | Parameters
|-
| nHandle || '''int:''' Handle of this device, returned from open
|-
| pDataBuffer || '''unsigned char*:''' Data buffer
|-
| nDataLength || '''int:''' Data length
|}
{|
{|
|-
|-
| ''nHandle'' || int || Handle of this device, returned from open
|  
|}
{|class="wizarpostable"
|-
|-
| ''pDataBuffer'' || unsigned char* || Data buffer
!  scope="row" colspan="2" | Returns
|-
|-
| ''nDataLength'' || int || Data length
| int || The result code, >= 0, written data length; <0 [[Error_code|error code]].
|}
|}
'''Returns'''


The result code, >= 0, written data length; <0 [[Error_code|error code]].
=== <big>flush_io</big>===
 
   <syntaxhighlight lang="c">int esp_flush_io(int nHandle)</syntaxhighlight >
=== flush_io===
   int esp_flush_io(int nHandle)
Flush the IO buffer of the serial port.
Flush the IO buffer of the serial port.
 
{|class="wizarpostable"
 
|-
'''Parameters'''
! scope="row" colspan="2" | Parameters
|-
| nHandle || '''int:''' Handle of this device, returned from open
|}
{|
{|
|-
|-
| ''nHandle'' || int || Handle of this device, returned from open
|  
|}
{|class="wizarpostable"
|-
!  scope="row" colspan="2" | Returns
|-
int || The result code, >= 0, written data length; <0 [[Error_code|error code]].
|}
|}
'''Returns'''
The result code, >= 0, success; <0 [[Error_code|error code]].

Revision as of 09:36, 2 May 2018

API Overview

The calling sequence is open>set_baudrate>read/write>fulsh_io>close

open

int esp_open(char* pDeviceName)

Open the serial port by the specified device name. This operation should be used before other operations.

Parameters
pDeviceName char*: Device name

pDeviceName

if using the fiscal module, the device name is SERIAL_EXT. Others are as follows:

  • W1/W1V2
    • SLAVE MODE: DB9
    • HOST_MODE: GS0_Q1
  • Q1
    • SLAVE MODE: WIZARHANDQ1
    • HOST_MODE: Q1_USB_SERIAL or USB_SERIAL
  • Q1V2
    • SLAVE MODE: USB_SLAVE_SERIAL
    • HOST_MODE: USB_HOST_SERIAL or USB_SERIAL
  • Q2/K2/M2
    • SLAVE MODE: USB_SLAVE_SERIAL
    • HOST_MODE: USB_HOST_SERIAL or USB_SERIAL
Returns
int The result code, >= 0, handle of this device; <0 error code.


close

int esp_close(int nHandle)

Close the serial port opened before. The open and close apis are pair operations. If you don’t want to use this device, you should call the close api to release this device.

Parameters
nHandle int: Handle of this device, returned from open
Returns
int The result code, >= 0, success; <0 error code.


set_baudrate

int esp_set_baudrate(int nHandle, unsigned int nBaudrate)

Set the baud rate of the serial port so that this device can read and write in the same baud rate. This api should be used before read and write.

Parameters
nHandle int: Handle of this device, returned from open
nBaudrate int: Baud rate
Returns
int The result code, >= 0, success; <0 error code.


read

int esp_read(int nHandle, unsigned char* pDataBuffer, int nExpectedDataLength, int nTimeout_MS)

Get information from the serial port.

Parameters
nHandle int: Handle of this device, returned from open
pDataBuffer unsigned char*: Data buffer
nExpectedDataLength int: Data length to read
nTimeout_MS int: Time in milliseconds. 0 : read and return immediately; <0: read until got data.
Returns
int The result code, >= 0, success; <0 error code.


write

int esp_write(int nHandle, unsigned char* pDataBuffer, int nDataLength)

Send information from the serial port.

Parameters
nHandle int: Handle of this device, returned from open
pDataBuffer unsigned char*: Data buffer
nDataLength int: Data length
Returns
int The result code, >= 0, written data length; <0 error code.

flush_io

int esp_flush_io(int nHandle)

Flush the IO buffer of the serial port.

Parameters
nHandle int: Handle of this device, returned from open
Returns
int The result code, >= 0, written data length; <0 error code.