11
Showing
8 changed files
with
151 additions
and
32 deletions
| ... | @@ -10,6 +10,7 @@ | ... | @@ -10,6 +10,7 @@ |
| 10 | <set> | 10 | <set> |
| 11 | <option value="$PROJECT_DIR$" /> | 11 | <option value="$PROJECT_DIR$" /> |
| 12 | <option value="$PROJECT_DIR$/app" /> | 12 | <option value="$PROJECT_DIR$/app" /> |
| 13 | <option value="$PROJECT_DIR$/ddtest" /> | ||
| 13 | </set> | 14 | </set> |
| 14 | </option> | 15 | </option> |
| 15 | <option name="resolveExternalAnnotations" value="false" /> | 16 | <option name="resolveExternalAnnotations" value="false" /> | ... | ... |
aa.txt
0 → 100644
File mode changed
| ... | @@ -26,4 +26,6 @@ dependencies { | ... | @@ -26,4 +26,6 @@ dependencies { |
| 26 | androidTestImplementation 'com.android.support.test:runner:1.0.2' | 26 | androidTestImplementation 'com.android.support.test:runner:1.0.2' |
| 27 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' | 27 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' |
| 28 | implementation 'com.github.freyskill:SerialPortHelper:v1.0.1' | 28 | implementation 'com.github.freyskill:SerialPortHelper:v1.0.1' |
| 29 | implementation project(':ddtest') | ||
| 30 | |||
| 29 | } | 31 | } | ... | ... |
| ... | @@ -14,6 +14,8 @@ import android.widget.Spinner; | ... | @@ -14,6 +14,8 @@ import android.widget.Spinner; |
| 14 | import android.widget.TextView; | 14 | import android.widget.TextView; |
| 15 | import android.widget.Toast; | 15 | import android.widget.Toast; |
| 16 | 16 | ||
| 17 | import com.example.ddtest.HexTool; | ||
| 18 | |||
| 17 | import top.keepempty.sph.library.SerialPortConfig; | 19 | import top.keepempty.sph.library.SerialPortConfig; |
| 18 | import top.keepempty.sph.library.SerialPortFinder; | 20 | import top.keepempty.sph.library.SerialPortFinder; |
| 19 | import top.keepempty.sph.library.SerialPortHelper; | 21 | import top.keepempty.sph.library.SerialPortHelper; |
| ... | @@ -65,25 +67,24 @@ public class MainActivity extends AppCompatActivity implements AdapterView.OnIte | ... | @@ -65,25 +67,24 @@ public class MainActivity extends AppCompatActivity implements AdapterView.OnIte |
| 65 | bt.setOnClickListener(new View.OnClickListener() { | 67 | bt.setOnClickListener(new View.OnClickListener() { |
| 66 | @Override | 68 | @Override |
| 67 | public void onClick(View view) { | 69 | public void onClick(View view) { |
| 68 | String ett =getFinalHex(String.valueOf(et.getText(),String.valueOf(et2.getText(),String.valueOf(et3.getText(),String.valueOf(et4.getText()); | 70 | String ett = HexTool.generateFinalHex(String.valueOf(et.getText()), String.valueOf(et2.getText()), String.valueOf(et3.getText()), String.valueOf(et4.getText())); |
| 69 | mSendDataEt.setText(ett); | 71 | mSendDataEt.setText(ett); |
| 70 | } | 72 | } |
| 71 | }); | 73 | }); |
| 72 | } | 74 | } |
| 73 | 75 | ||
| 74 | public String getFinalHex(String pizhong,String zhongliang,String danjia,String zongjia){ | 76 | public String getFinalHex(String pizhong, String zhongliang, String danjia, String zongjia) { |
| 75 | return (chineseToHexStr("皮重","10 00") + | 77 | return (chineseToHexStr("皮重", "10 00") + |
| 76 | chineseToHexStr(pizhong,"20 00") + | 78 | chineseToHexStr(pizhong, "20 00") + |
| 77 | chineseToHexStr("重量","30 00") | 79 | chineseToHexStr("重量", "30 00") |
| 78 | + chineseToHexStr(zhongliang,"40 00") | 80 | + chineseToHexStr(zhongliang, "40 00") |
| 79 | + chineseToHexStr("单价 元","50 00") + | 81 | + chineseToHexStr("单价 元", "50 00") + |
| 80 | chineseToHexStr(danjia,"60 00") | 82 | chineseToHexStr(danjia, "60 00") |
| 81 | + chineseToHexStr("总价 元","70 00") | 83 | + chineseToHexStr("总价 元", "70 00") |
| 82 | + chineseToHexStr(zongjia,"80 00") + " 00 00 00").replace(" ",""); | 84 | + chineseToHexStr(zongjia, "80 00") + " 00 00 00").replace(" ", ""); |
| 83 | } | 85 | } |
| 84 | 86 | ||
| 85 | 87 | ||
| 86 | |||
| 87 | public String chineseToHexStr(String text, String number) { | 88 | public String chineseToHexStr(String text, String number) { |
| 88 | try { | 89 | try { |
| 89 | // 将中文文本编码为 UTF-16 BE 字节数据 | 90 | // 将中文文本编码为 UTF-16 BE 字节数据 |
| ... | @@ -104,7 +105,7 @@ public class MainActivity extends AppCompatActivity implements AdapterView.OnIte | ... | @@ -104,7 +105,7 @@ public class MainActivity extends AppCompatActivity implements AdapterView.OnIte |
| 104 | // 将长度转换为十六进制字符串,不足两位补 0 | 105 | // 将长度转换为十六进制字符串,不足两位补 0 |
| 105 | String lengthHexStr = String.format(" %02X", length); | 106 | String lengthHexStr = String.format(" %02X", length); |
| 106 | 107 | ||
| 107 | return ("5A A5" + lengthHexStr + hexStr).replace(" ",""); | 108 | return ("5A A5" + lengthHexStr + hexStr).replace(" ", ""); |
| 108 | } catch (Exception e) { | 109 | } catch (Exception e) { |
| 109 | e.printStackTrace(); | 110 | e.printStackTrace(); |
| 110 | return null; | 111 | return null; |
| ... | @@ -137,8 +138,8 @@ public class MainActivity extends AppCompatActivity implements AdapterView.OnIte | ... | @@ -137,8 +138,8 @@ public class MainActivity extends AppCompatActivity implements AdapterView.OnIte |
| 137 | mSerialPortFinder = new SerialPortFinder(); | 138 | mSerialPortFinder = new SerialPortFinder(); |
| 138 | entryValues = mSerialPortFinder.getAllDevicesPath(); | 139 | entryValues = mSerialPortFinder.getAllDevicesPath(); |
| 139 | ArrayAdapter<String> adapter = new ArrayAdapter<String>( | 140 | ArrayAdapter<String> adapter = new ArrayAdapter<String>( |
| 140 | this, android.R.layout.simple_spinner_item, | 141 | this, android.R.layout.simple_spinner_item, |
| 141 | entryValues); | 142 | entryValues); |
| 142 | mPathSpinner.setAdapter(adapter); | 143 | mPathSpinner.setAdapter(adapter); |
| 143 | 144 | ||
| 144 | mSendBtn = findViewById(R.id.sph_sendBtn); | 145 | mSendBtn = findViewById(R.id.sph_sendBtn); |
| ... | @@ -146,12 +147,12 @@ public class MainActivity extends AppCompatActivity implements AdapterView.OnIte | ... | @@ -146,12 +147,12 @@ public class MainActivity extends AppCompatActivity implements AdapterView.OnIte |
| 146 | @Override | 147 | @Override |
| 147 | public void onClick(View v) { | 148 | public void onClick(View v) { |
| 148 | String sendTxt = mSendDataEt.getText().toString().trim(); | 149 | String sendTxt = mSendDataEt.getText().toString().trim(); |
| 149 | if(TextUtils.isEmpty(sendTxt)){ | 150 | if (TextUtils.isEmpty(sendTxt)) { |
| 150 | Toast.makeText(MainActivity.this,"请输入发送命令!",Toast.LENGTH_LONG).show(); | 151 | Toast.makeText(MainActivity.this, "请输入发送命令!", Toast.LENGTH_LONG).show(); |
| 151 | return; | 152 | return; |
| 152 | } | 153 | } |
| 153 | if (sendTxt.length() % 2 == 1) { | 154 | if (sendTxt.length() % 2 == 1) { |
| 154 | Toast.makeText(MainActivity.this,"命令错误!",Toast.LENGTH_LONG).show(); | 155 | Toast.makeText(MainActivity.this, "命令错误!", Toast.LENGTH_LONG).show(); |
| 155 | return; | 156 | return; |
| 156 | } | 157 | } |
| 157 | serialPortHelper.addCommands(sendTxt); | 158 | serialPortHelper.addCommands(sendTxt); |
| ... | @@ -161,10 +162,10 @@ public class MainActivity extends AppCompatActivity implements AdapterView.OnIte | ... | @@ -161,10 +162,10 @@ public class MainActivity extends AppCompatActivity implements AdapterView.OnIte |
| 161 | mOpenBtn.setOnClickListener(new View.OnClickListener() { | 162 | mOpenBtn.setOnClickListener(new View.OnClickListener() { |
| 162 | @Override | 163 | @Override |
| 163 | public void onClick(View v) { | 164 | public void onClick(View v) { |
| 164 | if(isOpen){ | 165 | if (isOpen) { |
| 165 | serialPortHelper.closeDevice(); | 166 | serialPortHelper.closeDevice(); |
| 166 | isOpen = false; | 167 | isOpen = false; |
| 167 | }else{ | 168 | } else { |
| 168 | openSerialPort(); | 169 | openSerialPort(); |
| 169 | } | 170 | } |
| 170 | showState(); | 171 | showState(); |
| ... | @@ -175,7 +176,7 @@ public class MainActivity extends AppCompatActivity implements AdapterView.OnIte | ... | @@ -175,7 +176,7 @@ public class MainActivity extends AppCompatActivity implements AdapterView.OnIte |
| 175 | /** | 176 | /** |
| 176 | * 打开串口 | 177 | * 打开串口 |
| 177 | */ | 178 | */ |
| 178 | private void openSerialPort(){ | 179 | private void openSerialPort() { |
| 179 | 180 | ||
| 180 | /** | 181 | /** |
| 181 | * 串口参数 | 182 | * 串口参数 |
| ... | @@ -185,7 +186,7 @@ public class MainActivity extends AppCompatActivity implements AdapterView.OnIte | ... | @@ -185,7 +186,7 @@ public class MainActivity extends AppCompatActivity implements AdapterView.OnIte |
| 185 | serialPortConfig.path = path; | 186 | serialPortConfig.path = path; |
| 186 | serialPortConfig.baudRate = baudRate; | 187 | serialPortConfig.baudRate = baudRate; |
| 187 | serialPortConfig.dataBits = dataBits; | 188 | serialPortConfig.dataBits = dataBits; |
| 188 | serialPortConfig.parity = checkBits; | 189 | serialPortConfig.parity = checkBits; |
| 189 | serialPortConfig.stopBits = stopBits; | 190 | serialPortConfig.stopBits = stopBits; |
| 190 | 191 | ||
| 191 | 192 | ||
| ... | @@ -195,8 +196,8 @@ public class MainActivity extends AppCompatActivity implements AdapterView.OnIte | ... | @@ -195,8 +196,8 @@ public class MainActivity extends AppCompatActivity implements AdapterView.OnIte |
| 195 | serialPortHelper.setConfigInfo(serialPortConfig); | 196 | serialPortHelper.setConfigInfo(serialPortConfig); |
| 196 | // 开启串口 | 197 | // 开启串口 |
| 197 | isOpen = serialPortHelper.openDevice(); | 198 | isOpen = serialPortHelper.openDevice(); |
| 198 | if(!isOpen){ | 199 | if (!isOpen) { |
| 199 | Toast.makeText(this,"串口打开失败!",Toast.LENGTH_LONG).show(); | 200 | Toast.makeText(this, "串口打开失败!", Toast.LENGTH_LONG).show(); |
| 200 | } | 201 | } |
| 201 | serialPortHelper.setSphResultCallback(new SphResultCallback() { | 202 | serialPortHelper.setSphResultCallback(new SphResultCallback() { |
| 202 | @Override | 203 | @Override |
| ... | @@ -250,15 +251,15 @@ public class MainActivity extends AppCompatActivity implements AdapterView.OnIte | ... | @@ -250,15 +251,15 @@ public class MainActivity extends AppCompatActivity implements AdapterView.OnIte |
| 250 | 251 | ||
| 251 | } | 252 | } |
| 252 | 253 | ||
| 253 | private void showState(){ | 254 | private void showState() { |
| 254 | if(isOpen){ | 255 | if (isOpen) { |
| 255 | Toast.makeText(this,"串口打开成功!",Toast.LENGTH_LONG).show(); | 256 | Toast.makeText(this, "串口打开成功!", Toast.LENGTH_LONG).show(); |
| 256 | mOpenBtn.setText("关闭串口"); | 257 | mOpenBtn.setText("关闭串口"); |
| 257 | mOpenBtn.setTextColor(ContextCompat.getColor(this,R.color.org)); | 258 | mOpenBtn.setTextColor(ContextCompat.getColor(this, R.color.org)); |
| 258 | mOpenBtn.setBackgroundResource(R.drawable.button_style_stroke); | 259 | mOpenBtn.setBackgroundResource(R.drawable.button_style_stroke); |
| 259 | }else { | 260 | } else { |
| 260 | mOpenBtn.setText("打开串口"); | 261 | mOpenBtn.setText("打开串口"); |
| 261 | mOpenBtn.setTextColor(ContextCompat.getColor(this,R.color.white)); | 262 | mOpenBtn.setTextColor(ContextCompat.getColor(this, R.color.white)); |
| 262 | mOpenBtn.setBackgroundResource(R.drawable.button_style_org); | 263 | mOpenBtn.setBackgroundResource(R.drawable.button_style_org); |
| 263 | } | 264 | } |
| 264 | } | 265 | } | ... | ... |
| ... | @@ -3,7 +3,6 @@ plugins { | ... | @@ -3,7 +3,6 @@ plugins { |
| 3 | } | 3 | } |
| 4 | 4 | ||
| 5 | android { | 5 | android { |
| 6 | namespace 'com.example.ddtest' | ||
| 7 | compileSdkVersion 28 | 6 | compileSdkVersion 28 |
| 8 | 7 | ||
| 9 | defaultConfig { | 8 | defaultConfig { |
| ... | @@ -23,4 +22,5 @@ android { | ... | @@ -23,4 +22,5 @@ android { |
| 23 | } | 22 | } |
| 24 | 23 | ||
| 25 | dependencies { | 24 | dependencies { |
| 25 | implementation 'com.github.freyskill:SerialPortHelper:v1.0.1' | ||
| 26 | } | 26 | } |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
| 1 | <?xml version="1.0" encoding="utf-8"?> | 1 | <?xml version="1.0" encoding="utf-8"?> |
| 2 | <manifest xmlns:android="http://schemas.android.com/apk/res/android"> | 2 | <manifest package="com.example.ddtest" xmlns:android="http://schemas.android.com/apk/res/android"> |
| 3 | 3 | ||
| 4 | </manifest> | 4 | </manifest> |
| ... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
| 1 | package com.example.ddtest; | ||
| 2 | |||
| 3 | import top.keepempty.sph.library.SerialPortConfig; | ||
| 4 | import top.keepempty.sph.library.SerialPortHelper; | ||
| 5 | import top.keepempty.sph.library.SphResultCallback; | ||
| 6 | |||
| 7 | public class SerialPortManager { | ||
| 8 | |||
| 9 | private static final String TAG = "SerialPortManager"; | ||
| 10 | private static SerialPortManager instance; | ||
| 11 | private SerialPortHelper serialPortHelper; | ||
| 12 | private SerialPortConfig config; | ||
| 13 | private SphResultCallback callback; | ||
| 14 | private boolean isOpen; | ||
| 15 | |||
| 16 | // 默认配置 | ||
| 17 | private static final String DEFAULT_PATH = "/dev/ttyS5"; | ||
| 18 | private static final int DEFAULT_BAUD_RATE = 115200; | ||
| 19 | private static final int DEFAULT_DATA_BITS = 8; | ||
| 20 | private static final char DEFAULT_PARITY = 'N'; | ||
| 21 | private static final int DEFAULT_STOP_BITS = 1; | ||
| 22 | |||
| 23 | private SerialPortManager() { | ||
| 24 | initDefaultConfig(); | ||
| 25 | } | ||
| 26 | |||
| 27 | public static synchronized SerialPortManager getInstance() { | ||
| 28 | if (instance == null) { | ||
| 29 | instance = new SerialPortManager(); | ||
| 30 | } | ||
| 31 | return instance; | ||
| 32 | } | ||
| 33 | |||
| 34 | private void initDefaultConfig() { | ||
| 35 | config = new SerialPortConfig(); | ||
| 36 | config.path = DEFAULT_PATH; | ||
| 37 | config.baudRate = DEFAULT_BAUD_RATE; | ||
| 38 | config.dataBits = DEFAULT_DATA_BITS; | ||
| 39 | config.parity = DEFAULT_PARITY; | ||
| 40 | config.stopBits = DEFAULT_STOP_BITS; | ||
| 41 | } | ||
| 42 | |||
| 43 | // 配置参数方法 | ||
| 44 | public void setPath(String path) { | ||
| 45 | config.path = path; | ||
| 46 | } | ||
| 47 | |||
| 48 | public void setBaudRate(int baudRate) { | ||
| 49 | config.baudRate = baudRate; | ||
| 50 | } | ||
| 51 | |||
| 52 | public void setDataBits(int dataBits) { | ||
| 53 | config.dataBits = dataBits; | ||
| 54 | } | ||
| 55 | |||
| 56 | public void setParity(char parity) { | ||
| 57 | config.parity = parity; | ||
| 58 | } | ||
| 59 | |||
| 60 | public void setStopBits(int stopBits) { | ||
| 61 | config.stopBits = stopBits; | ||
| 62 | } | ||
| 63 | |||
| 64 | // 初始化串口 | ||
| 65 | public void init() { | ||
| 66 | serialPortHelper = new SerialPortHelper(16); | ||
| 67 | serialPortHelper.setConfigInfo(config); | ||
| 68 | } | ||
| 69 | |||
| 70 | // 打开/关闭串口 | ||
| 71 | public boolean openPort() { | ||
| 72 | if (isOpen) return true; | ||
| 73 | isOpen = serialPortHelper.openDevice(); | ||
| 74 | if (!isOpen) { | ||
| 75 | notifyOpenFailed(); | ||
| 76 | } | ||
| 77 | return isOpen; | ||
| 78 | } | ||
| 79 | |||
| 80 | public void closePort() { | ||
| 81 | if (!isOpen) return; | ||
| 82 | serialPortHelper.closeDevice(); | ||
| 83 | isOpen = false; | ||
| 84 | notifyPortClosed(); | ||
| 85 | } | ||
| 86 | |||
| 87 | // 发送数据 | ||
| 88 | public void sendData(String hexData) { | ||
| 89 | if (hexData == null || hexData.length() % 2 != 0) { | ||
| 90 | throw new IllegalArgumentException("Invalid hex data"); | ||
| 91 | } | ||
| 92 | serialPortHelper.addCommands(hexData); | ||
| 93 | } | ||
| 94 | |||
| 95 | // 设置回调 | ||
| 96 | public void setCallback(SphResultCallback callback) { | ||
| 97 | this.callback = callback; | ||
| 98 | } | ||
| 99 | |||
| 100 | private void notifyOpenFailed() { | ||
| 101 | if (callback != null) { | ||
| 102 | callback.onComplete(); | ||
| 103 | } | ||
| 104 | } | ||
| 105 | |||
| 106 | private void notifyPortClosed() { | ||
| 107 | if (callback != null) { | ||
| 108 | callback.onComplete(); | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | public boolean isOpen() { | ||
| 113 | return isOpen; | ||
| 114 | } | ||
| 115 | } | ||
| ... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or sign in to post a comment