Author Topic: Connect to USB Serial Port  (Read 28952 times)

minius

  • Newbie
  • *
  • Posts: 11
Connect to USB Serial Port
« on: June 20, 2016, 06:19:07 AM »
Hey Guys!
I need to connect to an Arduino Mega 2560 board to exchange some information. Is it possible to communicate by USB port? I have tried with serial port commands. I'm not sure what numbers should I put for the port. The Arduino Board is connected on COM10. My project is time sensitive, so any help is welcomed!
Thanks!

Malkom

  • Administrator
  • Newbie
  • *****
  • Posts: 464
  • Here to help you
Re: Connect to USB Serial Port
« Reply #1 on: June 20, 2016, 09:50:46 AM »
Hey Guys!
I need to connect to an Arduino Mega 2560 board to exchange some information. Is it possible to communicate by USB port? I have tried with serial port commands. I'm not sure what numbers should I put for the port. The Arduino Board is connected on COM10. My project is time sensitive, so any help is welcomed!
Thanks!

Hi,

I know very little about Arduino but they seem to be USB HID devices so requiring a Vendor & Product ID in order to communicate with them.

There is a Windows HyperNext USB project for playing around with USB devices - the set of projects can be downloaded from here:-
http://www.tigabyte.com/hnfiles/rescreator.html

The HN USB project allows one to see the attached USB devices, as well as their Vendor & Product iDs.
Chapter 29 of the HN's Language Reference PDF and HN's built in help both detail how to interact with USB devices.

Regarding Arduino, here are a few links that might be useful:-

http://forum.arduino.cc/index.php?topic=92346.0

http://stackoverflow.com/questions/22687399/register-arduino-board-to-receiver-vendor-id

http://forum.arduino.cc/index.php?topic=233614.0


Best of luck.

Malkom



I am sorry but I do not have time to answer questions by PM or email.
If you post your questions in this forum then it might help others.

minius

  • Newbie
  • *
  • Posts: 11
Re: Connect to USB Serial Port
« Reply #2 on: June 20, 2016, 11:16:54 AM »
Thank you, Malkom, for the prompt and explicit reply. I got the HN USB project (I don't know why I didn't get it in the first download last week) and I hope to solve the communication.

I'm really new to HN and haven't been coding since Basic-S, more than 20 years ago. I was looking for an easy solution to make a nice-looking software that handles some data and communication with Arduino Mega. I need to finish the project in two weeks and it took me about one week to this stage.

Your support is life-saving!

Malkom

  • Administrator
  • Newbie
  • *****
  • Posts: 464
  • Here to help you
Re: Connect to USB Serial Port
« Reply #3 on: June 20, 2016, 12:09:07 PM »
I've just noticed that Arduino also supports serial ports for two way comms. HN also supports serial ports comms and has more commands and functionality for serial than it does for USB ports. Unfortunately most modern PCs & laptops don't have serial ports but I've found that USB to serial converters work very well for controlling things like motors and LEDs etc. There is a HN serial port project in that download. Chapter 23 of the Language Reference PDF details serial ports.

I hope things go well for you especially as your time is so short - getting comms to work reliably with hardware can be so tricky although it depends greatly upon the requirements.


I am sorry but I do not have time to answer questions by PM or email.
If you post your questions in this forum then it might help others.

minius

  • Newbie
  • *
  • Posts: 11
Re: Connect to USB Serial Port
« Reply #4 on: June 26, 2016, 08:17:47 PM »
I have done some progress with the USB HID Comms procedure as I have the Arduino board now connected to the HN software. Or that is what I thought. The command USBConnectFN(1) returns the value 1, but I can't get to make any communication. The Write is not sending anything and the Read is making HN to freeze. There is not much with USBReadFN and USBWriteFN in the manual. Could you help me get to the bottom of this? Thank you!
« Last Edit: June 26, 2016, 08:34:10 PM by minius »

Malkom

  • Administrator
  • Newbie
  • *****
  • Posts: 464
  • Here to help you
Re: Connect to USB Serial Port
« Reply #5 on: June 27, 2016, 04:54:15 PM »
I have done some progress with the USB HID Comms procedure as I have the Arduino board now connected to the HN software. Or that is what I thought. The command USBConnectFN(1) returns the value 1, but I can't get to make any communication. The Write is not sending anything and the Read is making HN to freeze. There is not much with USBReadFN and USBWriteFN in the manual. Could you help me get to the bottom of this? Thank you!
I've noticed from the links below that Arduino users set up the baud rate first before trying to communicate with it. It appears they use some Arduino software on their PC to set the baud rate etc:-

https://www.safaribooksonline.com/library/view/arduino-cookbook/9781449399368/ch04.html

https://www.arduino.cc/en/Reference/Serial

http://www.ladyada.net/learn/arduino/lesson4.html

I don't know the protocols needed to communicate with an Arduino - how to set it into receive mode or how to get it to get it to transmit some info - the links above might help you.

I still think that the HyperNext serial port commands might be more useful than the USB commands as I mentioned in my previous post.
If you could get your Arduino to repeatedly send out a number then you could try to get HyperNext to read it.
Next is to get the Arduino into read mode so that it could respond to some command you sent it -  there must be some command strings that  Arduino understands so it knows how to treat the data it has just received.

By the way, personally i prefer HyperNext 3.83 over v4.x because I have used v3.83 in several serial and USB projects whereas V4.x is untested. v4.x should work but the newer RB compiler used to compile it had many issues.





« Last Edit: June 27, 2016, 04:58:08 PM by Malkom »
I am sorry but I do not have time to answer questions by PM or email.
If you post your questions in this forum then it might help others.

minius

  • Newbie
  • *
  • Posts: 11
Re: Connect to USB Serial Port
« Reply #6 on: July 30, 2016, 02:19:17 PM »
I got the HN to communicate with Arduino board quite easy by sending text. The main problem now is that I need to send data as bytes and not as text. Is there a function in HN to convert or set variables to bytes?

Malkom

  • Administrator
  • Newbie
  • *****
  • Posts: 464
  • Here to help you
Re: Connect to USB Serial Port
« Reply #7 on: August 01, 2016, 07:14:28 AM »
I got the HN to communicate with Arduino board quite easy by sending text. The main problem now is that I need to send data as bytes and not as text. Is there a function in HN to convert or set variables to bytes?

Yes, its a bit long-winded but use the function ChrBFN to build a string of bytes.

Example, to build a byte string of the following three byte: 95, 32, 251

Code: [Select]
Local sb,res,uuid

Put ChrBFN(95) into sb
Append ChrBFN(32) onto sb
Append ChrBFN(251) onto sb

Put USBWriteFN(uuid,sb) into res


« Last Edit: August 01, 2016, 07:38:50 AM by Malkom »
I am sorry but I do not have time to answer questions by PM or email.
If you post your questions in this forum then it might help others.

minius

  • Newbie
  • *
  • Posts: 11
Re: Connect to USB Serial Port
« Reply #8 on: August 01, 2016, 08:03:03 PM »
Thank you for the example. I forgot to mention, that I use the SerialWrite function. I have succeeded to use the serial port commands for a USB connected Arduino board.

I get back with the question if it is possible to send via SerialWrite function information formatted as bytes? I have the information in HN as numbers that represent bytes (0-255). At the moment I have formated each number in the array to have 3 digits.

There is the possibility to convert the message with Arduino, but there is a mis-match between the sent message and the return message. I noticed there is a delay of 3 messages (I get the answer from the Arduino board for the message sent 3 times earlier). I can't tell if the delay is from the HN software or from the Arduino, but with the Arduino serila monitor software it works well.

Here is the code for the communication:

SerialWrite(1,info)
SerialTXWait(1)

Append SerialReadAllFN(1) onto field 14

Malkom

  • Administrator
  • Newbie
  • *****
  • Posts: 464
  • Here to help you
Re: Connect to USB Serial Port
« Reply #9 on: August 02, 2016, 03:41:00 PM »
Thank you for the example. I forgot to mention, that I use the SerialWrite function. I have succeeded to use the serial port commands for a USB connected Arduino board.

Thank you for letting me know you have made some progress and are using the SerialWrite function.


I get back with the question if it is possible to send via SerialWrite function information formatted as bytes? I have the information in HN as numbers that represent bytes (0-255). At the moment I have formated each number in the array to have 3 digits.

The strings in HN are intended to be useable with different languages and character sets like Cyrillic - therefore each character in a string actually uses 2 bytes.

Note, there are byte specific functions such as ChrBFN, LenBFN etc - perhaps you should be using these when building your byte-strings so as not to send extra information to the Arduino.


There is the possibility to convert the message with Arduino, but there is a mis-match between the sent message and the return message. I noticed there is a delay of 3 messages (I get the answer from the Arduino board for the message sent 3 times earlier). I can't tell if the delay is from the HN software or from the Arduino, but with the Arduino serila monitor software it works well.

Here is the code for the communication:

SerialWrite(1,info)
SerialTXWait(1)

Append SerialReadAllFN(1) onto field 14

Its been perhaps 10 years since I played around with this but do recall having similar problems with the serial NMEA messages coming from a GPS system. Once I understood the format then the thing worked. Not sure, but perhaps instead of using the SerialReadAllFN function I actually used the SerialReadBytesFN to read an expected number of bytes - perhaps the NMEA sentences are much simpler than an Arduino.

Have you had any luck getting the Arduino to slowly and repeatedly send out a simple string of bytes so HN can echo them to a field - it might help show up the mismatch.


I am sorry but I do not have time to answer questions by PM or email.
If you post your questions in this forum then it might help others.

Malkom

  • Administrator
  • Newbie
  • *****
  • Posts: 464
  • Here to help you
Re: Connect to USB Serial Port
« Reply #10 on: August 02, 2016, 04:20:16 PM »
Just a thought - if I were doing this I would use HyperNext V3.83 because it is well tested on serial/USB comms ports and there weren't any bugs reported on those aspects.

Regarding HyperNext 4.x - it is built using a much more modern complier that had lots of issues. HN 4.x was hardly tested on comms ports apart from some simple serial reading on Windows/OS X- but hopefully it will work for you.
I am sorry but I do not have time to answer questions by PM or email.
If you post your questions in this forum then it might help others.

minius

  • Newbie
  • *
  • Posts: 11
Re: Connect to USB Serial Port
« Reply #11 on: August 05, 2016, 06:34:53 PM »
Malkom, Thank you for the continuous support and advice. I like HN as it helped me show some results to my customer after a few days, with a couple of hours of learning the language and making the software. I already plan to make new programs in the future using HN.

I am to advance with the software in HN 4 to switch to HN3 now, but I think we found the problem in the Arduino software and I hope to have it fixed by tomorrow.

I could send and receive continuous text with HN to and from Arduino since one of the first trials as per your advice. The difficulties came when I need to send a precise message and receive a precise response. It is not very clear to me how all this works and how I could use the SerialReadBytesFN, ChrBFN, LenBFN etc functions. It will not matter if we succeed with the new Arduino software. Sending and receiving string  text was working almost well.

I have to install the new hardware and present the software to the customer on Monday, so I hope it will all work-out.

I have another issue now that HN is freezing when I try to compile or run the program. The compiler stops at about 15% and will not finish. Any advice on what I could do to solve this?

Malkom

  • Administrator
  • Newbie
  • *****
  • Posts: 464
  • Here to help you
Re: Connect to USB Serial Port
« Reply #12 on: August 08, 2016, 06:47:58 AM »
Malkom, Thank you for the continuous support and advice. I like HN as it helped me show some results to my customer after a few days, with a couple of hours of learning the language and making the software. I already plan to make new programs in the future using HN.

I am to advance with the software in HN 4 to switch to HN3 now, but I think we found the problem in the Arduino software and I hope to have it fixed by tomorrow.

I could send and receive continuous text with HN to and from Arduino since one of the first trials as per your advice. The difficulties came when I need to send a precise message and receive a precise response. It is not very clear to me how all this works and how I could use the SerialReadBytesFN, ChrBFN, LenBFN etc functions. It will not matter if we succeed with the new Arduino software. Sending and receiving string  text was working almost well.

I have to install the new hardware and present the software to the customer on Monday, so I hope it will all work-out.

I have another issue now that HN is freezing when I try to compile or run the program. The compiler stops at about 15% and will not finish. Any advice on what I could do to solve this?

Thank you for your project - very impressive what you have done - nice to see it :)

I'm so sorry, the freezing is due to a HN compiler bug - it found a syntax error in your code but couldn't recover.

The syntax error is in Popup 1 on Card 1, where you have the function MiddleFN in the IF .. THEN,   Note, HN compiler does not accept functions in the IF ... THEN part:


Your code:-

Code: [Select]
Local RX,ask,valid,polita

Put 'I' into ask
Put PopupItemFN(1) into polita
Append polita onto ask
Append '000000000000000' onto ask
SerialWrite(1,ask)
SerialTxWait(1)
Put SerialReadAllFN(1) into RX
    If MiddleFN(RX,1,1)='J' Then    *** error here ***
            Put MiddleFN(RX,2,17) in RX
            Else
            Call TimeStamp
            Append 'Nu s-a putut citi polita!' onto field 6
            GotoLabel 1
    EndIf


My suggestion (4 changes) - it compiles:-

Code: [Select]
Local RX,ask,valid,polita
Local s1    *** change ***

Put 'I' into ask
Put PopupItemFN(1) into polita
Append polita onto ask
Append '000000000000000' onto ask
SerialWrite(1,ask)
SerialTxWait(1)
Put SerialReadAllFN(1) into RX
Put MiddleFN(RX,1,1) into s1   *** change etc ***
    If s1='J' Then
            Put MiddleFN(RX,2,17) into RX
            Else
            Call TimeStamp
            Append 'Nu s-a putut citi polita!' onto field 6
            GotoLabel 1
    EndIf


Also, a bug below your IF - THEN

should be:-

Code: [Select]
Put MiddleFN(RX,2,17) into RX
I am sorry but I do not have time to answer questions by PM or email.
If you post your questions in this forum then it might help others.

minius

  • Newbie
  • *
  • Posts: 11
Re: Connect to USB Serial Port
« Reply #13 on: August 23, 2016, 09:17:01 PM »
Thank you for the help. Here is a quick up-date of my progress.

I have now the program working fine for sending the information, but there is an issue with reading the received information (the reply). I need to get a reply from the Arduino board as a string of numbers. The problem is that the string is not starting from the beginning, but it has other characters from the previous replies. Sometimes the answer is missing altogether for a few messages sent from HN only to appear as a longer string on the following replies.

I have worked around this issue by making a read from the serial buffer and clearing the variable used to store SerialReadAllFN at the procedure start, just before sending the message i need a reply for. Also having a For... EndFor loop to make 9 trials of communication helped get some results. There are still a lot of incomplete readings of the replies.

Malkom

  • Administrator
  • Newbie
  • *****
  • Posts: 464
  • Here to help you
Re: Connect to USB Serial Port
« Reply #14 on: August 24, 2016, 06:58:06 AM »
Thank you for the detailed info here.

My first guess with garbled messages is some kind of data corruption - perhaps the baud rate is too high.

What about if you lower the baud rate and/or put a delay between sending each character from the Arduino?

I've done a quick web search for arduino message garbled and it gives  a lot of results - many people have a similar problem to you.

http://stackoverflow.com/questions/17893792/arduino-garbled-serial-output

I've also had this problem before using HyperNext to communicate with a serial device but it wasn't just the baud rate it was the start/stop bit setting causing the problems. HN has settings for start/stop/parity bits.


 

I am sorry but I do not have time to answer questions by PM or email.
If you post your questions in this forum then it might help others.

 

anything